diff --git a/narwhals/_pandas_like/utils.py b/narwhals/_pandas_like/utils.py index 565fd92f2b..254eabddf6 100644 --- a/narwhals/_pandas_like/utils.py +++ b/narwhals/_pandas_like/utils.py @@ -572,7 +572,7 @@ def select_columns_by_name( # See https://github.com/narwhals-dev/narwhals/issues/1349#issuecomment-2470118122 # for why we need this if error := check_columns_exist( - column_names, # type: ignore[arg-type] + column_names, available=df.columns.tolist(), # type: ignore[attr-defined] ): raise error @@ -581,7 +581,7 @@ def select_columns_by_name( return df[column_names] # type: ignore[index] except KeyError as e: if error := check_columns_exist( - column_names, # type: ignore[arg-type] + column_names, available=df.columns.tolist(), # type: ignore[attr-defined] ): raise error from e diff --git a/narwhals/_utils.py b/narwhals/_utils.py index 7c4f4202eb..f3f3863559 100644 --- a/narwhals/_utils.py +++ b/narwhals/_utils.py @@ -2,7 +2,7 @@ import os import re -from collections.abc import Container, Iterable, Iterator, Mapping, Sequence +from collections.abc import Collection, Container, Iterable, Iterator, Mapping, Sequence from datetime import timezone from enum import Enum, auto from functools import lru_cache, wraps @@ -1181,7 +1181,7 @@ def is_ordered_categorical(series: Series[Any]) -> bool: def generate_unique_token( - n_bytes: int, columns: Sequence[str] + n_bytes: int, columns: Container[str] ) -> str: # pragma: no cover msg = ( "Use `generate_temporary_column_name` instead. `generate_unique_token` is " @@ -1191,7 +1191,7 @@ def generate_unique_token( return generate_temporary_column_name(n_bytes=n_bytes, columns=columns) -def generate_temporary_column_name(n_bytes: int, columns: Sequence[str]) -> str: +def generate_temporary_column_name(n_bytes: int, columns: Container[str]) -> str: """Generates a unique column name that is not present in the given list of columns. It relies on [python secrets token_hex](https://docs.python.org/3/library/secrets.html#secrets.token_hex) @@ -1489,7 +1489,7 @@ def generate_repr(header: str, native_repr: str) -> str: def check_columns_exist( - subset: Sequence[str], /, *, available: Sequence[str] + subset: Collection[str], /, *, available: Collection[str] ) -> ColumnNotFoundError | None: if missing := set(subset).difference(available): return ColumnNotFoundError.from_missing_and_available_column_names( @@ -1498,7 +1498,7 @@ def check_columns_exist( return None -def check_column_names_are_unique(columns: Sequence[str]) -> None: +def check_column_names_are_unique(columns: Collection[str]) -> None: len_unique_columns = len(set(columns)) if len(columns) != len_unique_columns: from collections import Counter @@ -1622,7 +1622,7 @@ def supports_arrow_c_stream(obj: Any) -> TypeIs[ArrowStreamExportable]: def _remap_full_join_keys( - left_on: Sequence[str], right_on: Sequence[str], suffix: str + left_on: Collection[str], right_on: Collection[str], suffix: str ) -> dict[str, str]: """Remap join keys to avoid collisions. diff --git a/narwhals/exceptions.py b/narwhals/exceptions.py index e9e7e3d26f..afb08fa8f3 100644 --- a/narwhals/exceptions.py +++ b/narwhals/exceptions.py @@ -3,7 +3,7 @@ from typing import TYPE_CHECKING if TYPE_CHECKING: - from collections.abc import Iterable, Sequence + from collections.abc import Collection, Iterable class NarwhalsError(ValueError): @@ -35,7 +35,7 @@ def __init__(self, message: str) -> None: @classmethod def from_missing_and_available_column_names( - cls, missing_columns: Iterable[str], available_columns: Sequence[str], / + cls, missing_columns: Iterable[str], available_columns: Collection[str], / ) -> ColumnNotFoundError: message = ( f"The following columns were not found: {sorted(missing_columns)}"