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

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
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions 13 lib/matplotlib/axes/_axes.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ from collections.abc import Callable, Iterable, Sequence
from typing import Any, Literal, overload
import numpy as np
from numpy.typing import ArrayLike
from matplotlib.typing import ColorType, MarkerType, LineStyleType
from matplotlib.typing import ColorType, MarkerType, LegendLocType, LineStyleType

class Axes(_AxesBase):
def get_title(self, loc: Literal["left", "center", "right"] = ...) -> str: ...
Expand All @@ -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.

@overload
def legend(self, *, handles: Iterable[Artist | tuple[Artist, ...]], **kwargs) -> Legend: ...
def legend(self, *, handles: Iterable[Artist | tuple[Artist, ...]],
loc: LegendLocType | None = ..., **kwargs) -> Legend: ...
@overload
def legend(self, labels: Iterable[str], **kwargs) -> Legend: ...
def legend(self, labels: Iterable[str],
*, loc: LegendLocType | None = ..., **kwargs) -> Legend: ...
@overload
def legend(self, **kwargs) -> Legend: ...
def legend(self, *, loc: LegendLocType | None = ..., **kwargs) -> Legend: ...

def inset_axes(
self,
Expand Down
13 changes: 8 additions & 5 deletions 13 lib/matplotlib/figure.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ from matplotlib.lines import Line2D
from matplotlib.patches import Rectangle, Patch
from matplotlib.text import Text
from matplotlib.transforms import Affine2D, Bbox, BboxBase, Transform
from .typing import ColorType, HashableList
from .typing import ColorType, HashableList, LegendLocType

_T = TypeVar("_T")

Expand Down Expand Up @@ -147,13 +147,16 @@ class FigureBase(Artist):
@overload
def legend(self) -> Legend: ...
@overload
def legend(self, handles: Iterable[Artist], labels: Iterable[str], **kwargs) -> Legend: ...
def legend(self, handles: Iterable[Artist], labels: Iterable[str],
*, loc: LegendLocType | None = ..., **kwargs) -> Legend: ...
@overload
def legend(self, *, handles: Iterable[Artist], **kwargs) -> Legend: ...
def legend(self, *, handles: Iterable[Artist],
loc: LegendLocType | None = ..., **kwargs) -> Legend: ...
@overload
def legend(self, labels: Iterable[str], **kwargs) -> Legend: ...
def legend(self, labels: Iterable[str],
*, loc: LegendLocType | None = ..., **kwargs) -> Legend: ...
@overload
def legend(self, **kwargs) -> Legend: ...
def legend(self, *, loc: LegendLocType | None = ..., **kwargs) -> Legend: ...

def text(
self,
Expand Down
7 changes: 4 additions & 3 deletions 7 lib/matplotlib/legend.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ from matplotlib.transforms import (
BboxBase,
Transform,
)
from matplotlib.typing import ColorType, LegendLocType


import pathlib
from collections.abc import Iterable
from typing import Any, Literal, overload
from .typing import ColorType


class DraggableLegend(DraggableOffsetBox):
legend: Legend
Expand Down Expand Up @@ -55,7 +56,7 @@ class Legend(Artist):
handles: Iterable[Artist | tuple[Artist, ...]],
labels: Iterable[str],
*,
loc: str | tuple[float, float] | int | None = ...,
loc: LegendLocType | None = ...,
numpoints: int | None = ...,
markerscale: float | None = ...,
markerfirst: bool = ...,
Expand Down Expand Up @@ -118,7 +119,7 @@ class Legend(Artist):
def get_texts(self) -> list[Text]: ...
def set_alignment(self, alignment: Literal["center", "left", "right"]) -> None: ...
def get_alignment(self) -> Literal["center", "left", "right"]: ...
def set_loc(self, loc: str | tuple[float, float] | int | None = ...) -> None: ...
def set_loc(self, loc: LegendLocType | None = ...) -> None: ...
def set_title(
self, title: str, prop: FontProperties | str | pathlib.Path | None = ...
) -> None: ...
Expand Down
21 changes: 21 additions & 0 deletions 21 lib/matplotlib/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,24 @@
_HT = TypeVar("_HT", bound=Hashable)
HashableList: TypeAlias = list[_HT | "HashableList[_HT]"]
"""A nested list of Hashable values."""


LegendLocType: TypeAlias = (
Literal[
# 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 arbitrary strings
"upper right", "upper left", "lower left", "lower right",
"right", "center left", "center right", "lower center", "upper center",
"center",
# Axes only
"best",
# Figure only
"outside upper left", "outside upper center", "outside upper right",
"outside right upper", "outside right center", "outside right lower",
"outside lower right", "outside lower center", "outside lower left",
"outside left lower", "outside left center", "outside left upper",
] |
tuple[float, float] |
int
)
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.