Skip to content

Navigation Menu

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

Appearance settings

ENH: Type the possible str legend locs as Literals #29465

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
Loading
from

Conversation

timhoffm
Copy link
Member

@timhoffm timhoffm commented Jan 12, 2025

Instead of accepting any str, we only accept as set of predefined literals. For simplicity, we don't distinguish the between allowed positions for Axes legend and figure legend. It's still better to limit the allowed range to the union of both rather than to accept abitrary strings.

This is a bit cumbersome, but since loc names are hard to remember and easy to misspell, I believe it's a real benefit if in-editor type checkers could notify on an invalid loc name.

@@ -60,13 +60,16 @@ class Axes(_AxesBase):
@overload
def legend(self) -> Legend: ...
@overload
def legend(self, handles: Iterable[Artist | tuple[Artist, ...]], labels: Iterable[str], **kwargs) -> Legend: ...
def legend(self, handles: Iterable[Artist | tuple[Artist, ...]], labels: Iterable[str],
*, loc: LegendLocType | None = ..., **kwargs) -> Legend: ...
Copy link
Member Author

Choose a reason for hiding this comment

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

I hope this works. I've pulled loc out of kwargs only for the stub; the method signature still does not contain it and handles it via kwargs instead. At least mypy doesn't complain, but I don't understand typing well enough whether that's guaranteed to work.

Copy link
Member

Choose a reason for hiding this comment

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

It should be fine since we haven't typed kwargs, I think.

lib/matplotlib/legend.pyi Outdated Show resolved Hide resolved
@tacaswell tacaswell added this to the v3.11.0 milestone Jan 17, 2025
Instead of accepting any str, we only accept as set of predefined
literals. For simplicity, we don't distinguish the between allowed
positions for Axes legend and figure legend. It's still better to limit
the allowed range to the union of both rather than to accept abitrary
strings.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.