Skip to content

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

Closed
jorenham opened this issue Apr 29, 2025 · 2 comments · Fixed by #19001
Closed

PEP 561 stub packages should take precedence over bundled .pyi stubs #18997

jorenham opened this issue Apr 29, 2025 · 2 comments · Fixed by #19001
Labels
bug mypy got something wrong topic-pep-561

Comments

@jorenham
Copy link
Contributor

jorenham commented Apr 29, 2025

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 for numpy. It is a backwards-incompatible rework of the stubs that are bundled with numpy containing experimental new features, and will therefore be opt-in (once released).

If both numpy and NumType's numpy-stubs are installed, PEP 561 states that numpy-stubs should take precedence over the ones from numpy. Mypy does this the other way around, effectively ignoring the new numpy-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.

@JelleZijlstra
Copy link
Member

@jorenham
Copy link
Contributor Author

jorenham commented Apr 29, 2025

I'm not familiar with the mypy codebase, but maybe it's related to the order of these conditions mypy.modulefinder.FindModuleCache._find_module?

mypy/mypy/modulefinder.py

Lines 513 to 523 in 7b4f631

if fscache.isfile_case(path, dir_prefix):
has_init = True
if verify and not verify_module(fscache, id, path, dir_prefix):
near_misses.append((path, dir_prefix))
continue
return path
elif fscache.isfile_case(path_stubs, dir_prefix):
if verify and not verify_module(fscache, id, path_stubs, dir_prefix):
near_misses.append((path_stubs, dir_prefix))
continue
return path_stubs

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
Labels
bug mypy got something wrong topic-pep-561
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants