microsoft / python-type-stubs

A set of type stubs for popular Python packages. These are works in progress from the Microsoft Python team and others, with the intent that they are contributed to typeshed or to the associated packages once sufficiently complete.
MIT License
251 stars 99 forks source link

pyplot.imread is missing Path and BinaryIO as valid input (pyplot.pyi ) #314

Closed matrs closed 4 months ago

matrs commented 4 months ago

The function pyplot.imread complains when a Path object is passed as input: Argument of type "Path" cannot be assigned to parameter "fname" of type "str | FileLike" in function "imread"

import matplotlib.pyplot as plt
from pathlib import Path
path = Path('/some_dir/img.png')
plt.imread(path)  # warning here

pyplot.pyi

def imread(fname: str | FileLike, format: str = ...) -> np.ndarray: ...

plt.imread does accept Path and BinaryIO objects:

Signature:
plt.imread(
    fname: 'str | pathlib.Path | BinaryIO',
    format: 'str | None' = None,
) -> 'np.ndarray'
debonte commented 4 months ago

What version of matplotlib are you using? Given the signature that you show above which has type annotations, I'm assuming you're using 3.8.0 or later. These recent releases are py.typed so Pylance should be relying on matplotlib's inline type annotations rather than our bundled matplotlib stubs. Perhaps you don't have your venv selected within VS Code and that's why Pylance is using the stubs?

matrs commented 4 months ago

I see, i'm not sure, but probably was a problem with which environment was active (I use conda). Good to know that new versions don't rely on stubs. Thanks!