Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Bug]: mypy failures in CI #29263

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
timhoffm opened this issue Dec 9, 2024 · 3 comments Β· Fixed by #29267
Closed

[Bug]: mypy failures in CI #29263

timhoffm opened this issue Dec 9, 2024 · 3 comments Β· Fixed by #29267
Milestone

Comments

@timhoffm
Copy link
Member

timhoffm commented Dec 9, 2024

Bug summary

Mypy has started reporting errors without any obvious changes on our side.

Code for reproduction

Any current CI run. E.g. https://github.com/matplotlib/matplotlib/actions/runs/12233488667/job/34120690881?pr=29052

Actual outcome

lib/matplotlib/pylab.py:48: error: Name "fft" already defined (by an import)  [no-redef]
lib/matplotlib/pylab.py:49: error: Name "random" already defined (by an import)  [no-redef]
lib/matplotlib/pylab.py:49: error: Incompatible import of "power" (imported name has type overloaded function, local name has type "_UFunc_Nin2_Nout1[Literal['power'], Literal[[18](https://github.com/matplotlib/matplotlib/actions/runs/12233488667/job/34120690881?pr=29052#step:6:19)], None]")  [assignment]
lib/matplotlib/pylab.py:50: error: Name "outer" already defined (possibly by an import)  [no-redef]
lib/matplotlib/pylab.py:50: error: Name "trace" already defined (possibly by an import)  [no-redef]
lib/matplotlib/pylab.py:50: error: Name "diagonal" already defined (possibly by an import)  [no-redef]
lib/matplotlib/pylab.py:50: error: Name "cross" already defined (possibly by an import)  [no-redef]
lib/matplotlib/pylab.py:50: error: Incompatible import of "matmul" (imported name has type overloaded function, local name has type "_GUFunc_Nin2_Nout1[Literal['matmul'], Literal[[19](https://github.com/matplotlib/matplotlib/actions/runs/12233488667/job/34120690881?pr=29052#step:6:20)], None, Literal['(n?,k),(k,m?)->(n?,m?)']]")  [assignment]
lib/matplotlib/pylab.py:63: error: Cannot assign to a type  [misc]
Found 9 errors in 1 file (checked 108 source files)
lib/matplotlib/pylab.py:48: error: Name "fft" already defined (by an import)  [no-redef]
lib/matplotlib/pylab.py:49: error: Name "random" already defined (by an import)  [no-redef]
lib/matplotlib/pylab.py:49: error: Incompatible import of "power" (imported name has type overloaded function, local name has type "_UFunc_Nin2_Nout1[Literal['power'], Literal[18], None]")  [assignment]
lib/matplotlib/pylab.py:50: error: Name "outer" already defined (possibly by an import)  [no-redef]
lib/matplotlib/pylab.py:50: error: Name "trace" already defined (possibly by an import)  [no-redef]
lib/matplotlib/pylab.py:50: error: Name "diagonal" already defined (possibly by an import)  [no-redef]
lib/matplotlib/pylab.py:50: error: Name "cross" already defined (possibly by an import)  [no-redef]
lib/matplotlib/pylab.py:50: error: Incompatible import of "matmul" (imported name has type overloaded function, local name has type "_GUFunc_Nin2_Nout1[Literal['matmul'], Literal[19], None, Literal['(n?,k),(k,m?)->(n?,m?)']]")  [assignment]
lib/matplotlib/pylab.py:63: error: Cannot assign to a type  [misc]

Suspicion

The questionable lines are

from numpy import *
from numpy.fft import *
from numpy.random import *
from numpy.linalg import *

Either mypy has changed behavior or change of numpy imports are responsible. Note that numpy 2.2 has been released yesterday, and it includes this PR changing imports numpy/numpy#24357.

@timhoffm
Copy link
Member Author

timhoffm commented Dec 9, 2024

Digging deeper, these are the duplicates:

In [4]: set(np.__all__) & set(np.fft.__all__)
Out[4]: {'fft'}

In [5]: set(np.__all__) & set(np.random.__all__)
Out[5]: {'power', 'random'}

In [6]: set(np.__all__) & set(np.linalg.__all__)
Out[6]:
{'cross',
 'diagonal',
 'matmul',
 'matrix_transpose',
 'outer',
 'tensordot',
 'trace',
 'vecdot'}
  • For fft and random we actually want the re-defs. np.fft and np.random are the submodules, but we want the same-named functions from the submodules.
  • For power the desired behavior is unclear. np.power is the power function. np.random.power is a random-number generating function with a power-distribution. np.random.power is what is currently exposed, via pylab, and that dates back to at least numpy 1.18 (before that numpy documentation is not clear whether that exists and we would have to try). But to keep compatibility, we also want to have the re-def here.
  • I haven't checked all linalg functions, but they seem to be at least partially different.

@rcomer
Copy link
Member

rcomer commented Dec 9, 2024

I wonder if we should just add pylab to the Mypy exclude list

matplotlib/pyproject.toml

Lines 223 to 238 in b358c98

exclude = [
#stubtest
".*/matplotlib/(sphinxext|backends|testing/jpl_units)",
#mypy precommit
"galleries/",
"doc/",
"lib/matplotlib/backends/",
"lib/matplotlib/sphinxext",
"lib/matplotlib/testing/jpl_units",
"lib/mpl_toolkits/",
#removing tests causes errors in backends
"lib/matplotlib/tests/",
# tinypages is used for testing the sphinx ext,
# stubtest will import and run, opening a figure if not excluded
".*/tinypages"
]

@timhoffm
Copy link
Member Author

timhoffm commented Dec 9, 2024

Excluding pylab sounds reasonable. After all, it's only a bunch of imports.

@QuLogic QuLogic added this to the v3.10.0 milestone Dec 10, 2024
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 a pull request may close this issue.

3 participants