diff --git a/py-polars/docs/source/conf.py b/py-polars/docs/source/conf.py index 231da57375de..85a25283e51e 100644 --- a/py-polars/docs/source/conf.py +++ b/py-polars/docs/source/conf.py @@ -205,7 +205,7 @@ def linkcode_resolve(domain: str, info: dict[str, Any]) -> str | None: # Accessing deprecated objects will generate noisy warnings warnings.simplefilter("ignore", FutureWarning) obj = getattr(obj, part) - except AttributeError: + except AttributeError: # noqa: PERF203 return None try: diff --git a/py-polars/docs/source/reference/api.rst b/py-polars/docs/source/reference/api.rst index 8cd9dc77475b..20eb4e0fa5cb 100644 --- a/py-polars/docs/source/reference/api.rst +++ b/py-polars/docs/source/reference/api.rst @@ -46,7 +46,7 @@ Examples @pl.api.register_expr_namespace("greetings") class Greetings: - def __init__(self, expr: pl.Expr): + def __init__(self, expr: pl.Expr) -> None: self._expr = expr def hello(self) -> pl.Expr: @@ -80,7 +80,7 @@ Examples @pl.api.register_dataframe_namespace("split") class SplitFrame: - def __init__(self, df: pl.DataFrame): + def __init__(self, df: pl.DataFrame) -> None: self._df = df def by_alternate_rows(self) -> list[pl.DataFrame]: @@ -112,7 +112,7 @@ Examples @pl.api.register_lazyframe_namespace("types") class DTypeOperations: - def __init__(self, ldf: pl.LazyFrame): + def __init__(self, ldf: pl.LazyFrame) -> None: self._ldf = ldf def upcast_integer_types(self) -> pl.LazyFrame: @@ -145,7 +145,7 @@ Examples @pl.api.register_series_namespace("math") class MathShortcuts: - def __init__(self, s: pl.Series): + def __init__(self, s: pl.Series) -> None: self._s = s def square(self) -> pl.Series: diff --git a/py-polars/polars/__init__.py b/py-polars/polars/__init__.py index d7da45a84f75..10f0ee54228b 100644 --- a/py-polars/polars/__init__.py +++ b/py-polars/polars/__init__.py @@ -16,6 +16,8 @@ __register_startup_deps() +from typing import Any + from polars import api, exceptions, plugins, selectors from polars._utils.polars_version import get_polars_version as _get_polars_version @@ -380,7 +382,7 @@ ] -def __getattr__(name: str): # type: ignore[no-untyped-def] +def __getattr__(name: str) -> Any: # Deprecate re-export of exceptions at top-level if name in dir(exceptions): from polars._utils.deprecation import issue_deprecation_warning diff --git a/py-polars/polars/_utils/async_.py b/py-polars/polars/_utils/async_.py index 5669bdbb6790..a8fed8facda7 100644 --- a/py-polars/polars/_utils/async_.py +++ b/py-polars/polars/_utils/async_.py @@ -15,7 +15,7 @@ class _GeventDataFrameResult(Generic[T]): - __slots__ = ("_watcher", "_value", "_result") + __slots__ = ("_result", "_value", "_watcher") def __init__(self) -> None: if not _GEVENT_AVAILABLE: diff --git a/py-polars/polars/_utils/slice.py b/py-polars/polars/_utils/slice.py index d22bf3c3bdbf..21ba5cf8a121 100644 --- a/py-polars/polars/_utils/slice.py +++ b/py-polars/polars/_utils/slice.py @@ -24,7 +24,7 @@ class PolarsSlice: is_unbounded: bool obj: FrameOrSeries - def __init__(self, obj: FrameOrSeries): + def __init__(self, obj: FrameOrSeries) -> None: self.obj = obj @staticmethod @@ -116,7 +116,7 @@ class LazyPolarsSlice: obj: LazyFrame - def __init__(self, obj: LazyFrame): + def __init__(self, obj: LazyFrame) -> None: self.obj = obj def apply(self, s: slice) -> LazyFrame: diff --git a/py-polars/polars/_utils/udfs.py b/py-polars/polars/_utils/udfs.py index dca868fb9f9f..661c4e9cf303 100644 --- a/py-polars/polars/_utils/udfs.py +++ b/py-polars/polars/_utils/udfs.py @@ -330,7 +330,7 @@ class BytecodeParser: _map_target_name: str | None = None _caller_variables: dict[str, Any] | None = None - def __init__(self, function: Callable[[Any], Any], map_target: MapTarget): + def __init__(self, function: Callable[[Any], Any], map_target: MapTarget) -> None: """ Initialize BytecodeParser instance and prepare to introspect UDFs. @@ -745,7 +745,7 @@ def __init__( instructions: Iterator[Instruction], function: Callable[[Any], Any], caller_variables: dict[str, Any] | None, - ): + ) -> None: self._function = function self._caller_variables = caller_variables self._original_instructions = list(instructions) diff --git a/py-polars/polars/_utils/various.py b/py-polars/polars/_utils/various.py index f82bbec0d785..f25ab95e23c2 100644 --- a/py-polars/polars/_utils/various.py +++ b/py-polars/polars/_utils/various.py @@ -128,9 +128,8 @@ def is_sequence( val: object, *, include_series: bool = False ) -> TypeGuard[Sequence[Any]]: """Check whether the given input is a numpy array or python sequence.""" - return ( - (_check_for_numpy(val) and isinstance(val, np.ndarray)) - or isinstance(val, (pl.Series, Sequence) if include_series else Sequence) + return (_check_for_numpy(val) and isinstance(val, np.ndarray)) or ( + isinstance(val, (pl.Series, Sequence) if include_series else Sequence) and not isinstance(val, str) ) diff --git a/py-polars/polars/api.py b/py-polars/polars/api.py index 84262d1b7998..11a61b63e570 100644 --- a/py-polars/polars/api.py +++ b/py-polars/polars/api.py @@ -11,8 +11,8 @@ __all__ = [ - "register_expr_namespace", "register_dataframe_namespace", + "register_expr_namespace", "register_lazyframe_namespace", "register_series_namespace", ] @@ -84,7 +84,7 @@ def register_expr_namespace(name: str) -> Callable[[type[NS]], type[NS]]: -------- >>> @pl.api.register_expr_namespace("pow_n") ... class PowersOfN: - ... def __init__(self, expr: pl.Expr): + ... def __init__(self, expr: pl.Expr) -> None: ... self._expr = expr ... ... def next(self, p: int) -> pl.Expr: @@ -137,7 +137,7 @@ def register_dataframe_namespace(name: str) -> Callable[[type[NS]], type[NS]]: -------- >>> @pl.api.register_dataframe_namespace("split") ... class SplitFrame: - ... def __init__(self, df: pl.DataFrame): + ... def __init__(self, df: pl.DataFrame) -> None: ... self._df = df ... ... def by_first_letter_of_column_names(self) -> list[pl.DataFrame]: @@ -235,7 +235,7 @@ def register_lazyframe_namespace(name: str) -> Callable[[type[NS]], type[NS]]: -------- >>> @pl.api.register_lazyframe_namespace("types") ... class DTypeOperations: - ... def __init__(self, lf: pl.LazyFrame): + ... def __init__(self, lf: pl.LazyFrame) -> None: ... self._lf = lf ... ... def split_by_column_dtypes(self) -> list[pl.LazyFrame]: @@ -336,7 +336,7 @@ def register_series_namespace(name: str) -> Callable[[type[NS]], type[NS]]: -------- >>> @pl.api.register_series_namespace("math") ... class MathShortcuts: - ... def __init__(self, s: pl.Series): + ... def __init__(self, s: pl.Series) -> None: ... self._s = s ... ... def square(self) -> pl.Series: diff --git a/py-polars/polars/config.py b/py-polars/polars/config.py index e2f7f27292d2..0ad1dfb985f9 100644 --- a/py-polars/polars/config.py +++ b/py-polars/polars/config.py @@ -19,6 +19,12 @@ else: from typing_extensions import TypeAlias + if sys.version_info >= (3, 11): + from typing import Self + else: + from typing_extensions import Self + + __all__ = ["Config"] TableFormatNames: TypeAlias = Literal[ @@ -160,7 +166,7 @@ def __init__(self, *, restore_defaults: bool = False, **options: Any) -> None: raise AttributeError(msg) getattr(self, opt)(value) - def __enter__(self) -> Config: + def __enter__(self) -> Self: """Support setting temporary Config options that are reset on scope exit.""" self._original_state = self._original_state or self.save() return self diff --git a/py-polars/polars/dataframe/_html.py b/py-polars/polars/dataframe/_html.py index 64c0847f250e..62464873e42a 100644 --- a/py-polars/polars/dataframe/_html.py +++ b/py-polars/polars/dataframe/_html.py @@ -22,7 +22,7 @@ def __init__( elements: list[str], tag: str, attributes: dict[str, str] | None = None, - ): + ) -> None: self.tag = tag self.elements = elements self.attributes = attributes @@ -54,7 +54,7 @@ def __init__( max_cols: int = 75, max_rows: int = 40, from_series: bool = False, - ): + ) -> None: self.df = df self.elements: list[str] = [] self.max_cols = max_cols diff --git a/py-polars/polars/dataframe/frame.py b/py-polars/polars/dataframe/frame.py index c422f8bffdf8..d9faf3e7cde8 100644 --- a/py-polars/polars/dataframe/frame.py +++ b/py-polars/polars/dataframe/frame.py @@ -351,7 +351,7 @@ def __init__( orient: Orientation | None = None, infer_schema_length: int | None = N_INFER_DEFAULT, nan_to_null: bool = False, - ): + ) -> None: if data is None: self._df = dict_to_pydf( {}, schema=schema, schema_overrides=schema_overrides @@ -1106,10 +1106,10 @@ def __bool__(self) -> NoReturn: ) raise TypeError(msg) - def __eq__(self, other: Any) -> DataFrame: # type: ignore[override] + def __eq__(self, other: object) -> DataFrame: # type: ignore[override] return self._comp(other, "eq") - def __ne__(self, other: Any) -> DataFrame: # type: ignore[override] + def __ne__(self, other: object) -> DataFrame: # type: ignore[override] return self._comp(other, "neq") def __gt__(self, other: Any) -> DataFrame: @@ -3428,7 +3428,7 @@ def write_excel( hidden_columns = () hidden_columns = _expand_selectors(df, hidden_columns) if isinstance(column_widths, int): - column_widths = {column: column_widths for column in df.columns} + column_widths = dict.fromkeys(df.columns, column_widths) else: column_widths = _expand_selector_dicts( # type: ignore[assignment] df, column_widths, expand_keys=True, expand_values=False @@ -3740,10 +3740,8 @@ def write_parquet( if compression is None: compression = "uncompressed" if isinstance(file, (str, Path)): - if ( - partition_by is not None - or pyarrow_options is not None - and pyarrow_options.get("partition_cols") + if partition_by is not None or ( + pyarrow_options is not None and pyarrow_options.get("partition_cols") ): file = normalize_filepath(file, check_not_directory=False) else: @@ -5999,7 +5997,7 @@ def group_by( │ c ┆ 3 ┆ 1 │ └─────┴─────┴─────┘ """ - for _key, value in named_by.items(): + for value in named_by.values(): if not isinstance(value, (str, pl.Expr, pl.Series)): msg = ( f"Expected Polars expression or object convertible to one, got {type(value)}.\n\n" diff --git a/py-polars/polars/dataframe/group_by.py b/py-polars/polars/dataframe/group_by.py index f3a9c185de41..204afc3d25c1 100644 --- a/py-polars/polars/dataframe/group_by.py +++ b/py-polars/polars/dataframe/group_by.py @@ -35,7 +35,7 @@ def __init__( *by: IntoExpr | Iterable[IntoExpr], maintain_order: bool, **named_by: IntoExpr, - ): + ) -> None: """ Utility class for performing a group by operation over the given DataFrame. @@ -772,7 +772,7 @@ def __init__( offset: str | timedelta | None, closed: ClosedInterval, group_by: IntoExpr | Iterable[IntoExpr] | None, - ): + ) -> None: period = parse_as_duration_string(period) offset = parse_as_duration_string(offset) @@ -909,7 +909,7 @@ def __init__( label: Label, group_by: IntoExpr | Iterable[IntoExpr] | None, start_by: StartBy, - ): + ) -> None: every = parse_as_duration_string(every) period = parse_as_duration_string(period) offset = parse_as_duration_string(offset) diff --git a/py-polars/polars/datatypes/classes.py b/py-polars/polars/datatypes/classes.py index b815d7d17608..8906f2459c28 100644 --- a/py-polars/polars/datatypes/classes.py +++ b/py-polars/polars/datatypes/classes.py @@ -325,7 +325,7 @@ def __init__( self, precision: int | None = None, scale: int = 0, - ): + ) -> None: # Issuing the warning on `__init__` does not trigger when the class is used # without being instantiated, but it's better than nothing from polars._utils.unstable import issue_unstable_warning @@ -422,7 +422,7 @@ class Datetime(TemporalType): def __init__( self, time_unit: TimeUnit = "us", time_zone: str | timezone | None = None - ): + ) -> None: if time_unit not in ("ms", "us", "ns"): msg = ( "invalid `time_unit`" @@ -475,7 +475,7 @@ class Duration(TemporalType): time_unit: TimeUnit - def __init__(self, time_unit: TimeUnit = "us"): + def __init__(self, time_unit: TimeUnit = "us") -> None: if time_unit not in ("ms", "us", "ns"): msg = ( "invalid `time_unit`" @@ -518,7 +518,7 @@ class Categorical(DataType): def __init__( self, ordering: CategoricalOrdering | None = "physical", - ): + ) -> None: self.ordering = ordering def __repr__(self) -> str: @@ -554,7 +554,7 @@ class Enum(DataType): categories: Series - def __init__(self, categories: Series | Iterable[str]): + def __init__(self, categories: Series | Iterable[str]) -> None: # Issuing the warning on `__init__` does not trigger when the class is used # without being instantiated, but it's better than nothing from polars._utils.unstable import issue_unstable_warning @@ -654,7 +654,7 @@ class List(NestedType): inner: PolarsDataType - def __init__(self, inner: PolarsDataType | PythonDataType): + def __init__(self, inner: PolarsDataType | PythonDataType) -> None: self.inner = polars.datatypes.parse_into_dtype(inner) def __eq__(self, other: PolarsDataType) -> bool: # type: ignore[override] @@ -713,7 +713,7 @@ def __init__( shape: int | tuple[int, ...] | None = None, *, width: int | None = None, - ): + ) -> None: if width is not None: from polars._utils.deprecation import issue_deprecation_warning @@ -803,7 +803,7 @@ class Field: name: str dtype: PolarsDataType - def __init__(self, name: str, dtype: PolarsDataType): + def __init__(self, name: str, dtype: PolarsDataType) -> None: self.name = name self.dtype = polars.datatypes.parse_into_dtype(dtype) @@ -858,7 +858,7 @@ class Struct(NestedType): fields: list[Field] - def __init__(self, fields: Sequence[Field] | SchemaDict): + def __init__(self, fields: Sequence[Field] | SchemaDict) -> None: if isinstance(fields, Mapping): self.fields = [Field(name, dtype) for name, dtype in fields.items()] else: diff --git a/py-polars/polars/datatypes/convert.py b/py-polars/polars/datatypes/convert.py index 1b0806b2ea75..91fe61da4b6f 100644 --- a/py-polars/polars/datatypes/convert.py +++ b/py-polars/polars/datatypes/convert.py @@ -318,7 +318,7 @@ def numpy_char_code_to_dtype(dtype_char: str) -> PolarsDataType: return Binary try: return DataTypeMappings.NUMPY_KIND_AND_ITEMSIZE_TO_DTYPE[ - (dtype.kind, dtype.itemsize) + dtype.kind, dtype.itemsize ] except KeyError: # pragma: no cover msg = f"cannot parse numpy data type {dtype!r} into Polars data type" diff --git a/py-polars/polars/datatypes/group.py b/py-polars/polars/datatypes/group.py index 1cf67c7eba22..f30153ed6e7a 100644 --- a/py-polars/polars/datatypes/group.py +++ b/py-polars/polars/datatypes/group.py @@ -26,12 +26,19 @@ ) if TYPE_CHECKING: + import sys + from polars._typing import ( PolarsDataType, PolarsIntegerType, PolarsTemporalType, ) + if sys.version_info >= (3, 11): + from typing import Self + else: + from typing_extensions import Self + class DataTypeGroup(frozenset): # type: ignore[type-arg] """Group of data types.""" @@ -40,7 +47,7 @@ class DataTypeGroup(frozenset): # type: ignore[type-arg] def __new__( cls, items: Iterable[DataType | DataTypeClass], *, match_base_type: bool = True - ) -> DataTypeGroup: + ) -> Self: """ Construct a DataTypeGroup. diff --git a/py-polars/polars/expr/array.py b/py-polars/polars/expr/array.py index 98b8dfe80541..302b0b024adf 100644 --- a/py-polars/polars/expr/array.py +++ b/py-polars/polars/expr/array.py @@ -17,7 +17,7 @@ class ExprArrayNameSpace: _accessor = "arr" - def __init__(self, expr: Expr): + def __init__(self, expr: Expr) -> None: self._pyexpr = expr._pyexpr def min(self) -> Expr: diff --git a/py-polars/polars/expr/binary.py b/py-polars/polars/expr/binary.py index 7ea6dc4d79ea..62cf43180fd3 100644 --- a/py-polars/polars/expr/binary.py +++ b/py-polars/polars/expr/binary.py @@ -16,7 +16,7 @@ class ExprBinaryNameSpace: _accessor = "bin" - def __init__(self, expr: Expr): + def __init__(self, expr: Expr) -> None: self._pyexpr = expr._pyexpr def contains(self, literal: IntoExpr) -> Expr: diff --git a/py-polars/polars/expr/categorical.py b/py-polars/polars/expr/categorical.py index 2d6ce7646443..fed977c3644d 100644 --- a/py-polars/polars/expr/categorical.py +++ b/py-polars/polars/expr/categorical.py @@ -13,7 +13,7 @@ class ExprCatNameSpace: _accessor = "cat" - def __init__(self, expr: Expr): + def __init__(self, expr: Expr) -> None: self._pyexpr = expr._pyexpr def get_categories(self) -> Expr: diff --git a/py-polars/polars/expr/datetime.py b/py-polars/polars/expr/datetime.py index 9a03b46b12d3..a35674412ccf 100644 --- a/py-polars/polars/expr/datetime.py +++ b/py-polars/polars/expr/datetime.py @@ -30,7 +30,7 @@ class ExprDateTimeNameSpace: _accessor = "dt" - def __init__(self, expr: Expr): + def __init__(self, expr: Expr) -> None: self._pyexpr = expr._pyexpr def add_business_days( diff --git a/py-polars/polars/expr/expr.py b/py-polars/polars/expr/expr.py index d94b257b0f1b..bb2430671234 100644 --- a/py-polars/polars/expr/expr.py +++ b/py-polars/polars/expr/expr.py @@ -2540,10 +2540,8 @@ def gather( │ two ┆ [4, 99] │ └───────┴───────────┘ """ - if ( - isinstance(indices, Sequence) - and not isinstance(indices, str) - or (_check_for_numpy(indices) and isinstance(indices, np.ndarray)) + if (isinstance(indices, Sequence) and not isinstance(indices, str)) or ( + _check_for_numpy(indices) and isinstance(indices, np.ndarray) ): indices_lit = F.lit(pl.Series("", indices, dtype=Int64))._pyexpr else: @@ -4260,7 +4258,7 @@ def __init__( self, function: Callable[[Series], Series | Any], return_dtype: PolarsDataType | None, - ): + ) -> None: self.function = function self.return_dtype = return_dtype diff --git a/py-polars/polars/expr/list.py b/py-polars/polars/expr/list.py index 5655b58c86cb..5560f368767b 100644 --- a/py-polars/polars/expr/list.py +++ b/py-polars/polars/expr/list.py @@ -25,7 +25,7 @@ class ExprListNameSpace: _accessor = "list" - def __init__(self, expr: Expr): + def __init__(self, expr: Expr) -> None: self._pyexpr = expr._pyexpr def __getitem__(self, item: int) -> Expr: diff --git a/py-polars/polars/expr/meta.py b/py-polars/polars/expr/meta.py index 333b35351a2d..a3536c97711e 100644 --- a/py-polars/polars/expr/meta.py +++ b/py-polars/polars/expr/meta.py @@ -20,7 +20,7 @@ class ExprMetaNameSpace: _accessor = "meta" - def __init__(self, expr: Expr): + def __init__(self, expr: Expr) -> None: self._pyexpr = expr._pyexpr def __eq__(self, other: ExprMetaNameSpace | Expr) -> bool: # type: ignore[override] diff --git a/py-polars/polars/expr/name.py b/py-polars/polars/expr/name.py index 8b6fe24d8dea..0b53cd158cab 100644 --- a/py-polars/polars/expr/name.py +++ b/py-polars/polars/expr/name.py @@ -11,7 +11,7 @@ class ExprNameNameSpace: _accessor = "name" - def __init__(self, expr: Expr): + def __init__(self, expr: Expr) -> None: self._from_pyexpr = expr._from_pyexpr self._pyexpr = expr._pyexpr diff --git a/py-polars/polars/expr/string.py b/py-polars/polars/expr/string.py index 351c1cdd8565..1b04ab7febad 100644 --- a/py-polars/polars/expr/string.py +++ b/py-polars/polars/expr/string.py @@ -36,7 +36,7 @@ class ExprStringNameSpace: _accessor = "str" - def __init__(self, expr: Expr): + def __init__(self, expr: Expr) -> None: self._pyexpr = expr._pyexpr def to_date( diff --git a/py-polars/polars/expr/struct.py b/py-polars/polars/expr/struct.py index 225f82b67199..09b9c1688fbb 100644 --- a/py-polars/polars/expr/struct.py +++ b/py-polars/polars/expr/struct.py @@ -16,7 +16,7 @@ class ExprStructNameSpace: _accessor = "struct" - def __init__(self, expr: Expr): + def __init__(self, expr: Expr) -> None: self._pyexpr = expr._pyexpr def __getitem__(self, item: str | int) -> Expr: diff --git a/py-polars/polars/expr/whenthen.py b/py-polars/polars/expr/whenthen.py index 1467d9e73698..752ed1b44c06 100644 --- a/py-polars/polars/expr/whenthen.py +++ b/py-polars/polars/expr/whenthen.py @@ -24,7 +24,7 @@ class When: In this state, `then` must be called to continue to finish the expression. """ - def __init__(self, when: Any): + def __init__(self, when: Any) -> None: self._when = when def then(self, statement: IntoExpr) -> Then: @@ -49,7 +49,7 @@ class Then(Expr): Represents the state of the expression after `pl.when(...).then(...)` is called. """ - def __init__(self, then: Any): + def __init__(self, then: Any) -> None: self._then = then @classmethod @@ -108,7 +108,7 @@ class ChainedWhen: In this state, `then` must be called to continue to finish the expression. """ - def __init__(self, chained_when: Any): + def __init__(self, chained_when: Any) -> None: self._chained_when = chained_when def then(self, statement: IntoExpr) -> ChainedThen: @@ -133,7 +133,7 @@ class ChainedThen(Expr): Represents the state of the expression after an additional `then` is called. """ - def __init__(self, chained_then: Any): + def __init__(self, chained_then: Any) -> None: self._chained_then = chained_then @classmethod diff --git a/py-polars/polars/functions/aggregation/__init__.py b/py-polars/polars/functions/aggregation/__init__.py index 245b458dfdc7..9dcd4f9b268b 100644 --- a/py-polars/polars/functions/aggregation/__init__.py +++ b/py-polars/polars/functions/aggregation/__init__.py @@ -18,16 +18,16 @@ __all__ = [ "all", - "any", - "cum_sum", - "max", - "min", - "sum", "all_horizontal", + "any", "any_horizontal", + "cum_sum", "cum_sum_horizontal", + "max", "max_horizontal", "mean_horizontal", + "min", "min_horizontal", + "sum", "sum_horizontal", ] diff --git a/py-polars/polars/interchange/buffer.py b/py-polars/polars/interchange/buffer.py index cd4981901d4c..2f3598169777 100644 --- a/py-polars/polars/interchange/buffer.py +++ b/py-polars/polars/interchange/buffer.py @@ -29,7 +29,7 @@ class PolarsBuffer(Buffer): a RuntimeError will be raised if data would be copied. """ - def __init__(self, data: Series, *, allow_copy: bool = True): + def __init__(self, data: Series, *, allow_copy: bool = True) -> None: if data.n_chunks() > 1: if not allow_copy: msg = "non-contiguous buffer must be made contiguous" diff --git a/py-polars/polars/interchange/column.py b/py-polars/polars/interchange/column.py index 197a8b5da584..65bc9a19fd46 100644 --- a/py-polars/polars/interchange/column.py +++ b/py-polars/polars/interchange/column.py @@ -34,7 +34,7 @@ class PolarsColumn(Column): a RuntimeError will be raised if data would be copied. """ - def __init__(self, column: Series, *, allow_copy: bool = True): + def __init__(self, column: Series, *, allow_copy: bool = True) -> None: self._col = column self._allow_copy = allow_copy diff --git a/py-polars/polars/interchange/dataframe.py b/py-polars/polars/interchange/dataframe.py index 0fc6e5094e7d..c63af632c816 100644 --- a/py-polars/polars/interchange/dataframe.py +++ b/py-polars/polars/interchange/dataframe.py @@ -30,7 +30,7 @@ class PolarsDataFrame(InterchangeDataFrame): version = 0 - def __init__(self, df: DataFrame, *, allow_copy: bool = True): + def __init__(self, df: DataFrame, *, allow_copy: bool = True) -> None: self._df = df self._allow_copy = allow_copy diff --git a/py-polars/polars/io/csv/batched_reader.py b/py-polars/polars/io/csv/batched_reader.py index f13efb7aa3b3..2bbb1583317c 100644 --- a/py-polars/polars/io/csv/batched_reader.py +++ b/py-polars/polars/io/csv/batched_reader.py @@ -56,7 +56,7 @@ def __init__( raise_if_empty: bool = True, truncate_ragged_lines: bool = False, decimal_comma: bool = False, - ): + ) -> None: path = normalize_filepath(source, check_not_directory=False) dtype_list: Sequence[tuple[str, PolarsDataType]] | None = None diff --git a/py-polars/polars/io/database/_executor.py b/py-polars/polars/io/database/_executor.py index bb56d48c6b2f..b45301b87da2 100644 --- a/py-polars/polars/io/database/_executor.py +++ b/py-polars/polars/io/database/_executor.py @@ -22,6 +22,7 @@ if TYPE_CHECKING: import sys + from collections.abc import Iterator from types import TracebackType import pyarrow as pa @@ -70,7 +71,7 @@ def __init__(self, frames: Any, *, cursor: Cursor) -> None: self._iter_frames = frames self._cursor = cursor - def __iter__(self) -> Iterable[DataFrame]: + def __iter__(self) -> Iterator[DataFrame]: yield from self._iter_frames if hasattr(self._cursor, "close"): @@ -546,7 +547,7 @@ def to_polars( if defer_cursor_close: frame = ( df - for df in CloseAfterFrameIter( # type: ignore[attr-defined] + for df in CloseAfterFrameIter( frame, cursor=self.result, ) diff --git a/py-polars/polars/io/delta.py b/py-polars/polars/io/delta.py index 66e60b723abb..9cb621fc5534 100644 --- a/py-polars/polars/io/delta.py +++ b/py-polars/polars/io/delta.py @@ -337,7 +337,7 @@ def _get_delta_lake_table( def _check_if_delta_available() -> None: if not _DELTALAKE_AVAILABLE: - msg = "deltalake is not installed" "\n\nPlease run: pip install deltalake" + msg = "deltalake is not installed\n\nPlease run: pip install deltalake" raise ModuleNotFoundError(msg) diff --git a/py-polars/polars/io/ipc/__init__.py b/py-polars/polars/io/ipc/__init__.py index 9423bbceb829..98f850931501 100644 --- a/py-polars/polars/io/ipc/__init__.py +++ b/py-polars/polars/io/ipc/__init__.py @@ -2,7 +2,7 @@ __all__ = [ "read_ipc", - "read_ipc_stream", "read_ipc_schema", + "read_ipc_stream", "scan_ipc", ] diff --git a/py-polars/polars/io/ndjson.py b/py-polars/polars/io/ndjson.py index cd9ea92bf3c0..33e85914ce34 100644 --- a/py-polars/polars/io/ndjson.py +++ b/py-polars/polars/io/ndjson.py @@ -122,9 +122,11 @@ def read_ndjson( """ if not ( isinstance(source, (str, Path)) - or isinstance(source, Sequence) - and source - and isinstance(source[0], (str, Path)) + or ( + isinstance(source, Sequence) + and source + and isinstance(source[0], (str, Path)) + ) ): # TODO: A lot of the parameters aren't applied for BytesIO if isinstance(source, StringIO): @@ -174,8 +176,7 @@ def scan_ndjson( | list[str] | list[Path] | list[IO[str]] - | list[IO[bytes]] - | bytes, + | list[IO[bytes]], *, schema: SchemaDefinition | None = None, schema_overrides: SchemaDefinition | None = None, diff --git a/py-polars/polars/io/spreadsheet/_write_utils.py b/py-polars/polars/io/spreadsheet/_write_utils.py index db947e6ef17d..ba4032f5293f 100644 --- a/py-polars/polars/io/spreadsheet/_write_utils.py +++ b/py-polars/polars/io/spreadsheet/_write_utils.py @@ -55,7 +55,7 @@ def _cluster(iterable: Iterable[Any], n: int = 2) -> Iterable[Any]: class _XLFormatCache: """Create/cache only one Format object per distinct set of format options.""" - def __init__(self, wb: Workbook): + def __init__(self, wb: Workbook) -> None: self._cache: dict[str, Format] = {} self.wb = wb @@ -412,10 +412,10 @@ def _map_str(s: Series) -> Series: # expand/normalise column totals if column_totals is True: - column_totals = {numeric(): "sum", **{t: "sum" for t in row_totals or ()}} + column_totals = {numeric(): "sum", **dict.fromkeys(row_totals or (), "sum")} elif isinstance(column_totals, str): fn = column_totals.lower() - column_totals = {numeric(): fn, **{t: fn for t in row_totals or ()}} + column_totals = {numeric(): fn, **dict.fromkeys(row_totals or (), fn)} column_totals = _unpack_multi_column_dict( # type: ignore[assignment] _expand_selector_dicts(df, column_totals, expand_keys=True, expand_values=False) @@ -423,7 +423,7 @@ def _map_str(s: Series) -> Series: else _expand_selectors(df, column_totals) ) column_total_funcs = ( - {col: "sum" for col in column_totals} + dict.fromkeys(column_totals, "sum") if isinstance(column_totals, Sequence) else (column_totals.copy() if isinstance(column_totals, dict) else {}) ) diff --git a/py-polars/polars/io/spreadsheet/functions.py b/py-polars/polars/io/spreadsheet/functions.py index 1fd25cc1417a..dd445f9e9c97 100644 --- a/py-polars/polars/io/spreadsheet/functions.py +++ b/py-polars/polars/io/spreadsheet/functions.py @@ -927,7 +927,7 @@ def _read_spreadsheet_calamine( schema_overrides = schema_overrides or {} if read_options.get("schema_sample_rows") == 0: # ref: https://github.com/ToucanToco/fastexcel/issues/236 - read_options["dtypes"] = {idx: "string" for idx in range(16384)} + read_options["dtypes"] = dict.fromkeys(range(16384), "string") elif schema_overrides and fastexcel_version >= (0, 10): parser_dtypes = read_options.get("dtypes", {}) for name, dtype in schema_overrides.items(): diff --git a/py-polars/polars/lazyframe/frame.py b/py-polars/polars/lazyframe/frame.py index 0eb14cba9e88..f1db322e4460 100644 --- a/py-polars/polars/lazyframe/frame.py +++ b/py-polars/polars/lazyframe/frame.py @@ -302,7 +302,7 @@ def __init__( orient: Orientation | None = None, infer_schema_length: int | None = N_INFER_DEFAULT, nan_to_null: bool = False, - ): + ) -> None: from polars.dataframe import DataFrame self._ldf = ( @@ -585,10 +585,10 @@ def _comparison_error(self, operator: str) -> NoReturn: msg = f'"{operator!r}" comparison not supported for LazyFrame objects' raise TypeError(msg) - def __eq__(self, other: Any) -> NoReturn: + def __eq__(self, other: object) -> NoReturn: self._comparison_error("==") - def __ne__(self, other: Any) -> NoReturn: + def __ne__(self, other: object) -> NoReturn: self._comparison_error("!=") def __gt__(self, other: Any) -> NoReturn: @@ -1733,7 +1733,7 @@ def profile( ) import matplotlib.pyplot as plt - fig, ax = plt.subplots(1, figsize=figsize) + _fig, ax = plt.subplots(1, figsize=figsize) max_val = timings["end"][-1] timings_ = timings.reverse() @@ -2970,7 +2970,7 @@ def cast( cast_map.update( {c: dtype} if isinstance(c, str) - else {x: dtype for x in expand_selector(self, c)} # type: ignore[arg-type] + else dict.fromkeys(expand_selector(self, c), dtype) # type: ignore[arg-type] ) return self._from_pyldf(self._ldf.cast(cast_map, strict)) @@ -3498,7 +3498,7 @@ def group_by( │ c ┆ 1 ┆ 1.0 │ └─────┴─────┴─────┘ """ - for _key, value in named_by.items(): + for value in named_by.values(): if not isinstance(value, (str, pl.Expr, pl.Series)): msg = ( f"Expected Polars expression or object convertible to one, got {type(value)}.\n\n" diff --git a/py-polars/polars/ml/torch.py b/py-polars/polars/ml/torch.py index c8c80c0c9581..134e1c260e63 100644 --- a/py-polars/polars/ml/torch.py +++ b/py-polars/polars/ml/torch.py @@ -121,7 +121,7 @@ def __init__( *, label: str | Expr | Sequence[str | Expr] | None = None, features: str | Expr | Sequence[str | Expr] | None = None, - ): + ) -> None: issue_unstable_warning("`PolarsDataset` is considered unstable.") if isinstance(label, (str, Expr)): label = [label] diff --git a/py-polars/polars/schema.py b/py-polars/polars/schema.py index 019d2d2f3ad0..a099b67aac2c 100644 --- a/py-polars/polars/schema.py +++ b/py-polars/polars/schema.py @@ -58,7 +58,7 @@ def __init__( | Iterable[tuple[str, DataType | PythonDataType]] | None ) = None, - ): + ) -> None: input = ( schema.items() if schema and isinstance(schema, Mapping) else (schema or {}) ) diff --git a/py-polars/polars/selectors.py b/py-polars/polars/selectors.py index 2e56aa3fb91e..36b26068a77f 100644 --- a/py-polars/polars/selectors.py +++ b/py-polars/polars/selectors.py @@ -248,7 +248,7 @@ def _expand_selector_dicts( if tuple_keys: expanded[cols] = value else: - expanded.update({c: value for c in cols}) + expanded.update(dict.fromkeys(cols, value)) else: expanded[key] = value return expanded @@ -319,7 +319,7 @@ def __init__( expr: Expr, name: str, parameters: dict[str, Any] | None = None, - ): + ) -> None: self._pyexpr = expr._pyexpr self._attrs = { "params": parameters, diff --git a/py-polars/polars/series/array.py b/py-polars/polars/series/array.py index 75910ad9446c..3051228ca941 100644 --- a/py-polars/polars/series/array.py +++ b/py-polars/polars/series/array.py @@ -20,7 +20,7 @@ class ArrayNameSpace: _accessor = "arr" - def __init__(self, series: Series): + def __init__(self, series: Series) -> None: self._s: PySeries = series._s def min(self) -> Series: diff --git a/py-polars/polars/series/binary.py b/py-polars/polars/series/binary.py index ded8ed5a0c8a..a1cdf5d8fb80 100644 --- a/py-polars/polars/series/binary.py +++ b/py-polars/polars/series/binary.py @@ -16,7 +16,7 @@ class BinaryNameSpace: _accessor = "bin" - def __init__(self, series: Series): + def __init__(self, series: Series) -> None: self._s: PySeries = series._s def contains(self, literal: IntoExpr) -> Series: diff --git a/py-polars/polars/series/categorical.py b/py-polars/polars/series/categorical.py index 82641bd41c83..04620b5ada9b 100644 --- a/py-polars/polars/series/categorical.py +++ b/py-polars/polars/series/categorical.py @@ -17,7 +17,7 @@ class CatNameSpace: _accessor = "cat" - def __init__(self, series: Series): + def __init__(self, series: Series) -> None: self._s: PySeries = series._s def get_categories(self) -> Series: diff --git a/py-polars/polars/series/datetime.py b/py-polars/polars/series/datetime.py index 928c81410a62..e4da43de78c7 100644 --- a/py-polars/polars/series/datetime.py +++ b/py-polars/polars/series/datetime.py @@ -30,7 +30,7 @@ class DateTimeNameSpace: _accessor = "dt" - def __init__(self, series: Series): + def __init__(self, series: Series) -> None: self._s: PySeries = series._s def __getitem__(self, item: int) -> dt.date | dt.datetime | dt.timedelta: diff --git a/py-polars/polars/series/list.py b/py-polars/polars/series/list.py index 53bcb22ce6f3..c6480e3ddbff 100644 --- a/py-polars/polars/series/list.py +++ b/py-polars/polars/series/list.py @@ -25,7 +25,7 @@ class ListNameSpace: _accessor = "list" - def __init__(self, series: Series): + def __init__(self, series: Series) -> None: self._s: PySeries = series._s def all(self) -> Series: diff --git a/py-polars/polars/series/series.py b/py-polars/polars/series/series.py index e2a4cb936e47..532cd4c74bc1 100644 --- a/py-polars/polars/series/series.py +++ b/py-polars/polars/series/series.py @@ -267,7 +267,7 @@ def __init__( *, strict: bool = True, nan_to_null: bool = False, - ): + ) -> None: # If 'Unknown' treat as None to trigger type inference if dtype == Unknown: dtype = None @@ -761,24 +761,24 @@ def _comp(self, other: Any, op: ComparisonOperator) -> Series: return self._from_pyseries(f(other)) @overload # type: ignore[override] - def __eq__(self, other: Expr) -> Expr: ... + def __eq__(self, other: Expr) -> Expr: ... # type: ignore[overload-overlap] @overload - def __eq__(self, other: Any) -> Series: ... + def __eq__(self, other: object) -> Series: ... - def __eq__(self, other: Any) -> Series | Expr: + def __eq__(self, other: object) -> Series | Expr: warn_null_comparison(other) if isinstance(other, pl.Expr): return F.lit(self).__eq__(other) return self._comp(other, "eq") @overload # type: ignore[override] - def __ne__(self, other: Expr) -> Expr: ... + def __ne__(self, other: Expr) -> Expr: ... # type: ignore[overload-overlap] @overload - def __ne__(self, other: Any) -> Series: ... + def __ne__(self, other: object) -> Series: ... - def __ne__(self, other: Any) -> Series | Expr: + def __ne__(self, other: object) -> Series | Expr: warn_null_comparison(other) if isinstance(other, pl.Expr): return F.lit(self).__ne__(other) @@ -3974,7 +3974,7 @@ def equals( def cast( self, - dtype: PolarsDataType | type[int] | type[float] | type[str] | type[bool], + dtype: type[int | float | str | bool] | PolarsDataType, *, strict: bool = True, wrap_numerical: bool = False, diff --git a/py-polars/polars/series/string.py b/py-polars/polars/series/string.py index 953803fc1253..e2104f67af44 100644 --- a/py-polars/polars/series/string.py +++ b/py-polars/polars/series/string.py @@ -29,7 +29,7 @@ class StringNameSpace: _accessor = "str" - def __init__(self, series: Series): + def __init__(self, series: Series) -> None: self._s: PySeries = series._s def to_date( diff --git a/py-polars/polars/series/struct.py b/py-polars/polars/series/struct.py index 160558a207b8..750ccfd51275 100644 --- a/py-polars/polars/series/struct.py +++ b/py-polars/polars/series/struct.py @@ -20,7 +20,7 @@ class StructNameSpace: _accessor = "struct" - def __init__(self, series: Series): + def __init__(self, series: Series) -> None: self._s: PySeries = series._s def __getitem__(self, item: int | str) -> Series: diff --git a/py-polars/polars/string_cache.py b/py-polars/polars/string_cache.py index f2bacc72b39b..e4768de6217d 100644 --- a/py-polars/polars/string_cache.py +++ b/py-polars/polars/string_cache.py @@ -8,8 +8,15 @@ from polars.polars import PyStringCacheHolder if TYPE_CHECKING: + import sys from types import TracebackType + if sys.version_info >= (3, 11): + from typing import Self + else: + from typing_extensions import Self + + __all__ = [ "StringCache", "disable_string_cache", @@ -68,7 +75,7 @@ class StringCache(contextlib.ContextDecorator): ... return pl.concat([s1, s2]) """ - def __enter__(self) -> StringCache: + def __enter__(self) -> Self: self._string_cache = PyStringCacheHolder() return self diff --git a/py-polars/polars/testing/parametric/strategies/core.py b/py-polars/polars/testing/parametric/strategies/core.py index 659a6f2433b7..b3aef2e51301 100644 --- a/py-polars/polars/testing/parametric/strategies/core.py +++ b/py-polars/polars/testing/parametric/strategies/core.py @@ -29,7 +29,7 @@ @st.composite -def series( # noqa: D417 +def series( draw: DrawFn, /, *, @@ -267,7 +267,7 @@ def dataframes( @st.composite -def dataframes( # noqa: D417 +def dataframes( draw: DrawFn, /, cols: int | column | Sequence[column] | None = None, diff --git a/py-polars/pyproject.toml b/py-polars/pyproject.toml index efa5bd1f15f9..86e5660dc8a3 100644 --- a/py-polars/pyproject.toml +++ b/py-polars/pyproject.toml @@ -151,59 +151,60 @@ fix = true [tool.ruff.lint] select = [ - "E", # pycodestyle - "W", # pycodestyle - "F", # Pyflakes + "ANN", # flake8-annotations "B", # flake8-bugbear "C4", # flake8-comprehensions "D", # flake8-docstrings - "D213", # Augment NumPy docstring convention: Multi-line docstring summary should start at the second line - "D417", # Augment NumPy docstring convention: Missing argument descriptions + "E", # pycodestyle + "EM", # flake8-errmsg + "F", # pyflakes + "FA", # flake8-future-annotations + "FBT001", # flake8-boolean-trap "I", # isort - "SIM", # flake8-simplify - "TCH", # flake8-type-checking - "TID", # flake8-tidy-imports - "UP", # pyupgrade + "ICN", # flake8-import-conventions + "INT", # flake8-gettext + "PERF", # perflint + "PIE", # flake8-pie "PT", # flake8-pytest-style - "RUF", # Ruff-specific rules "PTH", # flake8-use-pathlib - "FA", # flake8-future-annotations - "PIE", # flake8-pie + "PYI", # flake8-pyi + "RUF", # ruff-specific rules + "SIM", # flake8-simplify + "TCH", # flake8-type-checking "TD", # flake8-todos + "TID", # flake8-tidy-imports "TRY", # tryceratops - "EM", # flake8-errmsg - "FBT001", # flake8-boolean-trap + "UP", # pyupgrade + "W", # pycodestyle ] ignore = [ - # Line length regulated by formatter - "E501", - # pydocstyle: http://www.pydocstyle.org/en/stable/error_codes.html - "D401", # Relax NumPy docstring convention: First line should be in imperative mood - # flake8-pytest-style: - "PT011", # pytest.raises({exception}) is too broad, set the match parameter or use a more specific exception - # flake8-simplify - "SIM102", # Use a single `if` statement instead of nested `if` statements - "SIM108", # Use ternary operator - # ruff - "RUF005", # unpack-instead-of-concatenating-to-collection-literal - # pycodestyle + # ------------------------------------------------------------------ # TODO: Remove errors below to further improve docstring linting - # Ordered from most common to least common errors. - "D105", # Missing docstring in magic method + # ------------------------------------------------------------------ "D100", # Missing docstring in public module "D104", # Missing docstring in public package - # flake8-todos + "D105", # Missing docstring in magic method + # ------------------------------------------------------------------ + "ANN101", # Missing type annotation for `self` in method + "ANN102", # Missing type annotation for `cls` in classmethod + "ANN401", # Dynamically typed expressions (Any) are disallowed + "D401", # Relax NumPy docstring convention: first line should be imperative + "E501", # Line length regulated by formatter + "PT011", # pytest.raises is too broad, set match or use a more specific exception + "PYI041", # Use float instead of int | float + "RUF022", # `__all__` is not sorted + "RUF005", # Consider expression instead of concatenation + "SIM102", # Use a single `if` statement instead of nested `if` statements + "SIM108", # Use ternary operator "TD002", # Missing author in TODO "TD003", # Missing issue link on the line following this TODO - # tryceratops "TRY003", # Avoid specifying long messages outside the exception class - # Lints below are turned off because of conflicts with the ruff formatter - "D206", - "W191", ] +allowed-confusables = ["µ"] [tool.ruff.lint.per-file-ignores] +"dependencies.py" = ["ICN001"] "tests/**/*.py" = ["D100", "D102", "D103", "B018", "FBT001"] [tool.ruff.lint.pycodestyle] diff --git a/py-polars/requirements-lint.txt b/py-polars/requirements-lint.txt index 28c5e645e0d3..1490335f9266 100644 --- a/py-polars/requirements-lint.txt +++ b/py-polars/requirements-lint.txt @@ -1,3 +1,3 @@ mypy==1.11.1 -ruff==0.6.3 +ruff==0.6.4 typos==1.24.2 diff --git a/py-polars/tests/benchmark/data/__init__.py b/py-polars/tests/benchmark/data/__init__.py index 255752458b72..84b7ef3683ce 100644 --- a/py-polars/tests/benchmark/data/__init__.py +++ b/py-polars/tests/benchmark/data/__init__.py @@ -3,4 +3,7 @@ from tests.benchmark.data.h2oai import generate_group_by_data from tests.benchmark.data.pdsh import load_pdsh_table -__all__ = ["load_pdsh_table", "generate_group_by_data"] +__all__ = [ + "generate_group_by_data", + "load_pdsh_table", +] diff --git a/py-polars/tests/docs/run_doctest.py b/py-polars/tests/docs/run_doctest.py index 09ad6b41c14d..353b9cfb0dd0 100644 --- a/py-polars/tests/docs/run_doctest.py +++ b/py-polars/tests/docs/run_doctest.py @@ -39,7 +39,7 @@ from tempfile import TemporaryDirectory from typing import TYPE_CHECKING, Any, Iterator -import polars +import polars as pl if TYPE_CHECKING: from types import ModuleType @@ -67,15 +67,15 @@ for mod, methods in OPTIONAL_MODULES_AND_METHODS.items(): try: importlib.import_module(mod) - except ImportError: + except ImportError: # noqa: PERF203 SKIP_METHODS.update(methods) OPTIONAL_MODULES.add(mod) def doctest_teardown(d: doctest.DocTest) -> None: # don't let config changes or string cache state leak between tests - polars.Config.restore_defaults() - polars.disable_string_cache() + pl.Config.restore_defaults() + pl.disable_string_cache() def modules_in_path(p: Path) -> Iterator[ModuleType]: @@ -86,7 +86,7 @@ def modules_in_path(p: Path) -> Iterator[ModuleType]: file_name_import = ".".join(file.relative_to(p).parts)[:-3] temp_module = importlib.import_module(p.name + "." + file_name_import) yield temp_module - except ImportError as err: + except ImportError as err: # noqa: PERF203 if not any(re.search(rf"\b{mod}\b", str(err)) for mod in OPTIONAL_MODULES): raise @@ -152,14 +152,14 @@ def check_output(self, want: str, got: str, optionflags: Any) -> bool: # doctest.REPORT_NDIFF = True # __file__ returns the __init__.py, so grab the parent - src_dir = Path(polars.__file__).parent + src_dir = Path(pl.__file__).parent with TemporaryDirectory() as tmpdir: # collect all tests tests = [ doctest.DocTestSuite( m, - extraglobs={"pl": polars, "dirpath": Path(tmpdir)}, + extraglobs={"pl": pl, "dirpath": Path(tmpdir)}, tearDown=doctest_teardown, optionflags=1, ) @@ -168,7 +168,7 @@ def check_output(self, want: str, got: str, optionflags: Any) -> bool: test_suite = FilteredTestSuite(tests) # Ensure that we clean up any artifacts produced by the doctests - # with patch(polars.DataFrame.write_csv): + # with patch(pl.DataFrame.write_csv): # run doctests and report result = unittest.TextTestRunner().run(test_suite) success_flag = (result.testsRun > 0) & (len(result.failures) == 0) diff --git a/py-polars/tests/docs/test_user_guide.py b/py-polars/tests/docs/test_user_guide.py index 89203b4d09eb..df3fd52494f8 100644 --- a/py-polars/tests/docs/test_user_guide.py +++ b/py-polars/tests/docs/test_user_guide.py @@ -5,11 +5,11 @@ from pathlib import Path from typing import Iterator -import matplotlib +import matplotlib as mpl import pytest # Do not show plots -matplotlib.use("Agg") +mpl.use("Agg") # Get paths to Python code snippets repo_root = Path(__file__).parent.parent.parent.parent diff --git a/py-polars/tests/unit/constructors/test_constructors.py b/py-polars/tests/unit/constructors/test_constructors.py index fda072930525..dbfe77b46c97 100644 --- a/py-polars/tests/unit/constructors/test_constructors.py +++ b/py-polars/tests/unit/constructors/test_constructors.py @@ -458,9 +458,15 @@ class ABC: assert dataclasses.asdict(abc) == df.rows(named=True)[0] -def test_collections_namedtuple() -> None: - TestData = namedtuple("TestData", ["id", "info"]) - nt_data = [TestData(1, "a"), TestData(2, "b"), TestData(3, "c")] +@pytest.mark.parametrize( + "nt", + [ + namedtuple("TestData", ["id", "info"]), # noqa: PYI024 + NamedTuple("TestData", [("id", int), ("info", str)]), + ], +) +def test_collections_namedtuple(nt: type) -> None: + nt_data = [nt(1, "a"), nt(2, "b"), nt(3, "c")] result = pl.DataFrame(nt_data) expected = pl.DataFrame({"id": [1, 2, 3], "info": ["a", "b", "c"]}) diff --git a/py-polars/tests/unit/constructors/test_dataframe.py b/py-polars/tests/unit/constructors/test_dataframe.py index b193c99f5011..3703475aa438 100644 --- a/py-polars/tests/unit/constructors/test_dataframe.py +++ b/py-polars/tests/unit/constructors/test_dataframe.py @@ -37,9 +37,9 @@ class Foo: def __init__(self, value: int) -> None: self._value = value - def __eq__(self, other: Any) -> bool: + def __eq__(self, other: object) -> bool: return issubclass(other.__class__, self.__class__) and ( - self._value == other._value + self._value == other._value # type: ignore[attr-defined] ) def __repr__(self) -> str: diff --git a/py-polars/tests/unit/dataframe/test_df.py b/py-polars/tests/unit/dataframe/test_df.py index ab27d2647395..71b012945876 100644 --- a/py-polars/tests/unit/dataframe/test_df.py +++ b/py-polars/tests/unit/dataframe/test_df.py @@ -1189,7 +1189,7 @@ def gen(n: int, *, strkey: bool = True) -> Iterator[Any]: # iterable object class Rows: - def __init__(self, n: int, *, strkey: bool = True): + def __init__(self, n: int, *, strkey: bool = True) -> None: self._n = n self._strkey = strkey @@ -1586,7 +1586,7 @@ class Foo: def __hash__(self) -> int: return 0 - def __eq__(self, other: Any) -> bool: + def __eq__(self, other: object) -> bool: return True df = df.with_columns(pl.col("a").map_elements(lambda x: Foo()).alias("foo")) @@ -2514,7 +2514,7 @@ def test_set() -> None: # needs to be a 2 element tuple with pytest.raises(ValueError): - df[(1, 2, 3)] = 1 + df[1, 2, 3] = 1 # we cannot index with any type, such as bool with pytest.raises(TypeError): diff --git a/py-polars/tests/unit/dataframe/test_partition_by.py b/py-polars/tests/unit/dataframe/test_partition_by.py index c78a7043d4b7..5a0f145d7805 100644 --- a/py-polars/tests/unit/dataframe/test_partition_by.py +++ b/py-polars/tests/unit/dataframe/test_partition_by.py @@ -53,11 +53,11 @@ def test_partition_by_single(df: pl.DataFrame) -> None: def test_partition_by_as_dict() -> None: df = pl.DataFrame({"a": ["one", "two", "one", "two"], "b": [1, 2, 3, 4]}) result = df.partition_by(cs.all(), as_dict=True) - result_first = result[("one", 1)] + result_first = result["one", 1] assert result_first.to_dict(as_series=False) == {"a": ["one"], "b": [1]} result = df.partition_by("a", as_dict=True) - result_first = result[("one",)] + result_first = result["one",] assert result_first.to_dict(as_series=False) == {"a": ["one", "one"], "b": [1, 3]} @@ -65,7 +65,7 @@ def test_partition_by_as_dict_include_keys_false() -> None: df = pl.DataFrame({"a": ["one", "two", "one", "two"], "b": [1, 2, 3, 4]}) result = df.partition_by("a", include_key=False, as_dict=True) - result_first = result[("one",)] + result_first = result["one",] assert result_first.to_dict(as_series=False) == {"b": [1, 3]} diff --git a/py-polars/tests/unit/datatypes/test_object.py b/py-polars/tests/unit/datatypes/test_object.py index 8db373d3f58a..955461eac959 100644 --- a/py-polars/tests/unit/datatypes/test_object.py +++ b/py-polars/tests/unit/datatypes/test_object.py @@ -166,7 +166,7 @@ def test_null_obj_str_13512() -> None: def test_format_object_series_14267() -> None: s = pl.Series([Path(), Path("abc")]) - expected = "shape: (2,)\n" "Series: '' [o][object]\n" "[\n" "\t.\n" "\tabc\n" "]" + expected = "shape: (2,)\nSeries: '' [o][object]\n[\n\t.\n\tabc\n]" assert str(s) == expected diff --git a/py-polars/tests/unit/interop/numpy/test_ufunc_expr.py b/py-polars/tests/unit/interop/numpy/test_ufunc_expr.py index bba5c72b6fc7..1fe9ea3ed4be 100644 --- a/py-polars/tests/unit/interop/numpy/test_ufunc_expr.py +++ b/py-polars/tests/unit/interop/numpy/test_ufunc_expr.py @@ -137,7 +137,7 @@ def test_generalized_ufunc_scalar() -> None: numba = pytest.importorskip("numba") @numba.guvectorize([(numba.int64[:], numba.int64[:])], "(n)->()") # type: ignore[misc] - def my_custom_sum(arr, result) -> None: # type: ignore[no-untyped-def] + def my_custom_sum(arr, result) -> None: # type: ignore[no-untyped-def] # noqa: ANN001 total = 0 for value in arr: total += value diff --git a/py-polars/tests/unit/interop/test_interop.py b/py-polars/tests/unit/interop/test_interop.py index 1730a5c6f8bb..7f664e2fb197 100644 --- a/py-polars/tests/unit/interop/test_interop.py +++ b/py-polars/tests/unit/interop/test_interop.py @@ -5,7 +5,6 @@ import numpy as np import pandas as pd -import pyarrow import pyarrow as pa import pytest @@ -721,27 +720,27 @@ def test_compat_level(monkeypatch: pytest.MonkeyPatch) -> None: str_col = pl.Series(["awd"]) bin_col = pl.Series([b"dwa"]) assert str_col._newest_compat_level() == newest._version # type: ignore[attr-defined] - assert isinstance(str_col.to_arrow(), pyarrow.LargeStringArray) - assert isinstance(str_col.to_arrow(compat_level=oldest), pyarrow.LargeStringArray) - assert isinstance(str_col.to_arrow(compat_level=newest), pyarrow.StringViewArray) - assert isinstance(bin_col.to_arrow(), pyarrow.LargeBinaryArray) - assert isinstance(bin_col.to_arrow(compat_level=oldest), pyarrow.LargeBinaryArray) - assert isinstance(bin_col.to_arrow(compat_level=newest), pyarrow.BinaryViewArray) + assert isinstance(str_col.to_arrow(), pa.LargeStringArray) + assert isinstance(str_col.to_arrow(compat_level=oldest), pa.LargeStringArray) + assert isinstance(str_col.to_arrow(compat_level=newest), pa.StringViewArray) + assert isinstance(bin_col.to_arrow(), pa.LargeBinaryArray) + assert isinstance(bin_col.to_arrow(compat_level=oldest), pa.LargeBinaryArray) + assert isinstance(bin_col.to_arrow(compat_level=newest), pa.BinaryViewArray) df = pl.DataFrame({"str_col": str_col, "bin_col": bin_col}) - assert isinstance(df.to_arrow()["str_col"][0], pyarrow.LargeStringScalar) + assert isinstance(df.to_arrow()["str_col"][0], pa.LargeStringScalar) assert isinstance( - df.to_arrow(compat_level=oldest)["str_col"][0], pyarrow.LargeStringScalar + df.to_arrow(compat_level=oldest)["str_col"][0], pa.LargeStringScalar ) assert isinstance( - df.to_arrow(compat_level=newest)["str_col"][0], pyarrow.StringViewScalar + df.to_arrow(compat_level=newest)["str_col"][0], pa.StringViewScalar ) - assert isinstance(df.to_arrow()["bin_col"][0], pyarrow.LargeBinaryScalar) + assert isinstance(df.to_arrow()["bin_col"][0], pa.LargeBinaryScalar) assert isinstance( - df.to_arrow(compat_level=oldest)["bin_col"][0], pyarrow.LargeBinaryScalar + df.to_arrow(compat_level=oldest)["bin_col"][0], pa.LargeBinaryScalar ) assert isinstance( - df.to_arrow(compat_level=newest)["bin_col"][0], pyarrow.BinaryViewScalar + df.to_arrow(compat_level=newest)["bin_col"][0], pa.BinaryViewScalar ) assert len(df.write_ipc(None).getbuffer()) == 786 diff --git a/py-polars/tests/unit/io/database/test_async.py b/py-polars/tests/unit/io/database/test_async.py index b51a01c8e37e..a8492e7a5276 100644 --- a/py-polars/tests/unit/io/database/test_async.py +++ b/py-polars/tests/unit/io/database/test_async.py @@ -50,7 +50,7 @@ async def __aenter__(self) -> Any: await self.connect() return self - async def __aexit__(self, *args: Any, **kwargs: Any) -> None: + async def __aexit__(self, *args: object, **kwargs: Any) -> None: await self.close() async def close(self) -> None: diff --git a/py-polars/tests/unit/io/database/test_read.py b/py-polars/tests/unit/io/database/test_read.py index b50e88602dac..967379f356f7 100644 --- a/py-polars/tests/unit/io/database/test_read.py +++ b/py-polars/tests/unit/io/database/test_read.py @@ -108,7 +108,7 @@ def __init__( batched: bool, exact_batch_size: bool, repeat_batch_calls: bool = False, - ): + ) -> None: self.test_data = test_data self.repeat_batched_calls = repeat_batch_calls self.exact_batch_size = exact_batch_size diff --git a/py-polars/tests/unit/io/test_csv.py b/py-polars/tests/unit/io/test_csv.py index fcacedead1d4..dff14d620d4a 100644 --- a/py-polars/tests/unit/io/test_csv.py +++ b/py-polars/tests/unit/io/test_csv.py @@ -760,9 +760,9 @@ def test_ignore_try_parse_dates() -> None: ).encode() headers = ["a", "b", "c"] - dtypes: dict[str, type[pl.DataType]] = { - k: pl.String for k in headers - } # Forces String type for every column + dtypes: dict[str, type[pl.DataType]] = dict.fromkeys( + headers, pl.String + ) # Forces String type for every column df = pl.read_csv(csv, columns=headers, schema_overrides=dtypes) assert df.dtypes == [pl.String, pl.String, pl.String] @@ -1449,7 +1449,7 @@ def test_duplicated_columns() -> None: def test_error_message() -> None: - data = io.StringIO("target,wind,energy,miso\n" "1,2,3,4\n" "1,2,1e5,1\n") + data = io.StringIO("target,wind,energy,miso\n1,2,3,4\n1,2,1e5,1\n") with pytest.raises( ComputeError, match=r"could not parse `1e5` as dtype `i64` at column 'energy' \(column number 3\)", @@ -1717,10 +1717,8 @@ def test_csv_multiline_splits() -> None: def some_multiline_str(n: int) -> str: strs = [] strs.append('"') - # sample between 0 and 5 so that it is likely - # the multiline field also go 3 separators. - for length in np.random.randint(0, 5, n): - strs.append(f"{'xx,' * length}") + # sample between 0-5 so it is likely the multiline field also gets 3 separators. + strs.extend(f"{'xx,' * length}" for length in np.random.randint(0, 5, n)) strs.append('"') return "\n".join(strs) @@ -1993,7 +1991,7 @@ def test_empty_csv_no_raise() -> None: def test_csv_no_new_line_last() -> None: - csv = io.StringIO("a b\n" "1 1\n" "2 2\n" "3 2.1") + csv = io.StringIO("a b\n1 1\n2 2\n3 2.1") assert pl.read_csv(csv, separator=" ").to_dict(as_series=False) == { "a": [1, 2, 3], "b": [1.0, 2.0, 2.1], diff --git a/py-polars/tests/unit/io/test_lazy_count_star.py b/py-polars/tests/unit/io/test_lazy_count_star.py index a2c03596dd15..6a57dee9487d 100644 --- a/py-polars/tests/unit/io/test_lazy_count_star.py +++ b/py-polars/tests/unit/io/test_lazy_count_star.py @@ -30,18 +30,12 @@ def test_count_csv(io_files_path: Path, path: str, n_rows: int) -> None: @pytest.mark.write_disk def test_commented_csv() -> None: csv_a = NamedTemporaryFile() - csv_a.write( - b""" -A,B -Gr1,A -Gr1,B -# comment line - """.strip() - ) + csv_a.write(b"A,B\nGr1,A\nGr1,B\n# comment line\n") csv_a.seek(0) expected = pl.DataFrame(pl.Series("len", [2], dtype=pl.UInt32)) lf = pl.scan_csv(csv_a.name, comment_prefix="#").select(pl.len()) + assert "FAST COUNT" in lf.explain() assert_frame_equal(lf.collect(), expected) diff --git a/py-polars/tests/unit/io/test_parquet.py b/py-polars/tests/unit/io/test_parquet.py index e035a44686f0..10aa23dfa7b6 100644 --- a/py-polars/tests/unit/io/test_parquet.py +++ b/py-polars/tests/unit/io/test_parquet.py @@ -1731,7 +1731,7 @@ def test_different_page_validity_across_pages(value: str | int | float | bool) - def test_delta_length_byte_array_prefiltering(df: pl.DataFrame) -> None: cols = df.columns - encodings = {col: "DELTA_LENGTH_BYTE_ARRAY" for col in cols} + encodings = dict.fromkeys(cols, "DELTA_LENGTH_BYTE_ARRAY") encodings["filter_col"] = "PLAIN" f = io.BytesIO() diff --git a/py-polars/tests/unit/io/test_scan.py b/py-polars/tests/unit/io/test_scan.py index d710954fcbfe..8fdfb83f44ec 100644 --- a/py-polars/tests/unit/io/test_scan.py +++ b/py-polars/tests/unit/io/test_scan.py @@ -48,10 +48,10 @@ def _scan( if ( scan_func := { - ".ipc" : pl.scan_ipc, - ".parquet" : pl.scan_parquet, - ".csv" : pl.scan_csv, - ".ndjson" : pl.scan_ndjson, + ".ipc": pl.scan_ipc, + ".parquet": pl.scan_parquet, + ".csv": pl.scan_csv, + ".ndjson": pl.scan_ndjson, }.get(suffix) ) is not None: # fmt: skip result = scan_func( @@ -72,10 +72,10 @@ def _write(df: pl.DataFrame, file_path: Path) -> None: if ( write_func := { - ".ipc" : pl.DataFrame.write_ipc, - ".parquet" : pl.DataFrame.write_parquet, - ".csv" : pl.DataFrame.write_csv, - ".ndjson" : pl.DataFrame.write_ndjson, + ".ipc": pl.DataFrame.write_ipc, + ".parquet": pl.DataFrame.write_parquet, + ".csv": pl.DataFrame.write_csv, + ".ndjson": pl.DataFrame.write_ndjson, }.get(suffix) ) is not None: # fmt: skip return write_func(df, file_path) # type: ignore[operator, no-any-return] diff --git a/py-polars/tests/unit/operations/map/test_inefficient_map_warning.py b/py-polars/tests/unit/operations/map/test_inefficient_map_warning.py index 57a8ce795dc1..74946f084d51 100644 --- a/py-polars/tests/unit/operations/map/test_inefficient_map_warning.py +++ b/py-polars/tests/unit/operations/map/test_inefficient_map_warning.py @@ -9,8 +9,7 @@ from math import cosh from typing import Any, Callable -import numpy -import numpy as np # noqa: F401 +import numpy as np import pytest import polars as pl @@ -103,7 +102,7 @@ ("e", "lambda x: np.arcsinh(x)", 'pl.col("e").arcsinh()'), ("e", "lambda x: np.arctan(x)", 'pl.col("e").arctan()'), ("e", "lambda x: np.arctanh(x)", 'pl.col("e").arctanh()'), - ("a", "lambda x: 0 + numpy.cbrt(x)", '0 + pl.col("a").cbrt()'), + ("a", "lambda x: 0 + np.cbrt(x)", '0 + pl.col("a").cbrt()'), ("e", "lambda x: np.ceil(x)", 'pl.col("e").ceil()'), ("e", "lambda x: np.cos(x)", 'pl.col("e").cos()'), ("e", "lambda x: np.cosh(x)", 'pl.col("e").cosh()'), @@ -259,7 +258,7 @@ "datetime": datetime, "dt": dt, "math": math, - "np": numpy, + "np": np, "pl": pl, } @@ -330,7 +329,7 @@ def test_parse_apply_raw_functions() -> None: # test bare 'numpy' functions for func_name in _NUMPY_FUNCTIONS: - func = getattr(numpy, func_name) + func = getattr(np, func_name) # note: we can't parse/rewrite raw numpy functions... parser = BytecodeParser(func, map_target="expr") @@ -355,7 +354,7 @@ def test_parse_apply_raw_functions() -> None: pl.col("value").str.json_decode(), pl.col("value").map_elements(json.loads), ): - result_frames.append( + result_frames.append( # noqa: PERF401 pl.LazyFrame({"value": ['{"a":1, "b": true, "c": "xx"}', None]}) .select(extracted=expr) .unnest("extracted") @@ -411,10 +410,8 @@ def mcosh(self, x: float) -> float: ): s = pl.Series("srs", [0, 1, 2, 3, 4]) assert_series_equal( - s.map_elements( - lambda x: numpy.cos(3) + x - abs(-1), return_dtype=pl.Float64 - ), - numpy.cos(3) + s - 1, + s.map_elements(lambda x: np.cos(3) + x - abs(-1), return_dtype=pl.Float64), + np.cos(3) + s - 1, ) # if 's' is already the name of a global variable then the series alias diff --git a/py-polars/tests/unit/operations/map/test_map_groups.py b/py-polars/tests/unit/operations/map/test_map_groups.py index e8f5554333b1..d675d43e28f6 100644 --- a/py-polars/tests/unit/operations/map/test_map_groups.py +++ b/py-polars/tests/unit/operations/map/test_map_groups.py @@ -119,7 +119,7 @@ def test_map_groups_object_output() -> None: ) class Foo: - def __init__(self, payload: Any): + def __init__(self, payload: Any) -> None: self.payload = payload result = df.group_by("groups").agg( diff --git a/py-polars/tests/unit/operations/namespaces/temporal/test_to_datetime.py b/py-polars/tests/unit/operations/namespaces/temporal/test_to_datetime.py index 08ab045467ca..5bb09a0f8d1c 100644 --- a/py-polars/tests/unit/operations/namespaces/temporal/test_to_datetime.py +++ b/py-polars/tests/unit/operations/namespaces/temporal/test_to_datetime.py @@ -55,7 +55,7 @@ for fraction in FRACTIONS if time_format.endswith("%S") or fraction == "" for tz in TIMEZONES - if date_format.startswith("%Y") and time_format != "" or tz == "" + if (date_format.startswith("%Y") and time_format != "") or tz == "" ] diff --git a/py-polars/tests/unit/operations/namespaces/test_binary.py b/py-polars/tests/unit/operations/namespaces/test_binary.py index 92432dd74592..e15ca2010817 100644 --- a/py-polars/tests/unit/operations/namespaces/test_binary.py +++ b/py-polars/tests/unit/operations/namespaces/test_binary.py @@ -50,7 +50,7 @@ def test_contains() -> None: expected == df.select(pl.col("bin").bin.contains(pattern))["bin"].to_list() ) # frame filter - assert sum([e for e in expected if e is True]) == len( + assert sum(e for e in expected if e is True) == len( df.filter(pl.col("bin").bin.contains(pattern)) ) diff --git a/py-polars/tests/unit/operations/test_cast.py b/py-polars/tests/unit/operations/test_cast.py index 1b00c4fb0fe3..cdf7688f7c32 100644 --- a/py-polars/tests/unit/operations/test_cast.py +++ b/py-polars/tests/unit/operations/test_cast.py @@ -313,7 +313,6 @@ def _cast_lit_t( "expected_value", ), [ - # fmt: off # date to datetime (date(1970, 1, 1), pl.Date, pl.Datetime("ms"), True, datetime(1970, 1, 1)), (date(1970, 1, 1), pl.Date, pl.Datetime("us"), True, datetime(1970, 1, 1)), @@ -350,7 +349,6 @@ def _cast_lit_t( (date(2149, 6, 7), pl.Date, pl.Int16, False, None), (datetime(9999, 12, 31), pl.Datetime, pl.Int8, False, None), (datetime(9999, 12, 31), pl.Datetime, pl.Int16, False, None), - # fmt: on ], ) def test_strict_cast_temporal( @@ -388,7 +386,6 @@ def test_strict_cast_temporal( "expected_value", ), [ - # fmt: off # date to datetime (date(1970, 1, 1), pl.Date, pl.Datetime("ms"), datetime(1970, 1, 1)), (date(1970, 1, 1), pl.Date, pl.Datetime("us"), datetime(1970, 1, 1)), @@ -425,7 +422,6 @@ def test_strict_cast_temporal( (date(2149, 6, 7), pl.Date, pl.Int16, None), (datetime(9999, 12, 31), pl.Datetime, pl.Int8, None), (datetime(9999, 12, 31), pl.Datetime, pl.Int16, None), - # fmt: on ], ) def test_cast_temporal( diff --git a/py-polars/tests/unit/operations/test_comparison.py b/py-polars/tests/unit/operations/test_comparison.py index 42535cff85a6..72771ff64eab 100644 --- a/py-polars/tests/unit/operations/test_comparison.py +++ b/py-polars/tests/unit/operations/test_comparison.py @@ -236,7 +236,7 @@ def verify_total_ordering( "ne_missing": [refmiss != "="], } ans_correct = pl.DataFrame( - ans_correct_dict, schema={c: pl.Boolean for c in ans_correct_dict} + ans_correct_dict, schema=dict.fromkeys(ans_correct_dict, pl.Boolean) ) assert_frame_equal(ans[:1], ans_correct) @@ -287,7 +287,7 @@ def verify_total_ordering_broadcast( "ne_missing": [refmiss != "="], } ans_correct = pl.DataFrame( - ans_correct_dict, schema={c: pl.Boolean for c in ans_correct_dict} + ans_correct_dict, schema=dict.fromkeys(ans_correct_dict, pl.Boolean) ) assert_frame_equal(ans_first[:1], ans_correct) diff --git a/py-polars/tests/unit/operations/test_group_by.py b/py-polars/tests/unit/operations/test_group_by.py index 09864b329c22..af9dc9a180e2 100644 --- a/py-polars/tests/unit/operations/test_group_by.py +++ b/py-polars/tests/unit/operations/test_group_by.py @@ -371,7 +371,7 @@ def test_group_by_iteration() -> None: def test_group_by_iteration_selector() -> None: df = pl.DataFrame({"a": ["one", "two", "one", "two"], "b": [1, 2, 3, 4]}) result = dict(df.group_by(cs.string())) - result_first = result[("one",)] + result_first = result["one",] assert result_first.to_dict(as_series=False) == {"a": ["one", "one"], "b": [1, 3]} diff --git a/py-polars/tests/unit/series/test_series.py b/py-polars/tests/unit/series/test_series.py index 3f7f159ccae0..2a4d6f1d5285 100644 --- a/py-polars/tests/unit/series/test_series.py +++ b/py-polars/tests/unit/series/test_series.py @@ -9,7 +9,6 @@ import pyarrow as pa import pytest -import polars import polars as pl from polars._utils.construction import iterable_to_pyseries from polars.datatypes import ( @@ -959,7 +958,7 @@ def test_round_sig_figs( def test_round_sig_figs_raises_exc() -> None: - with pytest.raises(polars.exceptions.InvalidOperationError): + with pytest.raises(pl.exceptions.InvalidOperationError): pl.Series([1.234, 0.1234]).round_sig_figs(digits=0) @@ -1153,7 +1152,7 @@ def gen(n: int) -> Iterator[int]: # iterable object class Data: - def __init__(self, n: int): + def __init__(self, n: int) -> None: self._n = n def __iter__(self) -> Iterator[int]: diff --git a/py-polars/tests/unit/test_api.py b/py-polars/tests/unit/test_api.py index a90bc5ceb68d..c49254801562 100644 --- a/py-polars/tests/unit/test_api.py +++ b/py-polars/tests/unit/test_api.py @@ -11,7 +11,7 @@ def test_custom_df_namespace() -> None: @pl.api.register_dataframe_namespace("split") class SplitFrame: - def __init__(self, df: pl.DataFrame): + def __init__(self, df: pl.DataFrame) -> None: self._df = df def by_first_letter_of_column_names(self) -> list[pl.DataFrame]: @@ -47,7 +47,7 @@ def by_first_letter_of_column_values(self, col: str) -> list[pl.DataFrame]: def test_custom_expr_namespace() -> None: @pl.api.register_expr_namespace("power") class PowersOfN: - def __init__(self, expr: pl.Expr): + def __init__(self, expr: pl.Expr) -> None: self._expr = expr def next(self, p: int) -> pl.Expr: @@ -76,7 +76,7 @@ def nearest(self, p: int) -> pl.Expr: def test_custom_lazy_namespace() -> None: @pl.api.register_lazyframe_namespace("split") class SplitFrame: - def __init__(self, lf: pl.LazyFrame): + def __init__(self, lf: pl.LazyFrame) -> None: self._lf = lf def by_column_dtypes(self) -> list[pl.LazyFrame]: @@ -109,7 +109,7 @@ def by_column_dtypes(self) -> list[pl.LazyFrame]: def test_custom_series_namespace() -> None: @pl.api.register_series_namespace("math") class CustomMath: - def __init__(self, s: pl.Series): + def __init__(self, s: pl.Series) -> None: self._s = s def square(self) -> pl.Series: @@ -151,19 +151,19 @@ def test_namespace_cannot_override_builtin() -> None: @pl.api.register_dataframe_namespace("dt") class CustomDt: - def __init__(self, df: pl.DataFrame): + def __init__(self, df: pl.DataFrame) -> None: self._df = df def test_namespace_warning_on_override() -> None: @pl.api.register_dataframe_namespace("math") class CustomMath: - def __init__(self, df: pl.DataFrame): + def __init__(self, df: pl.DataFrame) -> None: self._df = df with pytest.raises(UserWarning): @pl.api.register_dataframe_namespace("math") class CustomMath2: - def __init__(self, df: pl.DataFrame): + def __init__(self, df: pl.DataFrame) -> None: self._df = df diff --git a/py-polars/tests/unit/test_config.py b/py-polars/tests/unit/test_config.py index e4f1a152ad7b..f6cd9411bebf 100644 --- a/py-polars/tests/unit/test_config.py +++ b/py-polars/tests/unit/test_config.py @@ -123,7 +123,7 @@ def test_set_tbl_rows() -> None: "│ … ┆ … ┆ … │\n" "└─────┴─────┴─────┘" ) - assert str(ser) == "shape: (5,)\n" "Series: 'ser' [i64]\n" "[\n" "\t…\n" "]" + assert str(ser) == "shape: (5,)\nSeries: 'ser' [i64]\n[\n\t…\n]" pl.Config.set_tbl_rows(1) assert ( @@ -137,7 +137,7 @@ def test_set_tbl_rows() -> None: "│ … ┆ … ┆ … │\n" "└─────┴─────┴─────┘" ) - assert str(ser) == "shape: (5,)\n" "Series: 'ser' [i64]\n" "[\n" "\t1\n" "\t…\n" "]" + assert str(ser) == "shape: (5,)\nSeries: 'ser' [i64]\n[\n\t1\n\t…\n]" pl.Config.set_tbl_rows(2) assert ( @@ -479,7 +479,7 @@ def test_shape_format_for_big_numbers() -> None: pl.Config.set_tbl_rows(0) ser = pl.Series("ser", range(1000)) - assert str(ser) == "shape: (1_000,)\n" "Series: 'ser' [i64]\n" "[\n" "\t…\n" "]" + assert str(ser) == "shape: (1_000,)\nSeries: 'ser' [i64]\n[\n\t…\n]" pl.Config.set_tbl_rows(1) pl.Config.set_tbl_cols(1) diff --git a/py-polars/tests/unit/test_cse.py b/py-polars/tests/unit/test_cse.py index c4b6466e51b7..6874512e6133 100644 --- a/py-polars/tests/unit/test_cse.py +++ b/py-polars/tests/unit/test_cse.py @@ -148,13 +148,7 @@ def test_cse_9630() -> None: @pytest.mark.write_disk def test_schema_row_index_cse() -> None: csv_a = NamedTemporaryFile() - csv_a.write( - b""" -A,B -Gr1,A -Gr1,B - """.strip() - ) + csv_a.write(b"A,B\nGr1,A\nGr1,B") csv_a.seek(0) df_a = pl.scan_csv(csv_a.name).with_row_index("Idx") @@ -166,8 +160,6 @@ def test_schema_row_index_cse() -> None: .collect(comm_subexpr_elim=True) ) - csv_a.close() - expected = pl.DataFrame( { "A": ["Gr1"], diff --git a/py-polars/tests/unit/test_format.py b/py-polars/tests/unit/test_format.py index 8f69d62fcaee..2461f100209b 100644 --- a/py-polars/tests/unit/test_format.py +++ b/py-polars/tests/unit/test_format.py @@ -81,7 +81,7 @@ def test_fmt_series( s = pl.Series(name="foo", values=values) with pl.Config(fmt_str_lengths=15): print(s) - out, err = capfd.readouterr() + out, _err = capfd.readouterr() assert out == expected @@ -246,7 +246,7 @@ def test_fmt_unsigned_int_thousands_sep( def test_fmt_float(capfd: pytest.CaptureFixture[str]) -> None: s = pl.Series(name="foo", values=[7.966e-05, 7.9e-05, 8.4666e-05, 8.00007966]) print(s) - out, err = capfd.readouterr() + out, _err = capfd.readouterr() expected = """shape: (4,) Series: 'foo' [f64] [