Skip to content

Commit

Permalink
TYP: Substitute Sequence[str] for list[str]
Browse files Browse the repository at this point in the history
  • Loading branch information
has2k1 committed Oct 25, 2024
1 parent def545c commit 52737c7
Show file tree
Hide file tree
Showing 9 changed files with 25 additions and 28 deletions.
3 changes: 3 additions & 0 deletions doc/changelog.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ title: Changelog

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

- The signatures for the scale classes now list all the allowed parameters.


### New Features

- [](:class:`~plotnine.geom_text`) has gained new aesthetics
Expand Down
4 changes: 2 additions & 2 deletions plotnine/coords/coord_flip.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from .coord_cartesian import coord_cartesian

if typing.TYPE_CHECKING:
from typing import TypeVar
from typing import Sequence, TypeVar

from plotnine.scales.scale import scale

Expand Down Expand Up @@ -77,7 +77,7 @@ def sub(a: str, b: str, df: pd.DataFrame):
"""
Substitute all keys that start with a to b
"""
columns: list[str] = df.columns.tolist()
columns: Sequence[str] = df.columns.tolist()
for label in columns:
if label.startswith(a):
new_label = b + label[1:]
Expand Down
6 changes: 3 additions & 3 deletions plotnine/doctools.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from textwrap import dedent, indent

if typing.TYPE_CHECKING:
from typing import Any, Type, TypeVar
from typing import Any, Sequence, Type, TypeVar

from plotnine.geoms.geom import geom
from plotnine.stats.stat import stat
Expand Down Expand Up @@ -197,7 +197,7 @@ def dict_to_table(header: tuple[str, str], contents: dict[str, str]) -> str:
def make_signature(
name: str,
params: dict[str, Any],
common_params: list[str],
common_params: Sequence[str],
common_param_values: dict[str, Any],
) -> str:
"""
Expand Down Expand Up @@ -360,7 +360,7 @@ def parameters_str_to_dict(param_section: str) -> dict[str, str]:
"""
d = {}
previous_param = ""
param_desc: list[str] = []
param_desc: Sequence[str] = []
for line in param_section.split("\n"):
param = param_spec(line)
if param:
Expand Down
2 changes: 1 addition & 1 deletion plotnine/facets/facet_grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def __init__(
rows: Optional[str | Sequence[str]] = None,
cols: Optional[str | Sequence[str]] = None,
*,
margins: bool | list[str] = False,
margins: bool | Sequence[str] = False,
scales: Literal["fixed", "free", "free_x", "free_y"] = "fixed",
space: (
Literal["fixed", "free", "free_x", "free_y"] | FacetSpaceRatios
Expand Down
2 changes: 1 addition & 1 deletion plotnine/iapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class scale_view:
"""

scale: scale
aesthetics: list[ScaledAestheticsName]
aesthetics: Sequence[ScaledAestheticsName]
name: Optional[str]
# Trained limits of the scale
limits: tuple[float, float] | Sequence[str]
Expand Down
6 changes: 3 additions & 3 deletions plotnine/layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -518,15 +518,15 @@ def add_group(data: pd.DataFrame) -> pd.DataFrame:

def discrete_columns(
df: pd.DataFrame, ignore: Sequence[str] | pd.Index
) -> list[str]:
) -> Sequence[str]:
"""
Return a list of the discrete columns in the dataframe
Parameters
----------
df : dataframe
df :
Data
ignore : list[str]
ignore :
A list|set|tuple with the names of the columns to skip.
"""
lst = []
Expand Down
24 changes: 9 additions & 15 deletions plotnine/qplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from .themes import theme

if typing.TYPE_CHECKING:
from typing import Any, Iterable, Literal, Optional
from typing import Any, Iterable, Literal, Optional, Sequence

from plotnine.typing import DataLike

Expand All @@ -32,8 +32,8 @@ def qplot(
y: Optional[str | Iterable[Any] | range] = None,
data: Optional[DataLike] = None,
facets: str = "",
margins: bool | list[str] = False,
geom: str | list[str] | tuple[str] = "auto",
margins: bool | Sequence[str] = False,
geom: str | Sequence[str] = "auto",
xlim: Optional[tuple[float, float]] = None,
ylim: Optional[tuple[float, float]] = None,
log: Optional[Literal["x", "y", "xy"]] = None,
Expand Down Expand Up @@ -126,18 +126,15 @@ def I(value: Any) -> Any:
data = pd.DataFrame()

# Work out plot data, and modify aesthetics, if necessary
def replace_auto(lst: list[str], str2: str) -> list[str]:
def replace_auto(lst: Sequence[str], str2: str) -> Sequence[str]:
"""
Replace all occurrences of 'auto' in with str2
"""
for i, value in enumerate(lst):
if value == "auto":
lst[i] = str2
return lst
return tuple(str2 if x == "auto" else x for x in lst)

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

if array_kind.discrete(x):
replace_auto(geom, "bar")
else:
replace_auto(geom, "histogram")

name = "bar" if array_kind.discrete(x) else "histogram"
geom = replace_auto(geom, name)
else:
if x is None:
if isinstance(aesthetics["y"], typing.Sized):
Expand All @@ -171,7 +165,7 @@ def replace_auto(lst: list[str], str2: str) -> list[str]:
# We could solve the issue in layer.compute_aesthetics
# but it is not worth the extra complexity
raise PlotnineError("Cannot infer how long x should be.")
replace_auto(geom, "point")
geom = replace_auto(geom, "point")

p: ggplot = ggplot(data, aes(**aesthetics))
p.environment = environment
Expand Down
4 changes: 2 additions & 2 deletions plotnine/scales/scale.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ class scale(
is to assign `np.nan`.
"""

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

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

def __post_init__(self):
breaks = getattr(self, "breaks")
Expand Down
2 changes: 1 addition & 1 deletion plotnine/themes/themeable.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ class axis_title(axis_title_x, axis_title_y):
[](`~plotnine.themes.themeable.Themeable`) or subclasses of it.
"""

_omit: list[str] = []
_omit: Sequence[str] = ()
"""
Properties to ignore during the apply stage.
Expand Down

0 comments on commit 52737c7

Please sign in to comment.