-
-
Notifications
You must be signed in to change notification settings - Fork 7.5k
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
[Bug]: Type of Axes is unknown pyright #28022
Comments
The technical location of the class is |
Pyright understands matplotlib and goes under Why not use |
Yes, changing |
See full explanations here:
Short version is that sphinx had trouble resolving some things, so I resorted to the fully specified names in some cases. I think this particular example should be fine to be shortened, but the other places in It is possible sphinx has changed how it resolves links in type hints since then, I have not checked. That said, this reference should work for type checkers, I'm pretty sure (and does work for mypy for instance, which is what we use for our CI). It is true that >>> import matplotlib
>>> matplotlib.axes
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/kyle/src/scipy/matplotlib/lib/matplotlib/_api/__init__.py", line 217, in __getattr__
raise AttributeError(
AttributeError: module 'matplotlib' has no attribute 'axes' Adding the explicit imports (I just did it in the diff --git a/lib/matplotlib/pyplot.py b/lib/matplotlib/pyplot.py
index 2376c62439..426892afca 100644
--- a/lib/matplotlib/pyplot.py
+++ b/lib/matplotlib/pyplot.py
@@ -90,6 +90,9 @@ if TYPE_CHECKING:
import PIL.Image
from numpy.typing import ArrayLike
+ import matplotlib.axes
+ import matplotlib.artist
+ import matplotlib.backend_bases
from matplotlib.axis import Tick
from matplotlib.axes._base import _AxesBase
from matplotlib.backend_bases import RendererBase, Event The remaining flags I see from pyright in pyplot.py are largely due to either: a) types are narrowed in ways pyright doesn't (or can't) know e.g.:
b) a small handful of artist functions that do not get type hints because of the complexity of doing so (its not impossible, just never got around to it and didn't hold the rest of the work on it) I have no issue with paring down the number of flags we get with pyright, though mypy remains my preference for CI purposes for now (not totally opposed to both, but did not optimize for pyright). There were ~3000 flags from pyright that mypy did not flag when we first added type hints (10k if you include tests), so not trivial. I do use a pyright-based LSP/editor integration, but it doesn't play well with editable meson installs, so mostly don't get mpl flags unless I go looking for them. |
I like the Let me know if you want a PR for that. Off topic: I prefer pyright for now as it does not hang up once an error is encountered unlike pyright. Also it seems to have support for PEP 695 generics, and it has some strong arguments in its comparison to mypy. I would prefer mypy but it automatically skips if a type cannot be inferred and that to me is a big bummer. Pyright on the other hand does not need to append |
Bug summary
plt.axes
should returnAxes
and notmatplotlib.axes.Axes
Code for reproduction
Actual outcome
Same as above
Expected outcome
Additional information
No response
Operating system
No response
Matplotlib Version
3.8.3
Matplotlib Backend
No response
Python version
3.12
Jupyter version
No response
Installation
pip
The text was updated successfully, but these errors were encountered: