Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add shapely stubs #12033

Merged
merged 4 commits into from
May 25, 2024
Merged

Add shapely stubs #12033

merged 4 commits into from
May 25, 2024

Conversation

hamdanal
Copy link
Contributor

@hamdanal hamdanal commented May 25, 2024

I basically copied the stubs from https://github.com/hamdanal/python-stubs/tree/5ecdeaede3a5d97b00b5d4298f13b7028c01e6eb/stubs/shapely-stubs

Note that these stubs are somewhat complete as I've been working on them for quiet some time. There is an extensive test suite which passes both mypy and pyright in addition to runtime type checking. I've also been testing those stubs in VS Code (through Pylance, no actual type checking) for a while and they seem to work as expected. Hopefully this will make the review a bit easier.

I haven't ported any of the tests to typeshed for now. I might add a subset of them later to avoid regressions.

Technical notes:

  • The stubs contain a lot of overloads. This is because shapely implements the numpy universal functions (ufunc) API and the behavior of its functions depend on whether they are passed scalar values as arguments or array-like objects.
  • The type hierarchy of objects in shapely is as follows: "All shapely objects" -> BaseGeometry -> Geometry -> object. For input types, I use shapely.Geometry as much as possible. For output types, I use shapely.geometry.BaseGeometry. This is because BaseGeometry is an implementation detail that should not be required to be imported by users -- they should be able to annotate their code using Geometry. For output types, Geometry is not very useful as it doesn't have any attributes or methods.

Related: shapely/shapely#1741 (comment)

@hamdanal hamdanal marked this pull request as draft May 25, 2024 09:38

This comment has been minimized.

@hamdanal hamdanal marked this pull request as ready for review May 25, 2024 09:52

This comment has been minimized.

Copy link
Collaborator

@srittau srittau left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Glancing through this, I see no red flags. Two optional suggestions below.

from .set_operations import *
from .strtree import *

__version__: str
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit (needs import):

Suggested change
__version__: str
__version__: Literal[str]

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you meant Final. Fixed.

Comment on lines 55 to 61
@type_check_only
class SupportsRead(Protocol[_T_co]):
def read(self) -> _T_co: ...

@type_check_only
class SupportsWrite(Protocol[_T_contra]):
def write(self, s: _T_contra, /) -> object: ...
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Considering that these are copies from _typeshed, maybe we should just re-export them? (Not that it matters terribly.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I deleted these definitions and used the typeshed one's instead. There is no need to redefine them.

Copy link
Contributor

Diff from mypy_primer, showing the effect of this PR on open source code:

psycopg (https://github.com/psycopg/psycopg)
+ psycopg/psycopg/types/shapely.py:47: error: Unused "type: ignore" comment  [unused-ignore]
+ psycopg/psycopg/types/shapely.py:52: error: Unused "type: ignore" comment  [unused-ignore]

@srittau srittau merged commit a83f02c into python:main May 25, 2024
71 checks passed
@hamdanal hamdanal deleted the shapely branch May 25, 2024 14:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants