Shapely: How about adding type hints?

Created on 25 May 2019  ·  5Comments  ·  Source: Toblerity/Shapely

As Shapely currently doesn't have type hints, results produced by type checkers are far from perfect. Some examples:

from shapely.geometry import Polygon, LineString, Point

area = Polygon().area  # Inferred type: Any. Should be: float
geom_type = LineString().geom_type  # Inferred type: Union[str, List[str]]
# Should be LineString in this case
representative_point = Polygon().representative_point()
# Inferred type: Union[Polygon, Any]. Should be: Point
x = Point(1, 1).x  # Inferred type: Union[object, Tuple[object, object, object], Tuple[object, object]]
# Should be: float

and so on.

So, how about adding stub files with the type hints, at least for the most basic functionality from the docs?


Shapely version: 1.6.4.post1, installed from conda.

Most helpful comment

I would love to see type annotations in Shapely.

All 5 comments

I'm not opposed to adding stub files. However, since I'm not a mypy user (yet) and there is a lot of code to annotate, I am not going to take on this task. I would be happy to review a PR, especially if it had tests of the annotations.

I would love to see type annotations in Shapely.

Annotations would be nice to have, perhaps for the 1.8 milestone (?)

One thing to consider planning this feature is whether to put annotations into stub files to maintain Python 2.7 compatibility for future versions of shapely. Or, make the next milestone (e.g. 1.8) release Python 3+ only, and maintain a separate branch for Python 2.7 bugs.

Note that Python 2.7 will not be maintained past 2020 (about 7 months from now).

Inline type annotations will be much easier to maintain going forward.

If we want to use the new annotation syntax this will required 3.5 as a minimum version, whereas we currently support 3.4. I don't know if we've agreed this yet?

Type annotations will look like this:

def nearest_points(g1: Geometry, g2: Geometry) -> Tuple[Geometry, Geometry]:

Unless we need to support 3.4, in which case they'd need to be done as comments (not as nice!):

def nearest_points(g1, g2): # type: (Geometry, Geometry) -> Tuple[Geometry, Geometry]

My preference would be to require Python 3.5 for Shapely 1.8.

Python 3.4 reached it's end-of-life in March 2019, and #680 addressed that for Travis CI tests, so I think requiring Python 3.5+ for the next milestone (1.8?) makes sense.

But this means we'd need to maintain a 1.7 branch for Python 2.7 users (also <=3.4).

@sgillies is it time to break away from the Python 2.x era?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kannes picture kannes  ·  4Comments

LostFan123 picture LostFan123  ·  3Comments

sgillies picture sgillies  ·  5Comments

doctor-ian picture doctor-ian  ·  4Comments

pvalsecc picture pvalsecc  ·  4Comments