Skip to content

Commit 52737c7

Browse files
committed
TYP: Substitute Sequence[str] for list[str]
1 parent def545c commit 52737c7

File tree

9 files changed

+25
-28
lines changed

9 files changed

+25
-28
lines changed

doc/changelog.qmd

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ title: Changelog
4848

4949
- Made it possile to map an aesthetic value to `None`. ({{< issue 791 >}})
5050

51+
- The signatures for the scale classes now list all the allowed parameters.
52+
53+
5154
### New Features
5255

5356
- [](:class:`~plotnine.geom_text`) has gained new aesthetics

plotnine/coords/coord_flip.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from .coord_cartesian import coord_cartesian
99

1010
if typing.TYPE_CHECKING:
11-
from typing import TypeVar
11+
from typing import Sequence, TypeVar
1212

1313
from plotnine.scales.scale import scale
1414

@@ -77,7 +77,7 @@ def sub(a: str, b: str, df: pd.DataFrame):
7777
"""
7878
Substitute all keys that start with a to b
7979
"""
80-
columns: list[str] = df.columns.tolist()
80+
columns: Sequence[str] = df.columns.tolist()
8181
for label in columns:
8282
if label.startswith(a):
8383
new_label = b + label[1:]

plotnine/doctools.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from textwrap import dedent, indent
99

1010
if typing.TYPE_CHECKING:
11-
from typing import Any, Type, TypeVar
11+
from typing import Any, Sequence, Type, TypeVar
1212

1313
from plotnine.geoms.geom import geom
1414
from plotnine.stats.stat import stat
@@ -197,7 +197,7 @@ def dict_to_table(header: tuple[str, str], contents: dict[str, str]) -> str:
197197
def make_signature(
198198
name: str,
199199
params: dict[str, Any],
200-
common_params: list[str],
200+
common_params: Sequence[str],
201201
common_param_values: dict[str, Any],
202202
) -> str:
203203
"""
@@ -360,7 +360,7 @@ def parameters_str_to_dict(param_section: str) -> dict[str, str]:
360360
"""
361361
d = {}
362362
previous_param = ""
363-
param_desc: list[str] = []
363+
param_desc: Sequence[str] = []
364364
for line in param_section.split("\n"):
365365
param = param_spec(line)
366366
if param:

