Skip to content

Commit 9af9228

Browse files
committed
refactor(typing): Use Into1DArray alias
- Less repetition, but also helps document what the 2nd `TypeVar` is for (`from_`) - It has to be in that position to follow the rules of https://typing.python.org/en/latest/spec/generics.html#default-ordering-and-subscription-rules
1 parent d33f1ae commit 9af9228

File tree

5 files changed

+14
-20
lines changed

5 files changed

+14
-20
lines changed

narwhals/_arrow/series.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@
5353
from narwhals._arrow.typing import _AsPyType
5454
from narwhals._arrow.typing import _BasicDataType
5555
from narwhals.dtypes import DType
56+
from narwhals.typing import Into1DArray
5657
from narwhals.typing import _1DArray
5758
from narwhals.typing import _2DArray
58-
from narwhals.typing import _NumpyScalar
5959
from narwhals.utils import Version
6060
from narwhals.utils import _FullContext
6161

@@ -159,9 +159,7 @@ def _from_scalar(self, value: Any) -> Self:
159159
return super()._from_scalar(value)
160160

161161
@classmethod
162-
def from_numpy(
163-
cls, data: _1DArray | _NumpyScalar, /, *, context: _FullContext
164-
) -> Self:
162+
def from_numpy(cls, data: Into1DArray, /, *, context: _FullContext) -> Self:
165163
return cls._from_iterable(
166164
data if is_numpy_array_1d(data) else [data], name="", context=context
167165
)

narwhals/_compliant/series.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616
from narwhals._compliant.namespace import CompliantNamespace # noqa: F401
1717
from narwhals._compliant.namespace import EagerNamespace
1818
from narwhals.dtypes import DType
19+
from narwhals.typing import Into1DArray
1920
from narwhals.typing import NativeSeries
20-
from narwhals.typing import _1DArray
21-
from narwhals.typing import _NumpyScalar
21+
from narwhals.typing import _1DArray # noqa: F401
2222
from narwhals.utils import Implementation
2323
from narwhals.utils import Version
2424
from narwhals.utils import _FullContext
@@ -28,7 +28,7 @@
2828
NativeSeriesT_co = TypeVar("NativeSeriesT_co", bound="NativeSeries", covariant=True)
2929

3030

31-
class CompliantSeries(NumpyConvertible["_1DArray", "_1DArray | _NumpyScalar"], Protocol):
31+
class CompliantSeries(NumpyConvertible["_1DArray", "Into1DArray"], Protocol):
3232
@property
3333
def dtype(self) -> DType: ...
3434
@property
@@ -41,9 +41,7 @@ def __narwhals_namespace__(self) -> Any: ... # CompliantNamespace[Any, Self]: .
4141
def _from_native_series(self, series: Any) -> Self: ...
4242
def _to_expr(self) -> Any: ... # CompliantExpr[Any, Self]: ...
4343
@classmethod
44-
def from_numpy(
45-
cls, data: _1DArray | _NumpyScalar, /, *, context: _FullContext
46-
) -> Self: ...
44+
def from_numpy(cls, data: Into1DArray, /, *, context: _FullContext) -> Self: ...
4745

4846

4947
class EagerSeries(CompliantSeries, Protocol[NativeSeriesT_co]):

narwhals/_pandas_like/series.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@
4646
from narwhals._pandas_like.dataframe import PandasLikeDataFrame
4747
from narwhals._pandas_like.namespace import PandasLikeNamespace
4848
from narwhals.dtypes import DType
49+
from narwhals.typing import Into1DArray
4950
from narwhals.typing import _1DArray
5051
from narwhals.typing import _AnyDArray
51-
from narwhals.typing import _NumpyScalar
5252
from narwhals.utils import Version
5353
from narwhals.utils import _FullContext
5454

@@ -194,9 +194,7 @@ def _from_iterable(
194194
)
195195

196196
@classmethod
197-
def from_numpy(
198-
cls, data: _1DArray | _NumpyScalar, /, *, context: _FullContext
199-
) -> Self:
197+
def from_numpy(cls, data: Into1DArray, /, *, context: _FullContext) -> Self:
200198
implementation = context._implementation
201199
if implementation.is_pandas_like():
202200
arr = data if is_numpy_array_1d(data) else [data]

narwhals/_polars/series.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@
2727
from narwhals._polars.expr import PolarsExpr
2828
from narwhals._polars.namespace import PolarsNamespace
2929
from narwhals.dtypes import DType
30+
from narwhals.typing import Into1DArray
3031
from narwhals.typing import _1DArray
31-
from narwhals.typing import _NumpyScalar
3232
from narwhals.utils import Version
3333
from narwhals.utils import _FullContext
3434

@@ -75,9 +75,7 @@ def _change_version(self: Self, version: Version) -> Self:
7575
)
7676

7777
@classmethod
78-
def from_numpy(
79-
cls, data: _1DArray | _NumpyScalar, /, *, context: _FullContext
80-
) -> Self:
78+
def from_numpy(cls, data: Into1DArray, /, *, context: _FullContext) -> Self:
8179
return cls(
8280
pl.Series(data if is_numpy_array_1d(data) else [data]),
8381
backend_version=context._backend_version,

narwhals/typing.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,10 +200,12 @@ def __native_namespace__(self) -> ModuleType: ...
200200

201201
_ShapeT = TypeVar("_ShapeT", bound="tuple[int, ...]")
202202
_NDArray: TypeAlias = "np.ndarray[_ShapeT, Any]"
203-
_1DArray: TypeAlias = "_NDArray[tuple[int]]" # noqa: PYI042, PYI047
203+
_1DArray: TypeAlias = "_NDArray[tuple[int]]" # noqa: PYI042
204204
_2DArray: TypeAlias = "_NDArray[tuple[int, int]]" # noqa: PYI042, PYI047
205205
_AnyDArray: TypeAlias = "_NDArray[tuple[int, ...]]" # noqa: PYI047
206-
_NumpyScalar: TypeAlias = "np.generic[Any]" # noqa: PYI047
206+
_NumpyScalar: TypeAlias = "np.generic[Any]"
207+
Into1DArray: TypeAlias = "_1DArray | _NumpyScalar"
208+
"""A 1-dimensional `numpy.ndarray` or scalar that can be converted into one."""
207209

208210

209211
class DTypes:

0 commit comments

Comments
 (0)