Skip to content

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

Closed as not planned

Description

@cesaryuan

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.

Activity

  1. 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.[/+] on May 15, 2024
  2. ksunden commented on May 15, 2024

    @ksunden
    Member

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions