python / mypy

Optional static typing for Python
https://www.mypy-lang.org/
Other
17.88k stars 2.74k forks source link

mypy --follow-imports error or skip crashes with numpy 2.0.0 #17396

Open tomsilver opened 3 weeks ago

tomsilver commented 3 weeks ago

This is a mypy crash revealed by the release of numpy 2.0.0. See also https://github.com/numpy/numpy/issues/26720

Crash Report

The crash is shown below. Notes:

Traceback

Traceback (most recent call last):
  File "/Users/tom/Desktop/venv/bin/mypy", line 8, in <module>
    sys.exit(console_entry())
  File "/Users/tom/Desktop/venv/lib/python3.10/site-packages/mypy/__main__.py", line 15, in console_entry
    main()
  File "mypy/main.py", line 100, in main
  File "mypy/main.py", line 182, in run_build
  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 None

To Reproduce

  1. pip install numpy mypy
  2. echo "import numpy" > test.py
  3. mypy test.py --follow-imports error

Your Environment

fynnsu commented 2 weeks ago

I also got the same error running stubgen on a package, when an (non-direct) ancestor dependency used numpy>=2.0.

hauntsaninja commented 2 weeks ago

Thanks 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'
hauntsaninja commented 2 weeks ago

(also standard disclaimer: a global --follow-imports flag is fairly niche and in practice I've seen more misuse of it than legitimate use)

rgommers commented 2 weeks ago

Thank you for looking into this @hauntsaninja. Would you mind clarifying what does_not_exist and also_missing are 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 with numpy.generic, numpy.number, typing.Generic and/or numpy._typing.NDArray`. But they all seem to exist as far as I can tell.

hauntsaninja commented 2 weeks ago

@rgommers I don't think there's anything for numpy to do here, this just needs to be fixed in mypy. --follow-imports error is 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]
rgommers commented 2 weeks ago

Thanks @hauntsaninja. I opened a PR to fix that issue, it should land in numpy 2.0.1.

owillebo commented 2 weeks ago

Running stubgen over from numpy import array also 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 None
Inkaros commented 1 week ago

If 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.