From 357a8bcbabfe6a0b80e20a4ef5b5518e1c70e2bd Mon Sep 17 00:00:00 2001 From: Marco Gorelli <33491632+MarcoGorelli@users.noreply.github.com> Date: Tue, 13 May 2025 10:31:55 +0100 Subject: [PATCH] chore: Reuse SeriesT alias more, make more types TYPE_CHECKING-only --- narwhals/series.py | 4 +- narwhals/series_cat.py | 8 +-- narwhals/series_dt.py | 7 +-- narwhals/series_list.py | 9 +-- narwhals/series_str.py | 8 +-- narwhals/series_struct.py | 8 +-- narwhals/typing.py | 122 +++++++++++++++++++------------------- 7 files changed, 68 insertions(+), 98 deletions(-) diff --git a/narwhals/series.py b/narwhals/series.py index 41e9b1aa48..ca85f2ba85 100644 --- a/narwhals/series.py +++ b/narwhals/series.py @@ -20,8 +20,6 @@ from narwhals.series_struct import SeriesStructNamespace from narwhals.translate import to_native from narwhals.typing import IntoSeriesT -from narwhals.typing import NonNestedLiteral -from narwhals.typing import SingleIndexSelector from narwhals.utils import _validate_rolling_arguments from narwhals.utils import generate_repr from narwhals.utils import is_compliant_series @@ -43,9 +41,11 @@ from narwhals.dtypes import DType from narwhals.typing import ClosedInterval from narwhals.typing import FillNullStrategy + from narwhals.typing import NonNestedLiteral from narwhals.typing import NumericLiteral from narwhals.typing import RankMethod from narwhals.typing import RollingInterpolationMethod + from narwhals.typing import SingleIndexSelector from narwhals.typing import TemporalLiteral from narwhals.typing import _1DArray from narwhals.utils import Implementation diff --git a/narwhals/series_cat.py b/narwhals/series_cat.py index 0230f235c2..cb976d4e8f 100644 --- a/narwhals/series_cat.py +++ b/narwhals/series_cat.py @@ -1,14 +1,8 @@ from __future__ import annotations -from typing import TYPE_CHECKING -from typing import Any from typing import Generic -from typing import TypeVar -if TYPE_CHECKING: - from narwhals.series import Series - -SeriesT = TypeVar("SeriesT", bound="Series[Any]") +from narwhals.typing import SeriesT class SeriesCatNamespace(Generic[SeriesT]): diff --git a/narwhals/series_dt.py b/narwhals/series_dt.py index 059075cfe1..a3303f7909 100644 --- a/narwhals/series_dt.py +++ b/narwhals/series_dt.py @@ -1,16 +1,13 @@ from __future__ import annotations from typing import TYPE_CHECKING -from typing import Any from typing import Generic -from typing import TypeVar + +from narwhals.typing import SeriesT if TYPE_CHECKING: - from narwhals.series import Series from narwhals.typing import TimeUnit -SeriesT = TypeVar("SeriesT", bound="Series[Any]") - class SeriesDateTimeNamespace(Generic[SeriesT]): def __init__(self, series: SeriesT) -> None: diff --git a/narwhals/series_list.py b/narwhals/series_list.py index 651acd7053..258535f157 100644 --- a/narwhals/series_list.py +++ b/narwhals/series_list.py @@ -1,15 +1,8 @@ from __future__ import annotations -from typing import TYPE_CHECKING -from typing import Any from typing import Generic -from typing import TypeVar -if TYPE_CHECKING: - from narwhals.series import Series - - -SeriesT = TypeVar("SeriesT", bound="Series[Any]") +from narwhals.typing import SeriesT class SeriesListNamespace(Generic[SeriesT]): diff --git a/narwhals/series_str.py b/narwhals/series_str.py index 335c39d214..82d50f5a85 100644 --- a/narwhals/series_str.py +++ b/narwhals/series_str.py @@ -1,14 +1,8 @@ from __future__ import annotations -from typing import TYPE_CHECKING -from typing import Any from typing import Generic -from typing import TypeVar -if TYPE_CHECKING: - from narwhals.series import Series - -SeriesT = TypeVar("SeriesT", bound="Series[Any]") +from narwhals.typing import SeriesT class SeriesStringNamespace(Generic[SeriesT]): diff --git a/narwhals/series_struct.py b/narwhals/series_struct.py index e6542cb461..4ae26b46b9 100644 --- a/narwhals/series_struct.py +++ b/narwhals/series_struct.py @@ -1,14 +1,8 @@ from __future__ import annotations -from typing import TYPE_CHECKING -from typing import Any from typing import Generic -from typing import TypeVar -if TYPE_CHECKING: - from narwhals.series import Series - -SeriesT = TypeVar("SeriesT", bound="Series[Any]") +from narwhals.typing import SeriesT class SeriesStructNamespace(Generic[SeriesT]): diff --git a/narwhals/typing.py b/narwhals/typing.py index 982a1bb2ee..a91dba4ab4 100644 --- a/narwhals/typing.py +++ b/narwhals/typing.py @@ -46,9 +46,67 @@ def filter(self, *args: Any, **kwargs: Any) -> Any: ... class DataFrameLike(Protocol): def __dataframe__(self, *args: Any, **kwargs: Any) -> Any: ... + class SupportsNativeNamespace(Protocol): + def __native_namespace__(self) -> ModuleType: ... -class SupportsNativeNamespace(Protocol): - def __native_namespace__(self) -> ModuleType: ... + # ruff: noqa: N802 + class DTypes(Protocol): + @property + def Decimal(self) -> type[dtypes.Decimal]: ... + @property + def Int128(self) -> type[dtypes.Int128]: ... + @property + def Int64(self) -> type[dtypes.Int64]: ... + @property + def Int32(self) -> type[dtypes.Int32]: ... + @property + def Int16(self) -> type[dtypes.Int16]: ... + @property + def Int8(self) -> type[dtypes.Int8]: ... + @property + def UInt128(self) -> type[dtypes.UInt128]: ... + @property + def UInt64(self) -> type[dtypes.UInt64]: ... + @property + def UInt32(self) -> type[dtypes.UInt32]: ... + @property + def UInt16(self) -> type[dtypes.UInt16]: ... + @property + def UInt8(self) -> type[dtypes.UInt8]: ... + @property + def Float64(self) -> type[dtypes.Float64]: ... + @property + def Float32(self) -> type[dtypes.Float32]: ... + @property + def String(self) -> type[dtypes.String]: ... + @property + def Boolean(self) -> type[dtypes.Boolean]: ... + @property + def Object(self) -> type[dtypes.Object]: ... + @property + def Categorical(self) -> type[dtypes.Categorical]: ... + @property + def Enum(self) -> type[dtypes.Enum]: ... + @property + def Datetime(self) -> type[dtypes.Datetime]: ... + @property + def Duration(self) -> type[dtypes.Duration]: ... + @property + def Date(self) -> type[dtypes.Date]: ... + @property + def Field(self) -> type[dtypes.Field]: ... + @property + def Struct(self) -> type[dtypes.Struct]: ... + @property + def List(self) -> type[dtypes.List]: ... + @property + def Array(self) -> type[dtypes.Array]: ... + @property + def Unknown(self) -> type[dtypes.Unknown]: ... + @property + def Time(self) -> type[dtypes.Time]: ... + @property + def Binary(self) -> type[dtypes.Binary]: ... IntoExpr: TypeAlias = Union["Expr", str, "Series[Any]"] @@ -331,66 +389,6 @@ def __native_namespace__(self) -> ModuleType: ... MultiColSelector: TypeAlias = "MultiIndexSelector[_T] | MultiNameSelector[_T]" -# ruff: noqa: N802 -class DTypes(Protocol): - @property - def Decimal(self) -> type[dtypes.Decimal]: ... - @property - def Int128(self) -> type[dtypes.Int128]: ... - @property - def Int64(self) -> type[dtypes.Int64]: ... - @property - def Int32(self) -> type[dtypes.Int32]: ... - @property - def Int16(self) -> type[dtypes.Int16]: ... - @property - def Int8(self) -> type[dtypes.Int8]: ... - @property - def UInt128(self) -> type[dtypes.UInt128]: ... - @property - def UInt64(self) -> type[dtypes.UInt64]: ... - @property - def UInt32(self) -> type[dtypes.UInt32]: ... - @property - def UInt16(self) -> type[dtypes.UInt16]: ... - @property - def UInt8(self) -> type[dtypes.UInt8]: ... - @property - def Float64(self) -> type[dtypes.Float64]: ... - @property - def Float32(self) -> type[dtypes.Float32]: ... - @property - def String(self) -> type[dtypes.String]: ... - @property - def Boolean(self) -> type[dtypes.Boolean]: ... - @property - def Object(self) -> type[dtypes.Object]: ... - @property - def Categorical(self) -> type[dtypes.Categorical]: ... - @property - def Enum(self) -> type[dtypes.Enum]: ... - @property - def Datetime(self) -> type[dtypes.Datetime]: ... - @property - def Duration(self) -> type[dtypes.Duration]: ... - @property - def Date(self) -> type[dtypes.Date]: ... - @property - def Field(self) -> type[dtypes.Field]: ... - @property - def Struct(self) -> type[dtypes.Struct]: ... - @property - def List(self) -> type[dtypes.List]: ... - @property - def Array(self) -> type[dtypes.Array]: ... - @property - def Unknown(self) -> type[dtypes.Unknown]: ... - @property - def Time(self) -> type[dtypes.Time]: ... - @property - def Binary(self) -> type[dtypes.Binary]: ... - - __all__ = [ "CompliantDataFrame", "CompliantLazyFrame",