Skip to content

[skip ci] fix type hints of OuptutHandlers based on its base class.#3480

Merged
vfdev-5 merged 2 commits intopytorch:masterfrom
gabrielfruet:doc/3479-output-handlers-type-hint
Jan 9, 2026
Merged

[skip ci] fix type hints of OuptutHandlers based on its base class.#3480
vfdev-5 merged 2 commits intopytorch:masterfrom
gabrielfruet:doc/3479-output-handlers-type-hint

Conversation

@gabrielfruet
Copy link
Copy Markdown
Contributor

Fixes #3479

Fix type hints for derived classes of BaseOutputHandler. They can receive None | List[str] | str , but their type hints only for None | List[str].

@github-actions github-actions bot added the module: handlers Core Handlers module label Jan 8, 2026
@vfdev-5
Copy link
Copy Markdown
Collaborator

vfdev-5 commented Jan 8, 2026

Thanks for the PR @gabrielfruet !
If you are interested in further typing improvements we can also make the change: Optional[Union[int, float]] = None into int | float = None which should be OK starting from python 3.10

@vfdev-5
Copy link
Copy Markdown
Collaborator

vfdev-5 commented Jan 8, 2026

@rchen152 we have the following pyrefly issue on this PR:

ERROR Argument `list[str]` is not assignable to parameter `handles` with type `list[Artist | tuple[Artist, ...]] | None` in function `matplotlib.pyplot.legend` [bad-argument-type]
   --> ignite/handlers/lr_finder.py:320:20
    |
320 |         plt.legend(legends)
    |                    ^^^^^^^
    |
Error: Argument `list[str]` is not assignable to parameter `handles` with type `list[Artist | tuple[Artist, ...]] | None` in function `matplotlib.pyplot.legend`
 INFO 1 error (103 suppressed)

is it pyrefly related or fixed or should we add an ignore comment? Thanks!

I checked matplotlib API: https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.legend.html
and using only labels (list of str) is possible but discouraged (4. Labeling existing plot elements)

@gabrielfruet
Copy link
Copy Markdown
Contributor Author

@vfdev-5 Thank you!

I could also make that improvement. I'll start with the BaseOutputHandler derived classes.

Are we supporting <3.10?

@vfdev-5
Copy link
Copy Markdown
Collaborator

vfdev-5 commented Jan 8, 2026

I could also make that improvement.

Thanks @gabrielfruet! Let's do that in a follow-up PRs.

Are we supporting <3.10?

python 3.9 is EOL, min supported python version is 3.10

@gabrielfruet
Copy link
Copy Markdown
Contributor Author

@vfdev-5 Also, if you could give some help. Didn't understand why these others GH actions like CPU tests ran with error. I didn't see any. Only on pyrefly.

@vfdev-5
Copy link
Copy Markdown
Collaborator

vfdev-5 commented Jan 8, 2026

We can ignore them for now. pytorch-nightly on 3.11+ started failing on some distrbuted metrics tests...

@rchen152
Copy link
Copy Markdown
Contributor

rchen152 commented Jan 8, 2026

@rchen152 we have the following pyrefly issue on this PR:

ERROR Argument `list[str]` is not assignable to parameter `handles` with type `list[Artist | tuple[Artist, ...]] | None` in function `matplotlib.pyplot.legend` [bad-argument-type]
   --> ignite/handlers/lr_finder.py:320:20
    |
320 |         plt.legend(legends)
    |                    ^^^^^^^
    |
Error: Argument `list[str]` is not assignable to parameter `handles` with type `list[Artist | tuple[Artist, ...]] | None` in function `matplotlib.pyplot.legend`
 INFO 1 error (103 suppressed)

is it pyrefly related or fixed or should we add an ignore comment? Thanks!

I checked matplotlib API: https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.legend.html and using only labels (list of str) is possible but discouraged (4. Labeling existing plot elements)

The error is due to this type signature in matplotlib-stubs, which pyrefly bundles automatically to provide better type information for matplotlib: https://github.com/hoel-bagard/matplotlib-stubs/blob/59fdaf216331aa54996e5f1149e6ee096ca63557/src/matplotlib-stubs/pyplot.pyi#L690.

With that said, I checked the description of matplotlib-stubs (https://pypi.org/project/matplotlib-stubs/), and it looks like matplotlib itself ships with official stubs, so maybe we should stop bundling matplotlib-stubs if the stubs are causing false positives.

I'll raise this with the other pyrefly devs. In the meantime, I think you can safely # pyrefly: ignore this error.

Copy link
Copy Markdown
Collaborator

@vfdev-5 vfdev-5 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks @gabrielfruet !

@vfdev-5 vfdev-5 merged commit 79642c5 into pytorch:master Jan 9, 2026
18 of 21 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

module: handlers Core Handlers module

Projects

None yet

Development

Successfully merging this pull request may close these issues.

OutputHandler typing issue: metric_names should accept str as well as Optional[List[str]] in all logger handlers

3 participants