Skip to content

[ENH]: More accurate type stub for plt.subplots like VSCode did before. #28226

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
cesaryuan opened this issue May 15, 2024 · 1 comment
Closed

Comments

@cesaryuan
Copy link
Contributor

cesaryuan commented May 15, 2024

Problem

Before matplotlib 3.8, VSCode embedded its type stubs for matplotlib. At that time, the typehint of plt.subplots is better than the typehint provided by matplotlib itself. You can see the differences below:

Let's see the embedded typehint for plt.subplots. I made it available by changing the Matplotlib version to 3.7. It can identify the ax type correctly.

However, after upgrading to matplotlib 3.8. The VSCode use the type stub provided by matplotlib instead of the one embedded by itself. And it cannot get the corrent type for ax.

image
image

Proposed solution

Adapt the type stub provided by VSCode. You can see them in https://github.com/microsoft/python-type-stubs/blob/102c1448f7504d6450a221ae9c29a260fa741a30/matplotlib/pyplot.pyi#L200C1-L209C30.

I tried just putting the pyplot.pyi file in the pyplot.py folder. It works. So maybe we can do it in repo.

@cesaryuan cesaryuan changed the title [ENH]: More accurate type stub for plts.subplots like VSCode did before. [ENH]: More accurate type stub for plt.subplots like VSCode did before. May 15, 2024
@ksunden
Copy link
Member

ksunden commented May 15, 2024

The short version is "its complicated" and, while we can do better the perfect behavior is not super possible, since this method returns different types based on the value of parameters. Any is a shortcut for "at least it won't yell at users here".

Closing as a duplicate of #28210 and #27025

See also #27001, which is a PR to improve this type hint, and includes a discussion of why the VSCode version is actually incorrect.

@ksunden ksunden closed this as not planned Won't fix, can't repro, duplicate, stale May 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants