Skip to content

Commit c3a512c

Browse files
authored
chore(typing): Enable allow-redefinition for mypy (#3650)
1 parent 53b5da9 commit c3a512c

9 files changed

Lines changed: 21 additions & 17 deletions

File tree

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,7 @@ exclude_also = [
338338
files = ["src/narwhals", "tests", "test-plugin"]
339339
pretty = true
340340
strict = true
341+
allow_redefinition = true # https://mypy.readthedocs.io/en/stable/command_line.html#cmdoption-mypy-allow-redefinition
341342

342343
[[tool.mypy.overrides]]
343344
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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ def test_array_inner_recursive() -> None:
187187
for dim in shape:
188188
assert isinstance(dtype, nw.Array)
189189
assert dtype.size == dim
190-
dtype = dtype.inner # type: ignore[assignment]
190+
dtype = dtype.inner
191191

192192

193193
def test_array_inner_recursive_repr() -> None:

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)