Skip to content

Commit

Permalink
fix: consistent naming of positional arguments (#1310)
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoGorelli authored Nov 3, 2024
1 parent 98666e3 commit b9720b2
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 41 deletions.
19 changes: 3 additions & 16 deletions docs/backcompat.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ Here are exceptions to our backwards compatibility policy:
need to rethink Narwhals. However, we expect such radical changes to be exceedingly unlikely.
- we may consider making some type hints more precise.

In general, decision are driven by use-cases, and we conduct a search of public GitHub repositories
before making any change.

## Breaking changes carried out so far

### After `stable.v1`
Expand Down Expand Up @@ -120,19 +123,3 @@ Here are exceptions to our backwards compatibility policy:
assert nw.Datetime("us") == nw.Datetime
assert nw_v1.Datetime("us") == nw_v1.Datetime
```

- The first argument to `from_native` has been renamed from `native_dataframe` to `native_object`:

```python
# v1 syntax:
nw.from_native(native_dataframe=df) # people tend to write this
# main namespace syntax:
nw.from_native(native_object=df)
```

In practice, we recommend passing this argument positionally, and that will work consistently
across namespaces:
```python
# Recommended
nw.from_native(df)
```
46 changes: 23 additions & 23 deletions narwhals/stable/v1/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,7 @@ def _stableify(

@overload
def from_native(
native_dataframe: IntoDataFrameT | IntoSeriesT,
native_object: IntoDataFrameT | IntoSeriesT,
*,
strict: Literal[False],
eager_only: None = ...,
Expand All @@ -589,7 +589,7 @@ def from_native(

@overload
def from_native(
native_dataframe: IntoDataFrameT | IntoSeriesT,
native_object: IntoDataFrameT | IntoSeriesT,
*,
strict: Literal[False],
eager_only: Literal[True],
Expand All @@ -601,7 +601,7 @@ def from_native(

@overload
def from_native(
native_dataframe: IntoDataFrameT,
native_object: IntoDataFrameT,
*,
strict: Literal[False],
eager_only: None = ...,
Expand All @@ -613,7 +613,7 @@ def from_native(

@overload
def from_native(
native_dataframe: T,
native_object: T,
*,
strict: Literal[False],
eager_only: None = ...,
Expand All @@ -625,7 +625,7 @@ def from_native(

@overload
def from_native(
native_dataframe: IntoDataFrameT,
native_object: IntoDataFrameT,
*,
strict: Literal[False],
eager_only: Literal[True],
Expand All @@ -637,7 +637,7 @@ def from_native(

@overload
def from_native(
native_dataframe: T,
native_object: T,
*,
strict: Literal[False],
eager_only: Literal[True],
Expand All @@ -649,7 +649,7 @@ def from_native(

@overload
def from_native(
native_dataframe: IntoFrameT | IntoSeriesT,
native_object: IntoFrameT | IntoSeriesT,
*,
strict: Literal[False],
eager_only: None = ...,
Expand All @@ -661,7 +661,7 @@ def from_native(

@overload
def from_native(
native_dataframe: IntoSeriesT,
native_object: IntoSeriesT,
*,
strict: Literal[False],
eager_only: None = ...,
Expand All @@ -673,7 +673,7 @@ def from_native(

@overload
def from_native(
native_dataframe: IntoFrameT,
native_object: IntoFrameT,
*,
strict: Literal[False],
eager_only: None = ...,
Expand All @@ -685,7 +685,7 @@ def from_native(

@overload
def from_native(
native_dataframe: T,
native_object: T,
*,
strict: Literal[False],
eager_only: None = ...,
Expand All @@ -697,7 +697,7 @@ def from_native(

@overload
def from_native(
native_dataframe: IntoDataFrameT,
native_object: IntoDataFrameT,
*,
strict: Literal[True] = ...,
eager_only: None = ...,
Expand All @@ -713,7 +713,7 @@ def from_native(

@overload
def from_native(
native_dataframe: IntoDataFrameT,
native_object: IntoDataFrameT,
*,
strict: Literal[True] = ...,
eager_only: Literal[True],
Expand All @@ -729,7 +729,7 @@ def from_native(

@overload
def from_native(
native_dataframe: IntoFrameT | IntoSeriesT,
native_object: IntoFrameT | IntoSeriesT,
*,
strict: Literal[True] = ...,
eager_only: None = ...,
Expand All @@ -745,7 +745,7 @@ def from_native(

@overload
def from_native(
native_dataframe: IntoSeriesT | Any, # remain `Any` for downstream compatibility
native_object: IntoSeriesT | Any, # remain `Any` for downstream compatibility
*,
strict: Literal[True] = ...,
eager_only: None = ...,
Expand All @@ -761,7 +761,7 @@ def from_native(

@overload
def from_native(
native_dataframe: IntoFrameT,
native_object: IntoFrameT,
*,
strict: Literal[True] = ...,
eager_only: None = ...,
Expand All @@ -778,7 +778,7 @@ def from_native(
# All params passed in as variables
@overload
def from_native(
native_dataframe: Any,
native_object: Any,
*,
strict: bool,
eager_only: bool | None,
Expand All @@ -789,7 +789,7 @@ def from_native(


def from_native(
native_dataframe: Any,
native_object: Any,
*,
strict: bool = True,
eager_only: bool | None = None,
Expand All @@ -801,7 +801,7 @@ def from_native(
Convert dataframe/series to Narwhals DataFrame, LazyFrame, or Series.
Arguments:
native_dataframe: Raw object from user.
native_object: Raw object from user.
Depending on the other arguments, input object can be:
- pandas.DataFrame
Expand All @@ -825,12 +825,12 @@ def from_native(
from narwhals.stable.v1 import dtypes

# Early returns
if isinstance(native_dataframe, (DataFrame, LazyFrame)) and not series_only:
return native_dataframe
if isinstance(native_dataframe, Series) and (series_only or allow_series):
return native_dataframe
if isinstance(native_object, (DataFrame, LazyFrame)) and not series_only:
return native_object
if isinstance(native_object, Series) and (series_only or allow_series):
return native_object
result = _from_native_impl(
native_dataframe,
native_object,
strict=strict,
eager_only=eager_only,
eager_or_interchange_only=eager_or_interchange_only,
Expand Down
2 changes: 0 additions & 2 deletions tests/stable_api_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,6 @@ def test_stable_api_docstrings() -> None:
continue
v1_doc = getattr(nw_v1, item).__doc__
nw_doc = getattr(nw, item).__doc__
if item == "from_native":
v1_doc = v1_doc.replace("native_dataframe", "native_object")
assert v1_doc == nw_doc


Expand Down

0 comments on commit b9720b2

Please sign in to comment.