Skip to content

Commit

Permalink
avoid while loops, shape docstring
Browse files Browse the repository at this point in the history
  • Loading branch information
FBruzzesi committed Feb 9, 2025
1 parent 981f87c commit 690fc99
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
12 changes: 7 additions & 5 deletions narwhals/_duckdb/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,11 +192,13 @@ def narwhals_to_native_dtype(dtype: DType | type[DType], version: Version) -> st
)
return f"STRUCT({inner})"
if isinstance_or_issubclass(dtype, dtypes.Array): # pragma: no cover
duckdb_shape_fmt = "".join(f"[{item}]" for item in dtype.shape) # type: ignore[union-attr]
while isinstance(dtype.inner, dtypes.Array): # type: ignore[union-attr]
dtype = dtype.inner # type: ignore[union-attr]
inner = narwhals_to_native_dtype(dtype.inner, version) # type: ignore[union-attr]
return f"{inner}{duckdb_shape_fmt}"
shape: tuple[int] = dtype.shape # type: ignore[union-attr]
duckdb_shape_fmt = "".join(f"[{item}]" for item in shape)
inner_dtype = dtype
for _ in shape:
inner_dtype = inner_dtype.inner # type: ignore[union-attr]
duckdb_inner = narwhals_to_native_dtype(inner_dtype, version)
return f"{duckdb_inner}{duckdb_shape_fmt}"
msg = f"Unknown dtype: {dtype}" # pragma: no cover
raise AssertionError(msg)

Expand Down
10 changes: 5 additions & 5 deletions narwhals/dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -765,7 +765,7 @@ class Array(NestedType):
Arguments:
inner: The datatype of the values within each array.
shape: the length of each array.
shape: The shape of the arrays.
Examples:
>>> import pandas as pd
Expand Down Expand Up @@ -840,12 +840,12 @@ def __hash__(self: Self) -> int:

def __repr__(self) -> str:
# Get leaf type
dtype = self.inner
while isinstance(dtype, Array):
dtype = dtype.inner
dtype_ = self
for _ in self.shape:
dtype_ = dtype_.inner # type: ignore[assignment]

class_name = self.__class__.__name__
return f"{class_name}({dtype!r}, shape={self.shape})"
return f"{class_name}({dtype_!r}, shape={self.shape})"


class Date(TemporalType):
Expand Down

0 comments on commit 690fc99

Please sign in to comment.