Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[Bug]: Type hints for xy and xycoords in annotate are too strict #30365

Open
Milestone
@Jeitan

Description

@Jeitan

Bug summary

Right now, it looks like the type hints for Axes.annotate only accept a tuple of floats as input for thexy andxycoords arguments, but that is too narrow. Specifically, if I've plotted something with a datetime on the x axis (a not-unusual practice), I am most likely going to specify the annotation position with an acceptable datetime-like object.

There are no runtime issues, but both mypy and basedpyright complain about theannotate line in the following code (ignore the messy output, it's just for example purposes).t is truly apd.Timestamp, but even if I try using a nativedatetime object the problem occurs.

Code for reproduction

fromtypingimportcastimportmatplotlib.pyplotaspltimportnumpyasnpimportpandasaspdidx=pd.date_range(start='01/01/2014',end='03/01/2014',freq='1h')n=len(idx)rng=np.random.default_rng()data=rng.normal(size=n)df=pd.DataFrame({"data":data},index=idx)daily=df.resample("D")maxs=daily.max()mins=daily.min()ax=maxs.plot()mins.plot(ax=ax)fort,xinmaxs["data"].items():t=cast(pd.Timestamp,t)x=cast(float,x)ax.annotate(str(x), (t,x))plt.show()

Actual outcome

$ basedpyright dummy1.py
/home/andrea/Code/playground/dummy1.py
/home/andrea/Code/playground/dummy1.py:26:26 - error: Argument of type "tuple[Timestamp, float]" cannot be assigned to parameter "xy" of type "tuple[float, float]" in function "annotate"
"Timestamp" is not assignable to "float" (reportArgumentType)
1 error, 0 warnings, 0 notes

$ mypy dummy1.py
dummy1.py:26: error: Argument 2 to "annotate" of "Axes" has incompatible type "tuple[Timestamp, float]"; expected "tuple[float, float]" [arg-type]
Found 1 error in 1 file (checked 1 source file)

Expected outcome

No type errors.

Additional information

Versions:
python: 3.12
matplotlib: 3.10.3
basedpyright: 1.31.0
mypy: 1.17.0

Operating system

Debian (Linux Mint)

Matplotlib Version

3.10.3

Matplotlib Backend

qtagg

Python version

3.12

Jupyter version

No response

Installation

pip

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp