Skip to content

Commit 6cd8e3c

Browse files
authored
enh: Start narwhals.stable.v2 and enforce deprecations (#2814)
1 parent f99778e commit 6cd8e3c

25 files changed

+1937
-1258
lines changed

.pre-commit-config.yaml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@ repos:
3636
narwhals/dataframe\.py|
3737
# TODO: gradually enable
3838
narwhals/dependencies\.py|
39-
# TODO: gradually enable
40-
narwhals/stable/v1/dependencies\.py|
4139
# private, so less urgent to document too well
4240
narwhals/_.*|
43-
^utils/.*
41+
^utils/.*|
42+
# Soft-deprecated, so less crucial to document so carefully
43+
narwhals/stable/v1/.*
4444
)$
4545
- repo: local
4646
hooks:
@@ -69,15 +69,15 @@ repos:
6969
from\ narwhals\ import\ dtypes|
7070
from\ narwhals.dtypes\ import\ [^D_]+|
7171
import\ narwhals.stable.v1.dtypes|
72-
from\ narwhals.stable.v1\ import\ dtypes|
73-
from\ narwhals.stable.v1.dtypes\ import
72+
from\ narwhals.stable\.v.\ import\ dtypes|
73+
from\ narwhals.stable\.v.\.dtypes\ import
7474
language: pygrep
7575
files: ^narwhals/
7676
exclude: |
7777
(?x)
7878
^(
7979
narwhals/_utils\.py|
80-
narwhals/stable/v1/_dtypes.py|
80+
narwhals/stable/v./_?dtypes.py|
8181
narwhals/.*__init__.py|
8282
narwhals/.*typing\.py
8383
)

docs/api-reference/expr.md

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@
88
- alias
99
- all
1010
- any
11-
- arg_max
12-
- arg_min
13-
- arg_true
1411
- cast
1512
- count
1613
- cum_count
@@ -24,8 +21,6 @@
2421
- exp
2522
- fill_null
2623
- filter
27-
- gather_every
28-
- head
2924
- clip
3025
- is_between
3126
- is_duplicated
@@ -57,14 +52,11 @@
5752
- rolling_sum
5853
- rolling_var
5954
- round
60-
- sample
6155
- shift
62-
- sort
6356
- skew
6457
- sqrt
6558
- std
6659
- sum
67-
- tail
6860
- unique
6961
- var
7062
show_source: false

docs/api-reference/narwhals.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ Here are the top-level functions available in Narwhals.
1919
- from_native
2020
- from_numpy
2121
- generate_temporary_column_name
22-
- get_level
2322
- get_native_namespace
2423
- is_ordered_categorical
2524
- len

narwhals/__init__.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@
5757
from_arrow,
5858
from_dict,
5959
from_numpy,
60-
get_level,
6160
len_ as len,
6261
lit,
6362
max,
@@ -141,7 +140,6 @@
141140
"from_native",
142141
"from_numpy",
143142
"generate_temporary_column_name",
144-
"get_level",
145143
"get_native_namespace",
146144
"is_ordered_categorical",
147145
"len",

narwhals/_compliant/dataframe.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
ToNarwhals,
2323
ToNarwhalsT_co,
2424
)
25-
from narwhals._typing_compat import assert_never, deprecated
25+
from narwhals._typing_compat import assert_never
2626
from narwhals._utils import (
2727
ValidateBackendVersion,
2828
Version,
@@ -312,10 +312,6 @@ def drop(self, columns: Sequence[str], *, strict: bool) -> Self: ...
312312
def drop_nulls(self, subset: Sequence[str] | None) -> Self: ...
313313
def explode(self, columns: Sequence[str]) -> Self: ...
314314
def filter(self, predicate: CompliantExprT_contra | Incomplete) -> Self: ...
315-
@deprecated(
316-
"`LazyFrame.gather_every` is deprecated and will be removed in a future version."
317-
)
318-
def gather_every(self, n: int, offset: int) -> Self: ...
319315
def group_by(
320316
self,
321317
keys: Sequence[str] | Sequence[CompliantExprT_contra],
@@ -349,8 +345,6 @@ def sink_parquet(self, file: str | Path | BytesIO) -> None: ...
349345
def sort(
350346
self, *by: str, descending: bool | Sequence[bool], nulls_last: bool
351347
) -> Self: ...
352-
@deprecated("`LazyFrame.tail` is deprecated and will be removed in a future version.")
353-
def tail(self, n: int) -> Self: ...
354348
def unique(
355349
self, subset: Sequence[str] | None, *, keep: LazyUniqueKeepStrategy
356350
) -> Self: ...

narwhals/_compliant/expr.py

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
LazyExprT,
2828
NativeExprT,
2929
)
30-
from narwhals._typing_compat import deprecated
3130
from narwhals._utils import _StoresCompliant
3231
from narwhals.dependencies import get_numpy, is_numpy_array
3332

@@ -107,9 +106,6 @@ def cast(self, dtype: IntoDType) -> Self: ...
107106
def count(self) -> Self: ...
108107
def min(self) -> Self: ...
109108
def max(self) -> Self: ...
110-
def arg_min(self) -> Self: ...
111-
def arg_max(self) -> Self: ...
112-
def arg_true(self) -> Self: ...
113109
def mean(self) -> Self: ...
114110
def sum(self) -> Self: ...
115111
def median(self) -> Self: ...
@@ -134,8 +130,6 @@ def len(self) -> Self: ...
134130
def log(self, base: float) -> Self: ...
135131
def round(self, decimals: int) -> Self: ...
136132
def mode(self) -> Self: ...
137-
def head(self, n: int) -> Self: ...
138-
def tail(self, n: int) -> Self: ...
139133
def shift(self, n: int) -> Self: ...
140134
def is_finite(self) -> Self: ...
141135
def is_nan(self) -> Self: ...
@@ -148,7 +142,6 @@ def cum_min(self, *, reverse: bool) -> Self: ...
148142
def cum_max(self, *, reverse: bool) -> Self: ...
149143
def cum_prod(self, *, reverse: bool) -> Self: ...
150144
def is_in(self, other: Any) -> Self: ...
151-
def sort(self, *, descending: bool, nulls_last: bool) -> Self: ...
152145
def rank(self, method: RankMethod, *, descending: bool) -> Self: ...
153146
def replace_strict(
154147
self,
@@ -158,14 +151,6 @@ def replace_strict(
158151
return_dtype: IntoDType | None,
159152
) -> Self: ...
160153
def over(self, partition_by: Sequence[str], order_by: Sequence[str]) -> Self: ...
161-
def sample(
162-
self,
163-
n: int | None,
164-
*,
165-
fraction: float | None,
166-
with_replacement: bool,
167-
seed: int | None,
168-
) -> Self: ...
169154
def quantile(
170155
self, quantile: float, interpolation: RollingInterpolationMethod
171156
) -> Self: ...
@@ -209,8 +194,6 @@ def rolling_std(
209194
self, window_size: int, *, min_samples: int, center: bool, ddof: int
210195
) -> Self: ...
211196

212-
@deprecated("Since `1.22.0`")
213-
def gather_every(self, n: int, offset: int) -> Self: ...
214197
def __and__(self, other: Any) -> Self: ...
215198
def __or__(self, other: Any) -> Self: ...
216199
def __add__(self, other: Any) -> Self: ...

narwhals/_utils.py

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ def _validate_backend_version(self) -> None:
217217

218218
class Version(Enum):
219219
V1 = auto()
220+
V2 = auto()
220221
MAIN = auto()
221222

222223
@property
@@ -225,6 +226,10 @@ def namespace(self) -> type[Namespace[Any]]:
225226
from narwhals.stable.v1._namespace import Namespace as NamespaceV1
226227

227228
return NamespaceV1
229+
if self is Version.V2:
230+
from narwhals.stable.v2._namespace import Namespace as NamespaceV2
231+
232+
return NamespaceV2
228233
from narwhals._namespace import Namespace
229234

230235
return Namespace
@@ -235,6 +240,10 @@ def dtypes(self) -> DTypes:
235240
from narwhals.stable.v1 import dtypes as dtypes_v1
236241

237242
return dtypes_v1
243+
if self is Version.V2:
244+
from narwhals.stable.v2 import dtypes as dtypes_v2
245+
246+
return dtypes_v2
238247
from narwhals import dtypes
239248

240249
return dtypes
@@ -245,6 +254,10 @@ def dataframe(self) -> type[DataFrame[Any]]:
245254
from narwhals.stable.v1 import DataFrame as DataFrameV1
246255

247256
return DataFrameV1
257+
if self is Version.V2:
258+
from narwhals.stable.v2 import DataFrame as DataFrameV2
259+
260+
return DataFrameV2
248261
from narwhals.dataframe import DataFrame
249262

250263
return DataFrame
@@ -255,6 +268,10 @@ def lazyframe(self) -> type[LazyFrame[Any]]:
255268
from narwhals.stable.v1 import LazyFrame as LazyFrameV1
256269

257270
return LazyFrameV1
271+
if self is Version.V2:
272+
from narwhals.stable.v2 import LazyFrame as LazyFrameV2
273+
274+
return LazyFrameV2
258275
from narwhals.dataframe import LazyFrame
259276

260277
return LazyFrame
@@ -265,6 +282,10 @@ def series(self) -> type[Series[Any]]:
265282
from narwhals.stable.v1 import Series as SeriesV1
266283

267284
return SeriesV1
285+
if self is Version.V2:
286+
from narwhals.stable.v2 import Series as SeriesV2
287+
288+
return SeriesV2
268289
from narwhals.series import Series
269290

270291
return Series
@@ -1452,7 +1473,7 @@ def find_stacklevel() -> int:
14521473
return n
14531474

14541475

1455-
def issue_deprecation_warning(message: str, _version: str) -> None:
1476+
def issue_deprecation_warning(message: str, _version: str) -> None: # pragma: no cover
14561477
"""Issue a deprecation warning.
14571478
14581479
Arguments:
@@ -1477,18 +1498,10 @@ def validate_strict_and_pass_though(
14771498
pass_through: bool | None, # noqa: FBT001
14781499
*,
14791500
pass_through_default: bool,
1480-
emit_deprecation_warning: bool,
14811501
) -> bool:
14821502
if strict is None and pass_through is None:
14831503
pass_through = pass_through_default
14841504
elif strict is not None and pass_through is None:
1485-
if emit_deprecation_warning:
1486-
msg = (
1487-
"`strict` in `from_native` is deprecated, please use `pass_through` instead.\n\n"
1488-
"Note: `strict` will remain available in `narwhals.stable.v1`.\n"
1489-
"See https://narwhals-dev.github.io/narwhals/backcompat/ for more information.\n"
1490-
)
1491-
issue_deprecation_warning(msg, _version="1.13.0")
14921505
pass_through = not strict
14931506
elif strict is None and pass_through is not None:
14941507
pass

narwhals/dataframe.py

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
is_list_of,
3535
is_sequence_like,
3636
is_slice_none,
37-
issue_deprecation_warning,
3837
issue_performance_warning,
3938
supports_arrow_c_stream,
4039
)
@@ -2867,22 +2866,6 @@ def head(self, n: int = 5) -> Self:
28672866
"""
28682867
return super().head(n)
28692868

2870-
def tail(self, n: int = 5) -> Self: # pragma: no cover
2871-
r"""Get the last `n` rows.
2872-
2873-
Warning:
2874-
`LazyFrame.tail` is deprecated and will be removed in a future version.
2875-
Note: this will remain available in `narwhals.stable.v1`.
2876-
See [stable api](../backcompat.md/) for more information.
2877-
2878-
Arguments:
2879-
n: Number of rows to return.
2880-
2881-
Returns:
2882-
A subset of the LazyFrame of shape (n, n_columns).
2883-
"""
2884-
return super().tail(n)
2885-
28862869
def drop(self, *columns: str | Iterable[str], strict: bool = True) -> Self:
28872870
r"""Remove columns from the LazyFrame.
28882871
@@ -3359,30 +3342,6 @@ def lazy(self) -> Self:
33593342
"""
33603343
return self
33613344

3362-
def gather_every(self, n: int, offset: int = 0) -> Self:
3363-
r"""Take every nth row in the DataFrame and return as a new DataFrame.
3364-
3365-
Warning:
3366-
`LazyFrame.gather_every` is deprecated and will be removed in a future version.
3367-
Note: this will remain available in `narwhals.stable.v1`.
3368-
See [stable api](../backcompat.md/) for more information.
3369-
3370-
Arguments:
3371-
n: Gather every *n*-th row.
3372-
offset: Starting index.
3373-
3374-
Returns:
3375-
The LazyFrame containing only the selected rows.
3376-
"""
3377-
msg = (
3378-
"`LazyFrame.gather_every` is deprecated and will be removed in a future version.\n\n"
3379-
"Note: this will remain available in `narwhals.stable.v1`.\n"
3380-
"See https://narwhals-dev.github.io/narwhals/backcompat/ for more information.\n"
3381-
)
3382-
issue_deprecation_warning(msg, _version="1.29.0")
3383-
3384-
return super().gather_every(n=n, offset=offset)
3385-
33863345
def unpivot(
33873346
self,
33883347
on: str | list[str] | None = None,

0 commit comments

Comments
 (0)