-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
TYP: Add common-type overloads of subplot_mosaic #26491
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Looking over It also shows that a mosaic with a nested 2D list can't catch the overload, even if all contained items are I think if callers want to be explicit, they could annotate their mosaic as |
I also checked over the galleries, and it fixes a few issues like: fig, axd = plt.subplot_mosaic(...)
for title, ax in axd.items():
ax.set_title(title) # Previously failed as set_title didn't take a Hashable. However, I see two issues:
I'm not sure if we should be using a |
This is failing the doc build because of the changes to |
Well it looks like you sorted the Hashable list sphinx problem, but sphinx introduced new problems, PR Submitted upstream |
**fig_kw: Any | ||
) -> tuple[Figure, dict[str, matplotlib.axes.Axes]] | \ | ||
tuple[Figure, dict[_T, matplotlib.axes.Axes]] | \ | ||
tuple[Figure, dict[Hashable, matplotlib.axes.Axes]]: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Type hints can be removed from the implementation signature with overloads
they just clutter everything up trying to encompass all possible cases, and don't actually add anything that is not covered by the extant type hints
The examples from PEP 484 and the typing module docs omit the type hints on the actual implementation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(This may allow getting rid of the type ignores? not sure)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I drop all the types on the function, then mypy --strict lib/matplotlib
gives:
lib/matplotlib/pyplot.py:1652: error: Function is missing a type annotation [no-untyped-def]
and all the type ignores are no longer needed, but that's because the function's untyped, no? It is fine without --strict
though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is annoying, I don't wholly agree with that, looks like the reasoning is that it won't type check the implementation then, but I'm not too concerned about that...
I could go either way.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it can be improved by removing the Hashable
path (and making the type var have bound=Hashable
in there... may actually be able to get rid of str
from all but the mosaic arg...
Actually its not clear to me what HashableList[Hashable]
is doing for us at all, shouldn't that just be the same as HashableList
, just not having the typevar bound to any particular type?
I'll assert, without proof, that passing a single string, a mosaic list of strings, or a mosaic list all of the same type is more common than passing arbitrary unrelated hashables. Thus it is somewhat convenient if the return type stipulates that the resulting dictionary is also keyed with strings or the common type. This also fixes the type of the `per_subplot_kw` argument, which also allows dictionary keys of tuples of the entries.
Owee, I'm MrMeeseeks, Look at me. There seem to be a conflict, please backport manually. Here are approximate instructions:
And apply the correct labels and milestones. Congratulations — you did some good work! Hopefully your backport PR will be tested by the continuous integration and merged soon! Remember to remove the If these instructions are inaccurate, feel free to suggest an improvement. |
…3.8.x Backport PR #26491 on branch v3.8.x (TYP: Add common-type overloads of subplot_mosaic)
PR summary
I'll assert, without proof, that passing a single string, a mosaic list of strings, or a mosaic list all of the same type is more common than passing arbitrary unrelated hashables. Thus it is somewhat convenient if the return type stipulates that the resulting dictionary is also keyed with strings or the common type.
This also fixes the type of the
per_subplot_kw
argument, which also allows dictionary keys of tuples of the entries.I don't like that we need a few ignores, but this does work from an external point of view:
produces:
i.e., for any common-types inputs, the dictionary uses that type as key, whereas mixed types is a generic
Hashable
.PR checklist