Repository navigation
mypy --follow-imports error or skip crashes with numpy 2.0.0 #17396
Description
Activity
I also got the same error running
stubgenon a package, when an (non-direct) ancestor dependency usednumpy>=2.0.Reacted by Avasam, Pieter Pas, Joel T. Collins and Francisco DiasThanks for the issue.
I minimised the repro, in case someone has time to look into it:
rm -rf repro mkdir repro mkdir repro/np echo 'from .arraysetops import UniqueAllResult' > repro/np/__init__.pyi echo ' from typing import Generic, NamedTuple, TypeVar from np import does_not_exist from np.missing import also_missing _SCT = TypeVar("_SCT", bound=does_not_exist) class UniqueAllResult(NamedTuple, Generic[_SCT]): values: also_missing[_SCT] ' > repro/np/arraysetops.pyi touch repro/np/py.typed PYTHONPATH=repro mypy -c 'import np'(also standard disclaimer: a global
--follow-importsflag is fairly niche and in practice I've seen more misuse of it than legitimate use)Thank you for looking into this @hauntsaninja. Would you mind clarifying what
does_not_existandalso_missingare in the numpy stub file? It looks from your reproducer that there's something to fix there for us in numpy, and it has to do withnumpy.generic,numpy.number,typing.Genericand/or numpy._typing.NDArray`. But they all seem to exist as far as I can tell.@rgommers I don't think there's anything for numpy to do here, this just needs to be fixed in mypy.
--follow-imports erroris an unusual setting that makes mypy not resolve imports.That said, there is one unrelated case of a missing import in numpy's type stubs that would be good to fix:
numpy/ma/extras.pyi:2: error: Cannot find implementation or library stub for module named "numpy.lib.index_tricks" [import-not-found]Thanks @hauntsaninja. I opened a PR to fix that issue, it should land in numpy 2.0.1.
Running stubgen over
from numpy import arrayalso fails with this error.Running attached test.txt Windows batch script results in;
Python 3.11.9 mypy==1.10.0 mypy-extensions==1.0.0 numpy==2.0.0 typing_extensions==4.12.2 Traceback (most recent call last): File "<frozen runpy>", line 198, in _run_module_as_main File "<frozen runpy>", line 88, in _run_code File "C:\Temp\mypy\.venv\Scripts\stubgen.exe\__main__.py", line 7, in <module> File "mypy\stubgen.py", line 1882, in main File "mypy\stubgen.py", line 1678, in generate_stubs File "mypy\stubgen.py", line 1610, in generate_asts_for_modules File "mypy\build.py", line 192, in build File "mypy\build.py", line 266, in _build File "mypy\build.py", line 2942, in dispatch File "mypy\build.py", line 3340, in process_graph File "mypy\build.py", line 3467, in process_stale_scc File "mypy\build.py", line 2503, in write_cache File "mypy\build.py", line 1564, in write_cache File "mypy\nodes.py", line 387, in serialize File "mypy\nodes.py", line 3936, in serialize File "mypy\nodes.py", line 3873, in serialize File "mypy\nodes.py", line 3304, in serialize File "mypy\types.py", line 667, in serialize File "mypy\types.py", line 2430, in serialize File "mypy\types.py", line 1468, in serialize File "mypy\types.py", line 667, in serialize File "mypy\types.py", line 3067, in serialize AssertionError: Internal error: unresolved placeholder type NoneIf this helps at all, stubgen from mypy < 1.5.0 does not produce this error when generating stubs for modules that use numpy 2.0.0.
Reacted by Varun Agrawal, Igor Burenkov, Pieter Pas and Dong Yang Jin- added 6 commits that reference this issue
on Jun 25, 2024 4 remaining items
- added a commit that references this issue
on Nov 14, 2024 also face this problem, has to downgrade numpy to
numpy<2to solve it :(For people facing the issue, an alternative workaround to downgrading numpy is running stubgen with "--no-analysis" argument. If you typehint your sources, this should not make a noticeable difference, but will avoid the error during stubs generation.
Reacted by Vincent Le Garrec, Francisco Dias and Kitsuya Azuma- added a commit that references this issue
on Dec 28, 2024 - added a commit that references this issue
on Feb 3, 2025 - added 2 commits that reference this issue
on Feb 4, 2025 - added a commit that references this issue
on Feb 24, 2025
This is a mypy crash revealed by the release of numpy 2.0.0. See also numpy/numpy#26720
Crash Report
The crash is shown below. Notes:
Traceback
To Reproduce
pip install numpy mypyecho "import numpy" > test.pymypy test.py --follow-imports errorYour Environment
--follow-imports errormypy.ini(and other config files): N/A