Skip to content

Commit 32ebac4

Browse files
committed
Merge remote-tracking branch 'upstream/main' into oh-nodes
2 parents c5719de + c3a512c commit 32ebac4

9 files changed

Lines changed: 39 additions & 18 deletions

File tree

pyproject.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -383,9 +383,8 @@ exclude_also = [
383383
files = ["src/narwhals", "tests", "test-plugin"]
384384
pretty = true
385385
strict = true
386+
allow_redefinition = true # https://mypy.readthedocs.io/en/stable/command_line.html#cmdoption-mypy-allow-redefinition
386387
check_untyped_defs = true
387-
allow_redefinition_new = true
388-
local_partial_types = true
389388

390389
[[tool.mypy.overrides]]
391390
module = [

src/narwhals/_arrow/series.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -600,13 +600,10 @@ def value_counts(
600600

601601
val_counts = pc.value_counts(self.native)
602602
values = val_counts.field("values")
603-
counts = cast("ChunkedArrayAny", val_counts.field("counts"))
604-
603+
counts = val_counts.field("counts")
605604
if normalize:
606-
arrays = [values, pc.divide(*cast_for_truediv(counts, pc.sum(counts)))]
607-
else:
608-
arrays = [values, counts]
609-
605+
counts = pc.divide(*cast_for_truediv(counts, pc.sum(counts)))
606+
arrays = values, counts
610607
val_count = pa.Table.from_arrays(arrays, names=[index_name_, value_name_])
611608

612609
if sort:

src/narwhals/_pandas_like/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ def rename(
195195
result = obj.rename(*args, **kwargs, copy=False, inplace=False)
196196
else:
197197
result = obj.rename(*args, **kwargs)
198-
return cast("NativeNDFrameT", result) # type: ignore[redundant-cast]
198+
return cast("NativeNDFrameT", result)
199199

200200

201201
@functools.lru_cache(maxsize=16)

src/narwhals/_polars/namespace.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ def concat_str(
186186
pl.when(~nm).then(sep).otherwise(pl.lit("")) for nm in null_mask[:-1]
187187
]
188188

189-
result = pl.fold( # type: ignore[assignment]
189+
result = pl.fold(
190190
acc=init_value,
191191
function=operator.add,
192192
exprs=[s + v for s, v in zip(separators, values, strict=True)],

src/narwhals/dtypes.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -970,12 +970,18 @@ def __hash__(self) -> int:
970970

971971
def __repr__(self) -> str:
972972
# Get leaf type
973-
dtype_ = self
973+
dtype: Self | IntoDType = self
974+
tp_self = type(self)
975+
# NOTE: This might look odd, but if we used a `while` loop
976+
# it won't trigger a `RecursionError` like `for` does.
977+
# https://github.com/narwhals-dev/narwhals/pull/3651
974978
for _ in self.shape:
975-
dtype_ = dtype_.inner # type: ignore[assignment]
976-
977-
class_name = self.__class__.__name__
978-
return f"{class_name}({dtype_!r}, shape={self.shape})"
979+
if isinstance(dtype, tp_self):
980+
dtype = dtype.inner
981+
else: # pragma: no cover
982+
msg = "Either something has gone horribly wrong in narwhals, or you've fiddled with the internals!"
983+
raise TypeError(msg)
984+
return f"{tp_self.__name__}({dtype!r}, shape={self.shape})"
979985

980986

981987
class Date(TemporalType):

tests/dtypes/dtypes_test.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,25 @@ def test_2d_array(constructor: Constructor, request: pytest.FixtureRequest) -> N
180180
assert df.collect_schema()["a"] == nw.Array(nw.Array(nw.Int64(), 2), 3)
181181

182182

183+
def test_array_inner_recursive() -> None:
184+
# https://github.com/pola-rs/polars/blob/210b34bee60db71ece147db060f570a41ba63c5d/py-polars/tests/unit/datatypes/test_array.py#L360-L365
185+
shape = (2, 3, 4, 5)
186+
dtype = nw.Array(nw.Int64, shape=shape)
187+
for dim in shape:
188+
assert isinstance(dtype, nw.Array)
189+
assert dtype.size == dim
190+
dtype = dtype.inner
191+
192+
193+
def test_array_inner_recursive_repr() -> None:
194+
dtype_1 = nw.Array(nw.Int64, 2)
195+
dtype_2 = nw.Array(dtype_1, 1)
196+
dtype_3 = nw.Array(dtype_2, 2)
197+
dtype_1.inner = dtype_3.inner
198+
with pytest.raises(RecursionError):
199+
repr(dtype_3)
200+
201+
183202
def test_second_time_unit() -> None:
184203
pytest.importorskip("pandas")
185204
pytest.importorskip("pyarrow")

tests/frame/group_by_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def test_group_by_iter(constructor_eager: ConstructorEager) -> None:
8181
assert isinstance(sub_df, nw.DataFrame)
8282
keys.append(key)
8383
assert sorted(keys) == sorted(expected_keys)
84-
expected_keys = [(1, 4), (3, 6)] # type: ignore[list-item]
84+
expected_keys = [(1, 4), (3, 6)]
8585
keys = []
8686
for key, _ in df.group_by("a", "b"):
8787
keys.append(key)

tests/frame/reindex_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def test_reindex(df_raw: Any) -> None:
2727
assert result_s[1]
2828
assert not result_s[2]
2929
result = df.with_columns(s.sort())
30-
expected = {"a": [1, 2, 3], "b": [4, 4, 6], "z": [7.0, 8.0, 9.0]} # type: ignore[list-item]
30+
expected = {"a": [1, 2, 3], "b": [4, 4, 6], "z": [7.0, 8.0, 9.0]}
3131
assert_equal_data(result, expected)
3232
with pytest.raises(MultiOutputExpressionError):
3333
nw.to_native(df.with_columns(nw.all() + nw.all()))

tests/series_only/value_counts_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def test_value_counts(
2929
expected_index = [4, 1, 6]
3030

3131
if normalize:
32-
expected_count = [v / len(data) for v in expected_count] # type: ignore[misc]
32+
expected_count = [v / len(data) for v in expected_count]
3333

3434
expected_name = name or ("proportion" if normalize else "count")
3535
expected = {"a": expected_index, expected_name: expected_count}

0 commit comments

Comments
 (0)