plotnine/facets/facet_grid.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ def __init__(
8383
rows: Optional[str | Sequence[str]] = None,
8484
cols: Optional[str | Sequence[str]] = None,
8585
*,
86-
margins: bool | list[str] = False,
86+
margins: bool | Sequence[str] = False,
8787
scales: Literal["fixed", "free", "free_x", "free_y"] = "fixed",
8888
space: (
8989
Literal["fixed", "free", "free_x", "free_y"] | FacetSpaceRatios

plotnine/iapi.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class scale_view:
3636
"""
3737

3838
scale: scale
39-
aesthetics: list[ScaledAestheticsName]
39+
aesthetics: Sequence[ScaledAestheticsName]
4040
name: Optional[str]
4141
# Trained limits of the scale
4242
limits: tuple[float, float] | Sequence[str]

plotnine/layer.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -518,15 +518,15 @@ def add_group(data: pd.DataFrame) -> pd.DataFrame:
518518

519519
def discrete_columns(
520520
df: pd.DataFrame, ignore: Sequence[str] | pd.Index
521-
) -> list[str]:
521+
) -> Sequence[str]:
522522
"""
523523
Return a list of the discrete columns in the dataframe
524524
525525
Parameters
526526
----------
527-
df : dataframe
527+
df :
528528
Data
529-
ignore : list[str]
529+
ignore :
530530
A list|set|tuple with the names of the columns to skip.
531531
"""
532532
lst = []

plotnine/qplot.py

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
from .themes import theme
2121

2222
if typing.TYPE_CHECKING:
23-
from typing import Any, Iterable, Literal, Optional
23+
from typing import Any, Iterable, Literal, Optional, Sequence
2424

2525
from plotnine.typing import DataLike
2626

@@ -32,8 +32,8 @@ def qplot(
3232
y: Optional[str | Iterable[Any] | range] = None,
3333
data: Optional[DataLike] = None,
3434
facets: str = "",
35-
margins: bool | list[str] = False,
36-
geom: str | list[str] | tuple[str] = "auto",
35+
margins: bool | Sequence[str] = False,
36+
geom: str | Sequence[str] = "auto",
3737
xlim: Optional[tuple[float, float]] = None,
3838
ylim: Optional[tuple[float, float]] = None,
3939
log: Optional[Literal["x", "y", "xy"]] = None,
@@ -126,18 +126,15 @@ def I(value: Any) -> Any:
126126
data = pd.DataFrame()
127127

128128
# Work out plot data, and modify aesthetics, if necessary
129-
def replace_auto(lst: list[str], str2: str) -> list[str]:
129+
def replace_auto(lst: Sequence[str], str2: str) -> Sequence[str]:
130130
"""
131131
Replace all occurrences of 'auto' in with str2
132132
"""
133-
for i, value in enumerate(lst):
134-
if value == "auto":
135-
lst[i] = str2
136-
return lst
133+
return tuple(str2 if x == "auto" else x for x in lst)
137134

138135
if "auto" in geom:
139136
if "sample" in aesthetics:
140-
replace_auto(geom, "qq")
137+
geom = replace_auto(geom, "qq")
141138
elif y is None:
142139
# If x is discrete we choose geom_bar &
143140
# geom_histogram otherwise. But we need to
@@ -156,11 +153,8 @@ def replace_auto(lst: list[str], str2: str) -> list[str]:
156153
elif not hasattr(aesthetics["x"], "dtype"):
157154
x = np.asarray(aesthetics["x"])
158155

159-
if array_kind.discrete(x):
160-
replace_auto(geom, "bar")
161-
else:
162-
replace_auto(geom, "histogram")
163-
156+
name = "bar" if array_kind.discrete(x) else "histogram"
157+
geom = replace_auto(geom, name)
164158
else:
165159
if x is None:
166160
if isinstance(aesthetics["y"], typing.Sized):
@@ -171,7 +165,7 @@ def replace_auto(lst: list[str], str2: str) -> list[str]:
171165
# We could solve the issue in layer.compute_aesthetics
172166
# but it is not worth the extra complexity
173167
raise PlotnineError("Cannot infer how long x should be.")
174-
replace_auto(geom, "point")
168+
geom = replace_auto(geom, "point")
175169

176170
p: ggplot = ggplot(data, aes(**aesthetics))
177171
p.environment = environment

plotnine/scales/scale.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ class scale(
116116
is to assign `np.nan`.
117117
"""
118118

119-
aesthetics: list[ScaledAestheticsName] = field(default_factory=list)
119+
aesthetics: Sequence[ScaledAestheticsName] = ()
120120
"""
121121
Aesthetics affected by this scale. These are defined by each scale
122122
and the user should probably not change them. Have fun.
@@ -125,7 +125,7 @@ class scale(
125125
_range: RangeT = field(init=False, repr=False)
126126

127127
# Defined aesthetics for the scale
128-
_aesthetics: list[ScaledAestheticsName] = field(init=False, repr=False)
128+
_aesthetics: Sequence[ScaledAestheticsName] = field(init=False, repr=False)
129129

130130
def __post_init__(self):
131131
breaks = getattr(self, "breaks")

plotnine/themes/themeable.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ class axis_title(axis_title_x, axis_title_y):
9393
[](`~plotnine.themes.themeable.Themeable`) or subclasses of it.
9494
"""
9595

96-
_omit: list[str] = []
96+
_omit: Sequence[str] = ()
9797
"""
9898
Properties to ignore during the apply stage.
9999

0 commit comments

Comments
 (0)