Skip to content

[Bug]: Missing file pyplot.pyi for mypy typing #26919

Closed

Description

@hhoppe

Bug summary

With mypy, plt.show() results in error: Call to untyped function "show" in typed context [no-untyped-call].

Code for reproduction

import matplotlib.pyplot as plt
plt.show()
# Then run `mypy`.

Actual outcome

error: Call to untyped function "show" in typed context [no-untyped-call]

Expected outcome

No error.

Additional information

No error was reported in earlier versions.
The error appears with matplotlib 3.8.0 and mypy 1.5.1.

Operating system

Ubuntu

Matplotlib Version

3.8.0

Matplotlib Backend

No response

Python version

No response

Jupyter version

No response

Installation

pip

Activity

  1. ksunden commented on Sep 26, 2023

    @ksunden
    Member

    Pyplot is typed inline, so the lack of pyi is intentional.

    show, along with draw_if_interactive and new_figure_manager, are complicated from a static typing perspective because their signatures are actually dynamically set at runtime, and can change when switching backends...

    We could maybe at least add a -> None/-> FigureManagerBase to them which would elide this warning, though -> None will cause us to have to add ignores because we return calls to the figure manager show/draw_if_interactive

    backend_bases does define what the expected signature of these methods is, so arguably we could implement them with those expected signatures, but if any backend out there is adding args/kwargs to these methods, it might break them.

    Note that no-untyped-call is not in the default mypy configuration, so to reproduce you must also pass --disallow-untyped-calls (or --strict or have that enabled in a config file)

  2. Mihirshrivas commented on Sep 26, 2023

    @Mihirshrivas

    Issue Description

    I have encountered an issue when using mypy with matplotlib version 3.8.0. When calling plt.show(), mypy reports the following error:

    Reproducible Example

    I have created a minimal reproducible example to demonstrate the issue:

    import matplotlib.pyplot as plt
    plt.show()

    Environment Information

    Python Version: 3.11.1
    Matplotlib Version: 3.8.0
    Mypy Version: 1.5.1
    Operating System: Windows

    Expected Outcome
    I expect that no error should be reported by mypy when calling plt.show() as it has not produced errors in previous versions of matplotlib.

    Steps Taken:
    I have tried updating both mypy and matplotlib to their latest versions using pip install --upgrade mypy matplotlib. The issue still persists.

  3. ksunden commented on Sep 26, 2023

    @ksunden
    Member

    Note also that type hints are new for version 3.8.0, which is why previous versions would not have flagged this.

  4. hhoppe commented on Sep 26, 2023

    @hhoppe
    ContributorAuthor

    @ksunden All your comments are right-on. I had forgotten that a pyproject.toml file contains strict = true.

    Just now, I edited pyplot.py to have:

    def show(*args, **kwargs) -> Any:
    

    and the mypy "error" goes away.
    Would that be an appropriate PR?

    Thanks.

  5. ksunden commented on Sep 27, 2023

    @ksunden
    Member

    I guess I'd be okay with that... I'm personally don't like no-untyped-call as an error to flag, and if it weren't for such, would probably prefer to just leave things like this untyped... (I think untyped is preferable to Any just being sprinkled about, in general)

    But the pragmatic answer is probably to add something so that it doesn't flag for users.

    I'm 50/50 on whether to go with None or Any, as I think backends should not have a return for the pyplot_show function (based on other type hints/docstrings), but if it does return, we will return whatever it gives us... (actually I tried it and mypy doesn't complain about the return when we say the return type is None)

    I will note that Any as a return is also flagged by --strict, though not in calling code, only if we did that when running against mpl itself, which we are still quite far from being able to do properly.

  6. added this to the v3.8.1 milestone on Oct 4, 2023
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

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions