Shapely: 添加类型提示怎么样?

创建于 2019-05-25  ·  5评论  ·  资料来源: Toblerity/Shapely

由于 Shapely 目前没有类型提示,类型检查器产生的结果远非完美。 一些例子:

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

等等。

那么,至少对于文档中最基本的功能,如何添加带有类型提示的存根文件


Shapely 版本:1.6.4.post1,从 conda 安装。

最有用的评论

我很想在 Shapely 中看到类型注释。

所有5条评论

我不反对添加存根文件。 但是,由于我还不是 mypy 用户(还)并且有很多代码要注释,因此我不打算承担此任务。 我很乐意审查 PR,特别是如果它对注释进行了测试。

我很想在 Shapely 中看到类型注释。

注释会很好,也许是为了 1.8 里程碑(?)

考虑规划此功能的一件事是是否将注释放入存根文件中以保持 Python 2.7 与 shapely 未来版本的兼容性。 或者,使下一个里程碑(例如 1.8)仅发布 Python 3+,并为 Python 2.7 错误维护一个单独的分支。

请注意,Python 2.7 将不会在 2020 年之后(大约 7 个月后)进行维护。

内联类型注释将更容易维护。

如果我们想使用新的注解语法,最低版本需要 3.5,而我们目前支持 3.4。 我不知道我们是否已经同意了?

类型注释将如下所示:

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

除非我们需要支持 3.4,在这种情况下,它们需要作为注释来完成(不太好!):

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

我的偏好是需要 Python 3.5 for Shapely 1.8。

Python 3.4于 2019 年 3 月停产,#680 解决了 Travis CI 测试的问题,所以我认为需要 Python 3.5+ 来实现下一个里程碑(1.8?)是有道理的。

但这意味着我们需要为 Python 2.7 用户(也<=3.4)维护一个 1.7 分支。

@sgillies是时候脱离 Python 2.x 时代了吗?

此页面是否有帮助?
0 / 5 - 0 等级