diff --git a/plotnine/_utils/__init__.py b/plotnine/_utils/__init__.py index b43518a0a..af3f9de90 100644 --- a/plotnine/_utils/__init__.py +++ b/plotnine/_utils/__init__.py @@ -316,10 +316,10 @@ def len_unique(x): if drop: return _id_var(res, drop) else: - return list(res) + return res -def _id_var(x: pd.Series[Any], drop: bool = False) -> list[int]: +def _id_var(x: AnyArrayLike, drop: bool = False) -> list[int]: """ Assign ids to items in x. If two items are the same, they get the same id. @@ -334,7 +334,7 @@ def _id_var(x: pd.Series[Any], drop: bool = False) -> list[int]: if len(x) == 0: return [] - if array_kind.categorical(x): + if isinstance(x, pd.Series) and array_kind.categorical(x): if drop: x = x.cat.remove_unused_categories() lst = list(x.cat.codes + 1) diff --git a/plotnine/facets/facet.py b/plotnine/facets/facet.py index ddae33b5a..8a1cdae64 100644 --- a/plotnine/facets/facet.py +++ b/plotnine/facets/facet.py @@ -405,7 +405,7 @@ def make_figure(self) -> tuple[Figure, list[Axes]]: # Create axes it = itertools.product(range(self.nrow), range(self.ncol)) for i, (row, col) in enumerate(it): - axsarr[row, col] = figure.add_subplot(gs[i]) + axsarr[row, col] = figure.add_subplot(gs[i]) # pyright: ignore[reportCallIssue,reportArgumentType] # axsarr = np.array([ # figure.add_subplot(gs[i]) diff --git a/plotnine/geoms/geom_path.py b/plotnine/geoms/geom_path.py index 90c0c76ee..8adb987cb 100644 --- a/plotnine/geoms/geom_path.py +++ b/plotnine/geoms/geom_path.py @@ -1,8 +1,8 @@ from __future__ import annotations -import typing from collections import Counter from contextlib import suppress +from typing import TYPE_CHECKING from warnings import warn import numpy as np @@ -12,7 +12,7 @@ from ..exceptions import PlotnineWarning from .geom import geom -if typing.TYPE_CHECKING: +if TYPE_CHECKING: from typing import Any, Literal, Sequence import numpy.typing as npt @@ -24,6 +24,7 @@ from plotnine.coords.coord import coord from plotnine.iapi import panel_view from plotnine.layer import layer + from plotnine.typing import BoolArray @document @@ -66,7 +67,7 @@ class geom_path(geom): } def handle_na(self, data: pd.DataFrame) -> pd.DataFrame: - def keep(x: Sequence[float]) -> npt.NDArray[np.bool_]: + def keep(x: Sequence[float]) -> BoolArray: # first non-missing to last non-missing first = match([False], x, nomatch=1, start=0)[0] last = len(x) - match([False], x[::-1], nomatch=1, start=0)[0] @@ -90,7 +91,7 @@ def keep(x: Sequence[float]) -> npt.NDArray[np.bool_]: # return data n1 = len(data) - data = data[bool_idx] + data = data.loc[bool_idx] # pyright: ignore[reportCallIssue,reportArgumentType] data.reset_index(drop=True, inplace=True) n2 = len(data) @@ -481,7 +482,7 @@ def _draw_segments(data: pd.DataFrame, ax: Axes, **params: Any): linestyle = data.loc[indices, "linetype"] coll = LineCollection( - segments, + segments, # pyright: ignore[reportArgumentType] edgecolor=edgecolor, linewidth=linewidth, linestyle=linestyle, diff --git a/plotnine/guides/guides.py b/plotnine/guides/guides.py index 125c37dbe..ba1802cd5 100644 --- a/plotnine/guides/guides.py +++ b/plotnine/guides/guides.py @@ -467,7 +467,7 @@ def _lrtb(pos): if just is None: just = (0.5, 0.5) elif just in VALID_JUSTIFICATION_WORDS: - just = ensure_xy_location(just) + just = ensure_xy_location(just) # pyright: ignore[reportArgumentType] elif isinstance(just, (float, int)): just = (just, just) return just[idx] diff --git a/plotnine/typing.py b/plotnine/typing.py index 4c30c3451..6b99d9fb2 100644 --- a/plotnine/typing.py +++ b/plotnine/typing.py @@ -38,7 +38,7 @@ def to_pandas(self) -> pd.DataFrame: # Arrays (strictly numpy) AnyArray: TypeAlias = NDArray[Any] BoolArray: TypeAlias = NDArray[np.bool_] -FloatArray: TypeAlias = NDArray[np.float64] +FloatArray: TypeAlias = NDArray[np.floating] IntArray: TypeAlias = NDArray[np.int64] StrArray: TypeAlias = NDArray[np.str_] diff --git a/pyproject.toml b/pyproject.toml index d4021ccfd..600e6b9c2 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -77,7 +77,7 @@ dev = [ ] typing = [ - "pyright==1.1.389", + "pyright==1.1.390", "ipython", "pandas-stubs", ]