-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
PEP 561 stub packages should take precedence over bundled .pyi
stubs
#18997
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
Labels
Comments
Relevant section of the spec: https://typing.python.org/en/latest/spec/distributing.html#import-resolution-ordering |
This was referenced Apr 29, 2025
I'm not familiar with the mypy codebase, but maybe it's related to the order of these conditions Lines 513 to 523 in 7b4f631
|
jorenham
added a commit
to jorenham/mypy
that referenced
this issue
Apr 29, 2025
JelleZijlstra
pushed a commit
that referenced
this issue
May 1, 2025
<!-- If this pull request fixes an issue, add "Fixes #NNN" with the issue number. --> This fixes the import resolution order for stubs from a `-stubs` package and stubs bundled with a `py.typed` package, and fixes #18997. Besides the unit tests, the effectiveness of this fix is also demonstrated at https://github.com/jorenham/mypy-pep561-numpy-issue#mypy-jorenhamfix-18997-with-numtype After investigating a bit more, it looks like mypy's incorrect prioritization of stubs was limited to `__init__.pyi`. I confirmed this by adding `reveal_type(np.dtypes.StringDType())` to the `main.pyi` in https://github.com/jorenham/mypy-pep561-numpy-issue. With `mypy==1.15.0` installed, it correctly showed `numpy.dtypes.StringDType` *without* NumType, and `numpy.dtypes.StringDType[Never]` *with* NumType installed. So both #18997 and this PR only apply to `__init__.pyi`.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
NumPy relies on bundled
.pyi
stubs for static typing. They live side-by-side with the.py
(if they exist). See e.g. https://github.com/numpy/numpy/tree/main/numpy.NumType is a static-typing project that we are developing. It comes with (different)
numpy-stubs
stubs-only package fornumpy
. It is a backwards-incompatible rework of the stubs that are bundled withnumpy
containing experimental new features, and will therefore be opt-in (once released).If both
numpy
and NumType'snumpy-stubs
are installed, PEP 561 states thatnumpy-stubs
should take precedence over the ones fromnumpy
. Mypy does this the other way around, effectively ignoring the newnumpy-stubs
.This effectively means that we currently cannot support mypy. So I consider this to be a big problem.
The only workaround I was able find is to delete all
.pyi
in the local numpy installation directory. But this isn't something we can realistically ask of our users.So if any other workarounds come to mind, please let me know :)
Repro-repo with additional details
https://github.com/jorenham/mypy-pep561-numpy-issue.
The text was updated successfully, but these errors were encountered: