From a0ec630b25aa847699f9c2d7389fee84749a6491 Mon Sep 17 00:00:00 2001 From: Ritchie Vink Date: Fri, 27 Sep 2024 12:56:26 +0200 Subject: [PATCH] feat(python): Drop python 3.8 support (#18965) --- .github/workflows/lint-python.yml | 4 +- .github/workflows/release-python.yml | 2 +- .github/workflows/test-python.yml | 2 +- py-polars/Cargo.toml | 2 +- py-polars/polars/_typing.py | 52 ++++++++----------- py-polars/polars/_utils/async_.py | 4 +- .../polars/_utils/construction/dataframe.py | 8 ++- .../polars/_utils/construction/series.py | 7 ++- py-polars/polars/_utils/construction/utils.py | 4 +- py-polars/polars/_utils/convert.py | 2 +- py-polars/polars/_utils/deprecation.py | 5 +- py-polars/polars/_utils/getitem.py | 5 +- py-polars/polars/_utils/parse/expr.py | 3 +- py-polars/polars/_utils/udfs.py | 4 +- py-polars/polars/_utils/various.py | 13 +++-- py-polars/polars/convert/general.py | 5 +- py-polars/polars/convert/normalize.py | 17 ++---- py-polars/polars/dataframe/_html.py | 3 +- py-polars/polars/dataframe/frame.py | 23 ++++---- py-polars/polars/dataframe/group_by.py | 3 +- py-polars/polars/dataframe/plotting.py | 4 +- py-polars/polars/datatypes/classes.py | 5 +- py-polars/polars/datatypes/constructor.py | 4 +- py-polars/polars/datatypes/convert.py | 3 +- py-polars/polars/datatypes/group.py | 3 +- py-polars/polars/dependencies.py | 13 ++--- py-polars/polars/expr/array.py | 3 +- py-polars/polars/expr/datetime.py | 4 +- py-polars/polars/expr/expr.py | 10 ++-- py-polars/polars/expr/list.py | 3 +- py-polars/polars/expr/string.py | 3 +- py-polars/polars/expr/struct.py | 4 +- py-polars/polars/expr/whenthen.py | 4 +- .../functions/aggregation/horizontal.py | 4 +- py-polars/polars/functions/as_datatype.py | 3 +- py-polars/polars/functions/business.py | 4 +- py-polars/polars/functions/col.py | 3 +- py-polars/polars/functions/eager.py | 5 +- py-polars/polars/functions/lazy.py | 6 ++- py-polars/polars/functions/whenthen.py | 4 +- py-polars/polars/interchange/protocol.py | 6 +-- py-polars/polars/io/_utils.py | 6 ++- py-polars/polars/io/csv/_utils.py | 4 +- py-polars/polars/io/csv/batched_reader.py | 3 +- py-polars/polars/io/csv/functions.py | 5 +- .../polars/io/database/_cursor_proxies.py | 4 +- py-polars/polars/io/database/_executor.py | 6 +-- py-polars/polars/io/database/functions.py | 3 +- py-polars/polars/io/ipc/functions.py | 4 +- py-polars/polars/io/ndjson.py | 3 +- py-polars/polars/io/plugins.py | 6 ++- py-polars/polars/io/spreadsheet/_utils.py | 5 +- .../polars/io/spreadsheet/_write_utils.py | 4 +- py-polars/polars/io/spreadsheet/functions.py | 3 +- py-polars/polars/lazyframe/frame.py | 8 ++- py-polars/polars/lazyframe/group_by.py | 4 +- py-polars/polars/ml/torch.py | 3 +- py-polars/polars/plugins.py | 4 +- py-polars/polars/schema.py | 12 ++--- py-polars/polars/selectors.py | 4 +- py-polars/polars/series/array.py | 3 +- py-polars/polars/series/datetime.py | 3 +- py-polars/polars/series/list.py | 3 +- py-polars/polars/series/series.py | 7 +-- py-polars/polars/series/string.py | 4 +- py-polars/polars/series/struct.py | 4 +- py-polars/polars/sql/context.py | 3 +- .../testing/parametric/strategies/core.py | 4 +- .../testing/parametric/strategies/data.py | 4 +- .../testing/parametric/strategies/dtype.py | 4 +- .../testing/parametric/strategies/legacy.py | 3 +- py-polars/pyproject.toml | 3 +- py-polars/tests/docs/run_doctest.py | 3 +- py-polars/tests/docs/test_user_guide.py | 2 +- py-polars/tests/unit/conftest.py | 11 ++-- .../unit/constructors/test_constructors.py | 5 +- .../tests/unit/constructors/test_dataframe.py | 6 ++- py-polars/tests/unit/dataframe/test_df.py | 3 +- .../tests/unit/dataframe/test_upsample.py | 1 - py-polars/tests/unit/datatypes/test_parse.py | 15 +++--- py-polars/tests/unit/io/cloud/test_aws.py | 3 +- .../tests/unit/io/database/test_async.py | 3 +- py-polars/tests/unit/io/database/test_read.py | 2 +- .../tests/unit/io/database/test_write.py | 8 +-- py-polars/tests/unit/io/test_plugins.py | 2 +- py-polars/tests/unit/io/test_spreadsheet.py | 4 +- py-polars/tests/unit/io/test_utils.py | 5 +- .../unit/operations/map/test_map_elements.py | 7 +-- .../unit/operations/map/test_map_groups.py | 5 +- .../temporal/test_add_business_days.py | 9 +--- .../namespaces/temporal/test_to_datetime.py | 10 +--- .../tests/unit/operations/test_comparison.py | 4 +- .../tests/unit/operations/test_cross_join.py | 11 +--- .../tests/unit/operations/test_ewm_by.py | 9 +--- .../tests/unit/operations/test_interpolate.py | 9 +--- .../tests/unit/operations/test_transpose.py | 2 +- py-polars/tests/unit/series/test_series.py | 3 +- py-polars/tests/unit/sql/test_joins.py | 30 +++++++---- py-polars/tests/unit/test_config.py | 5 +- py-polars/tests/unit/test_format.py | 9 ++-- py-polars/tests/unit/test_selectors.py | 10 +--- py-polars/tests/unit/test_string_cache.py | 2 +- py-polars/tests/unit/utils/test_utils.py | 3 +- 103 files changed, 325 insertions(+), 278 deletions(-) diff --git a/.github/workflows/lint-python.yml b/.github/workflows/lint-python.yml index 5197645a8f23..f75e5e576e02 100644 --- a/.github/workflows/lint-python.yml +++ b/.github/workflows/lint-python.yml @@ -39,7 +39,7 @@ jobs: strategy: fail-fast: false matrix: - python-version: ['3.8', '3.12'] + python-version: ['3.9', '3.12'] steps: - uses: actions/checkout@v4 @@ -63,4 +63,4 @@ jobs: # Allow untyped calls for older Python versions - name: Run mypy working-directory: py-polars - run: mypy ${{ (matrix.python-version == '3.8') && '--allow-untyped-calls' || '' }} + run: mypy ${{ (matrix.python-version == '3.9') && '--allow-untyped-calls' || '' }} diff --git a/.github/workflows/release-python.yml b/.github/workflows/release-python.yml index e7b482b42749..c44242a2a374 100644 --- a/.github/workflows/release-python.yml +++ b/.github/workflows/release-python.yml @@ -18,7 +18,7 @@ concurrency: cancel-in-progress: true env: - PYTHON_VERSION: '3.8' + PYTHON_VERSION: '3.9' CARGO_INCREMENTAL: 0 CARGO_NET_RETRY: 10 RUSTUP_MAX_RETRIES: 10 diff --git a/.github/workflows/test-python.yml b/.github/workflows/test-python.yml index 23fe797f2795..4f7c7e6a7027 100644 --- a/.github/workflows/test-python.yml +++ b/.github/workflows/test-python.yml @@ -39,7 +39,7 @@ jobs: fail-fast: false matrix: os: [ubuntu-latest] - python-version: ['3.8', '3.11', '3.12'] + python-version: ['3.9', '3.12'] include: - os: windows-latest python-version: '3.12' diff --git a/py-polars/Cargo.toml b/py-polars/Cargo.toml index 6f26a5475da4..7a783d9e7481 100644 --- a/py-polars/Cargo.toml +++ b/py-polars/Cargo.toml @@ -12,7 +12,7 @@ libc = { workspace = true } # Explicit dependency is needed to add bigidx in CI during release polars = { workspace = true } polars-python = { workspace = true, features = ["pymethods", "iejoin"] } -pyo3 = { workspace = true, features = ["abi3-py38", "chrono", "extension-module", "multiple-pymethods"] } +pyo3 = { workspace = true, features = ["abi3-py39", "chrono", "extension-module", "multiple-pymethods"] } [build-dependencies] built = { version = "0.7", features = ["chrono", "git2", "cargo-lock"], optional = true } diff --git a/py-polars/polars/_typing.py b/py-polars/polars/_typing.py index 9b0cc722de57..1670b08aeb2f 100644 --- a/py-polars/polars/_typing.py +++ b/py-polars/polars/_typing.py @@ -1,17 +1,11 @@ from __future__ import annotations +from collections.abc import Collection, Iterable, Mapping, Sequence from typing import ( TYPE_CHECKING, Any, - Collection, - Iterable, - List, Literal, - Mapping, Protocol, - Sequence, - Tuple, - Type, TypedDict, TypeVar, Union, @@ -55,29 +49,29 @@ def __arrow_c_stream__(self, requested_schema: object | None = None) -> object: # Data types PolarsDataType: TypeAlias = Union["DataTypeClass", "DataType"] -PolarsTemporalType: TypeAlias = Union[Type["TemporalType"], "TemporalType"] -PolarsIntegerType: TypeAlias = Union[Type["IntegerType"], "IntegerType"] +PolarsTemporalType: TypeAlias = Union[type["TemporalType"], "TemporalType"] +PolarsIntegerType: TypeAlias = Union[type["IntegerType"], "IntegerType"] OneOrMoreDataTypes: TypeAlias = Union[PolarsDataType, Iterable[PolarsDataType]] PythonDataType: TypeAlias = Union[ - Type[int], - Type[float], - Type[bool], - Type[str], - Type["date"], - Type["time"], - Type["datetime"], - Type["timedelta"], - Type[List[Any]], - Type[Tuple[Any, ...]], - Type[bytes], - Type[object], - Type["Decimal"], - Type[None], + type[int], + type[float], + type[bool], + type[str], + type["date"], + type["time"], + type["datetime"], + type["timedelta"], + type[list[Any]], + type[tuple[Any, ...]], + type[bytes], + type[object], + type["Decimal"], + type[None], ] SchemaDefinition: TypeAlias = Union[ Mapping[str, Union[PolarsDataType, PythonDataType]], - Sequence[Union[str, Tuple[str, Union[PolarsDataType, PythonDataType, None]]]], + Sequence[Union[str, tuple[str, Union[PolarsDataType, PythonDataType, None]]]], ] SchemaDict: TypeAlias = Mapping[str, PolarsDataType] @@ -85,7 +79,7 @@ def __arrow_c_stream__(self, requested_schema: object | None = None) -> object: TemporalLiteral: TypeAlias = Union["date", "time", "datetime", "timedelta"] NonNestedLiteral: TypeAlias = Union[NumericLiteral, TemporalLiteral, str, bool, bytes] # Python literal types (can convert into a `lit` expression) -PythonLiteral: TypeAlias = Union[NonNestedLiteral, List[Any]] +PythonLiteral: TypeAlias = Union[NonNestedLiteral, list[Any]] # Inputs that can convert into a `col` expression IntoExprColumn: TypeAlias = Union["Expr", "Series", str] # Inputs that can convert into an expression @@ -204,7 +198,7 @@ def __arrow_c_stream__(self, requested_schema: object | None = None) -> object: # Excel IO ColumnFormatDict: TypeAlias = Mapping[ # dict of colname(s) or selector(s) to format string or dict - Union[ColumnNameOrSelector, Tuple[ColumnNameOrSelector, ...]], + Union[ColumnNameOrSelector, tuple[ColumnNameOrSelector, ...]], Union[str, Mapping[str, str]], ] ConditionalFormatDict: TypeAlias = Mapping[ @@ -214,12 +208,12 @@ def __arrow_c_stream__(self, requested_schema: object | None = None) -> object: ] ColumnTotalsDefinition: TypeAlias = Union[ # dict of colname(s) to str, a collection of str, or a boolean - Mapping[Union[ColumnNameOrSelector, Tuple[ColumnNameOrSelector]], str], + Mapping[Union[ColumnNameOrSelector, tuple[ColumnNameOrSelector]], str], Sequence[str], bool, ] ColumnWidthsDefinition: TypeAlias = Union[ - Mapping[ColumnNameOrSelector, Union[Tuple[str, ...], int]], int + Mapping[ColumnNameOrSelector, Union[tuple[str, ...], int]], int ] RowTotalsDefinition: TypeAlias = Union[ # dict of colname to str(s), a collection of str, or a boolean @@ -234,7 +228,7 @@ def __arrow_c_stream__(self, requested_schema: object | None = None) -> object: # typevars for core polars types PolarsType = TypeVar("PolarsType", "DataFrame", "LazyFrame", "Series", "Expr") FrameType = TypeVar("FrameType", "DataFrame", "LazyFrame") -BufferInfo: TypeAlias = Tuple[int, int, int] +BufferInfo: TypeAlias = tuple[int, int, int] # type alias for supported spreadsheet engines ExcelSpreadsheetEngine: TypeAlias = Literal["xlsx2csv", "openpyxl", "calamine"] diff --git a/py-polars/polars/_utils/async_.py b/py-polars/polars/_utils/async_.py index a8fed8facda7..966f28cecdb6 100644 --- a/py-polars/polars/_utils/async_.py +++ b/py-polars/polars/_utils/async_.py @@ -1,12 +1,14 @@ from __future__ import annotations -from typing import TYPE_CHECKING, Any, Awaitable, Generator, Generic, TypeVar +from collections.abc import Awaitable +from typing import TYPE_CHECKING, Any, Generic, TypeVar from polars._utils.wrap import wrap_df from polars.dependencies import _GEVENT_AVAILABLE if TYPE_CHECKING: from asyncio.futures import Future + from collections.abc import Generator from polars.polars import PyDataFrame diff --git a/py-polars/polars/_utils/construction/dataframe.py b/py-polars/polars/_utils/construction/dataframe.py index 1ab05930f5da..f174fbc736cf 100644 --- a/py-polars/polars/_utils/construction/dataframe.py +++ b/py-polars/polars/_utils/construction/dataframe.py @@ -1,6 +1,7 @@ from __future__ import annotations import contextlib +from collections.abc import Generator, Mapping from datetime import date, datetime, time, timedelta from functools import singledispatch from itertools import islice, zip_longest @@ -9,11 +10,6 @@ TYPE_CHECKING, Any, Callable, - Generator, - Iterable, - Mapping, - MutableMapping, - Sequence, ) import polars._reexport as pl @@ -63,6 +59,8 @@ from polars.polars import PyDataFrame if TYPE_CHECKING: + from collections.abc import Iterable, MutableMapping, Sequence + from polars import DataFrame, Expr, Series from polars._typing import ( Orientation, diff --git a/py-polars/polars/_utils/construction/series.py b/py-polars/polars/_utils/construction/series.py index 379bdbeb0a30..121378b6d873 100644 --- a/py-polars/polars/_utils/construction/series.py +++ b/py-polars/polars/_utils/construction/series.py @@ -1,16 +1,13 @@ from __future__ import annotations import contextlib +from collections.abc import Generator, Iterator from datetime import date, datetime, time, timedelta from itertools import islice from typing import ( TYPE_CHECKING, Any, Callable, - Generator, - Iterable, - Iterator, - Sequence, ) import polars._reexport as pl @@ -65,6 +62,8 @@ from polars.polars import PySeries if TYPE_CHECKING: + from collections.abc import Iterable, Sequence + from polars import DataFrame, Series from polars._typing import PolarsDataType from polars.dependencies import pandas as pd diff --git a/py-polars/polars/_utils/construction/utils.py b/py-polars/polars/_utils/construction/utils.py index de214a2dfb15..8b73728c92fc 100644 --- a/py-polars/polars/_utils/construction/utils.py +++ b/py-polars/polars/_utils/construction/utils.py @@ -2,11 +2,13 @@ import sys from functools import lru_cache -from typing import TYPE_CHECKING, Any, Callable, Sequence, get_type_hints +from typing import TYPE_CHECKING, Any, Callable, get_type_hints from polars.dependencies import _check_for_pydantic, pydantic if TYPE_CHECKING: + from collections.abc import Sequence + import pandas as pd PANDAS_SIMPLE_NUMPY_DTYPES = { diff --git a/py-polars/polars/_utils/convert.py b/py-polars/polars/_utils/convert.py index e2f4066c62de..cc23c77f73ac 100644 --- a/py-polars/polars/_utils/convert.py +++ b/py-polars/polars/_utils/convert.py @@ -8,7 +8,6 @@ Any, Callable, NoReturn, - Sequence, no_type_check, overload, ) @@ -26,6 +25,7 @@ from polars.dependencies import _ZONEINFO_AVAILABLE, zoneinfo if TYPE_CHECKING: + from collections.abc import Sequence from datetime import date, tzinfo from decimal import Decimal diff --git a/py-polars/polars/_utils/deprecation.py b/py-polars/polars/_utils/deprecation.py index bf93cff116f7..4a84826bca86 100644 --- a/py-polars/polars/_utils/deprecation.py +++ b/py-polars/polars/_utils/deprecation.py @@ -1,14 +1,15 @@ from __future__ import annotations import inspect +from collections.abc import Sequence from functools import wraps -from typing import TYPE_CHECKING, Callable, Sequence, TypeVar +from typing import TYPE_CHECKING, Callable, TypeVar from polars._utils.various import issue_warning if TYPE_CHECKING: import sys - from typing import Mapping + from collections.abc import Mapping from polars._typing import Ambiguous diff --git a/py-polars/polars/_utils/getitem.py b/py-polars/polars/_utils/getitem.py index 991dba52bb2d..2ab09ba9af8c 100644 --- a/py-polars/polars/_utils/getitem.py +++ b/py-polars/polars/_utils/getitem.py @@ -1,6 +1,7 @@ from __future__ import annotations -from typing import TYPE_CHECKING, Any, Iterable, NoReturn, Sequence, overload +from collections.abc import Sequence +from typing import TYPE_CHECKING, Any, NoReturn, overload import polars._reexport as pl import polars.functions as F @@ -22,6 +23,8 @@ from polars.meta.index_type import get_index_type if TYPE_CHECKING: + from collections.abc import Iterable + from polars import DataFrame, Series from polars._typing import ( MultiColSelector, diff --git a/py-polars/polars/_utils/parse/expr.py b/py-polars/polars/_utils/parse/expr.py index 69e372ae6219..2213f20a1b6f 100644 --- a/py-polars/polars/_utils/parse/expr.py +++ b/py-polars/polars/_utils/parse/expr.py @@ -1,7 +1,8 @@ from __future__ import annotations import contextlib -from typing import TYPE_CHECKING, Any, Iterable +from collections.abc import Iterable +from typing import TYPE_CHECKING, Any import polars._reexport as pl from polars import functions as F diff --git a/py-polars/polars/_utils/udfs.py b/py-polars/polars/_utils/udfs.py index 661c4e9cf303..0ff968ed59ec 100644 --- a/py-polars/polars/_utils/udfs.py +++ b/py-polars/polars/_utils/udfs.py @@ -16,11 +16,9 @@ from pathlib import Path from typing import ( TYPE_CHECKING, - AbstractSet, Any, Callable, ClassVar, - Iterator, Literal, NamedTuple, Union, @@ -29,6 +27,8 @@ from polars._utils.various import re_escape if TYPE_CHECKING: + from collections.abc import Iterator + from collections.abc import Set as AbstractSet from dis import Instruction if sys.version_info >= (3, 10): diff --git a/py-polars/polars/_utils/various.py b/py-polars/polars/_utils/various.py index f25ab95e23c2..4acad1df5237 100644 --- a/py-polars/polars/_utils/various.py +++ b/py-polars/polars/_utils/various.py @@ -5,18 +5,21 @@ import re import sys import warnings -from collections.abc import MappingView, Sized +from collections.abc import ( + Collection, + Generator, + Iterable, + MappingView, + Sequence, + Sized, +) from enum import Enum from pathlib import Path from typing import ( TYPE_CHECKING, Any, Callable, - Collection, - Generator, - Iterable, Literal, - Sequence, TypeVar, overload, ) diff --git a/py-polars/polars/convert/general.py b/py-polars/polars/convert/general.py index 011054169d04..cee4b925e9e9 100644 --- a/py-polars/polars/convert/general.py +++ b/py-polars/polars/convert/general.py @@ -3,7 +3,8 @@ import io import itertools import re -from typing import TYPE_CHECKING, Any, Iterable, Mapping, Sequence, overload +from collections.abc import Iterable, Sequence +from typing import TYPE_CHECKING, Any, overload import polars._reexport as pl from polars import functions as F @@ -24,6 +25,8 @@ from polars.exceptions import NoDataError if TYPE_CHECKING: + from collections.abc import Mapping + from polars import DataFrame, Series from polars._typing import Orientation, SchemaDefinition, SchemaDict from polars.dependencies import numpy as np diff --git a/py-polars/polars/convert/normalize.py b/py-polars/polars/convert/normalize.py index 7f90260aaac4..d6e24ddc8af5 100644 --- a/py-polars/polars/convert/normalize.py +++ b/py-polars/polars/convert/normalize.py @@ -4,27 +4,20 @@ import json from collections import abc -from typing import TYPE_CHECKING, Any, Sequence +from typing import TYPE_CHECKING, Any from polars._utils.unstable import unstable from polars.dataframe import DataFrame from polars.datatypes.constants import N_INFER_DEFAULT if TYPE_CHECKING: - from polars.schema import Schema - -import sys + from collections.abc import Sequence -if sys.version_info >= (3, 9): + from polars.schema import Schema - def _remove_prefix(text: str, prefix: str) -> str: - return text.removeprefix(prefix) -else: - def _remove_prefix(text: str, prefix: str) -> str: - if text.startswith(prefix): - return text[len(prefix) :] - return text +def _remove_prefix(text: str, prefix: str) -> str: + return text.removeprefix(prefix) def _simple_json_normalize( diff --git a/py-polars/polars/dataframe/_html.py b/py-polars/polars/dataframe/_html.py index 62464873e42a..6f034eab0f41 100644 --- a/py-polars/polars/dataframe/_html.py +++ b/py-polars/polars/dataframe/_html.py @@ -4,11 +4,12 @@ import os from textwrap import dedent -from typing import TYPE_CHECKING, Iterable +from typing import TYPE_CHECKING from polars.dependencies import html if TYPE_CHECKING: + from collections.abc import Iterable from types import TracebackType from polars import DataFrame diff --git a/py-polars/polars/dataframe/frame.py b/py-polars/polars/dataframe/frame.py index 7fafdd058735..911d057a1f46 100644 --- a/py-polars/polars/dataframe/frame.py +++ b/py-polars/polars/dataframe/frame.py @@ -6,7 +6,12 @@ import os import random from collections import defaultdict -from collections.abc import Sized +from collections.abc import ( + Generator, + Iterable, + Sequence, + Sized, +) from io import BytesIO, StringIO from operator import itemgetter from pathlib import Path @@ -16,13 +21,7 @@ Any, Callable, ClassVar, - Collection, - Generator, - Iterable, - Iterator, - Mapping, NoReturn, - Sequence, TypeVar, get_args, overload, @@ -115,6 +114,11 @@ if TYPE_CHECKING: import sys + from collections.abc import ( + Collection, + Iterator, + Mapping, + ) from datetime import timedelta from io import IOBase from typing import Literal @@ -3973,8 +3977,9 @@ def unpack_table_name(name: str) -> tuple[str | None, str | None, str]: else (connection, False) ) with ( - conn if can_close_conn else contextlib.nullcontext() - ), conn.cursor() as cursor: + conn if can_close_conn else contextlib.nullcontext(), + conn.cursor() as cursor, + ): catalog, db_schema, unpacked_table_name = unpack_table_name(table_name) n_rows: int if adbc_version >= (0, 7): diff --git a/py-polars/polars/dataframe/group_by.py b/py-polars/polars/dataframe/group_by.py index 204afc3d25c1..ce3cf054a7a0 100644 --- a/py-polars/polars/dataframe/group_by.py +++ b/py-polars/polars/dataframe/group_by.py @@ -1,6 +1,6 @@ from __future__ import annotations -from typing import TYPE_CHECKING, Callable, Iterable +from typing import TYPE_CHECKING, Callable from polars import functions as F from polars._utils.convert import parse_as_duration_string @@ -8,6 +8,7 @@ if TYPE_CHECKING: import sys + from collections.abc import Iterable from datetime import timedelta from polars import DataFrame diff --git a/py-polars/polars/dataframe/plotting.py b/py-polars/polars/dataframe/plotting.py index 75a2b92aa09e..11828cd54ebc 100644 --- a/py-polars/polars/dataframe/plotting.py +++ b/py-polars/polars/dataframe/plotting.py @@ -1,6 +1,6 @@ from __future__ import annotations -from typing import TYPE_CHECKING, Callable, Dict, Union +from typing import TYPE_CHECKING, Callable, Union from polars.dependencies import altair as alt @@ -27,7 +27,7 @@ from typing_extensions import Unpack Encoding: TypeAlias = Union[X, Y, Color, Order, Size, Tooltip] - Encodings: TypeAlias = Dict[str, Encoding] + Encodings: TypeAlias = dict[str, Encoding] def _maybe_extract_shorthand(encoding: Encoding) -> Encoding: diff --git a/py-polars/polars/datatypes/classes.py b/py-polars/polars/datatypes/classes.py index 8906f2459c28..bb538b4f01e8 100644 --- a/py-polars/polars/datatypes/classes.py +++ b/py-polars/polars/datatypes/classes.py @@ -2,9 +2,10 @@ import contextlib from collections import OrderedDict +from collections.abc import Mapping from datetime import timezone from inspect import isclass -from typing import TYPE_CHECKING, Any, Iterable, Iterator, Mapping, Sequence +from typing import TYPE_CHECKING, Any import polars._reexport as pl import polars.datatypes @@ -14,6 +15,8 @@ from polars.polars import dtype_str_repr as _dtype_str_repr if TYPE_CHECKING: + from collections.abc import Iterable, Iterator, Sequence + from polars import Series from polars._typing import ( CategoricalOrdering, diff --git a/py-polars/polars/datatypes/constructor.py b/py-polars/polars/datatypes/constructor.py index 63e0d912cbf8..bcd77f9b54a0 100644 --- a/py-polars/polars/datatypes/constructor.py +++ b/py-polars/polars/datatypes/constructor.py @@ -2,7 +2,7 @@ import functools from decimal import Decimal as PyDecimal -from typing import TYPE_CHECKING, Any, Callable, Sequence +from typing import TYPE_CHECKING, Any, Callable from polars import datatypes as dt from polars.dependencies import numpy as np @@ -16,6 +16,8 @@ _DOCUMENTING = True if TYPE_CHECKING: + from collections.abc import Sequence + from polars._typing import PolarsDataType if not _DOCUMENTING: diff --git a/py-polars/polars/datatypes/convert.py b/py-polars/polars/datatypes/convert.py index 91fe61da4b6f..d46d8c111581 100644 --- a/py-polars/polars/datatypes/convert.py +++ b/py-polars/polars/datatypes/convert.py @@ -4,9 +4,10 @@ import functools import re import sys +from collections.abc import Collection from datetime import date, datetime, time, timedelta from decimal import Decimal as PyDecimal -from typing import TYPE_CHECKING, Any, Collection, Optional, Union +from typing import TYPE_CHECKING, Any, Optional, Union from polars.datatypes.classes import ( Array, diff --git a/py-polars/polars/datatypes/group.py b/py-polars/polars/datatypes/group.py index f30153ed6e7a..3332dd4a7e7f 100644 --- a/py-polars/polars/datatypes/group.py +++ b/py-polars/polars/datatypes/group.py @@ -1,6 +1,6 @@ from __future__ import annotations -from typing import TYPE_CHECKING, Any, Iterable +from typing import TYPE_CHECKING, Any from polars.datatypes.classes import ( Array, @@ -27,6 +27,7 @@ if TYPE_CHECKING: import sys + from collections.abc import Iterable from polars._typing import ( PolarsDataType, diff --git a/py-polars/polars/dependencies.py b/py-polars/polars/dependencies.py index 10548da8c904..9770c2035ce8 100644 --- a/py-polars/polars/dependencies.py +++ b/py-polars/polars/dependencies.py @@ -2,11 +2,12 @@ import re import sys -from functools import lru_cache +from collections.abc import Hashable +from functools import cache from importlib import import_module from importlib.util import find_spec from types import ModuleType -from typing import TYPE_CHECKING, Any, ClassVar, Hashable, cast +from typing import TYPE_CHECKING, Any, ClassVar, cast _ALTAIR_AVAILABLE = True _DELTALAKE_AVAILABLE = True @@ -149,6 +150,7 @@ def _lazy_import(module_name: str) -> tuple[ModuleType, bool]: import json import pickle import subprocess + import zoneinfo import altair import deltalake @@ -161,11 +163,6 @@ def _lazy_import(module_name: str) -> tuple[ModuleType, bool]: import pyarrow import pydantic import pyiceberg - - if sys.version_info >= (3, 9): - import zoneinfo - else: - from backports import zoneinfo else: # infrequently-used builtins dataclasses, _ = _lazy_import("dataclasses") @@ -193,7 +190,7 @@ def _lazy_import(module_name: str) -> tuple[ModuleType, bool]: gevent, _GEVENT_AVAILABLE = _lazy_import("gevent") -@lru_cache(maxsize=None) +@cache def _might_be(cls: type, type_: str) -> bool: # infer whether the given class "might" be associated with the given # module (in which case it's reasonable to do a real isinstance check; diff --git a/py-polars/polars/expr/array.py b/py-polars/polars/expr/array.py index 302b0b024adf..928e3149c35d 100644 --- a/py-polars/polars/expr/array.py +++ b/py-polars/polars/expr/array.py @@ -1,6 +1,7 @@ from __future__ import annotations -from typing import TYPE_CHECKING, Callable, Sequence +from collections.abc import Sequence +from typing import TYPE_CHECKING, Callable from polars._utils.parse import parse_into_expression from polars._utils.wrap import wrap_expr diff --git a/py-polars/polars/expr/datetime.py b/py-polars/polars/expr/datetime.py index a35674412ccf..9aaed1352d09 100644 --- a/py-polars/polars/expr/datetime.py +++ b/py-polars/polars/expr/datetime.py @@ -1,7 +1,7 @@ from __future__ import annotations import datetime as dt -from typing import TYPE_CHECKING, Iterable +from typing import TYPE_CHECKING import polars._reexport as pl from polars import functions as F @@ -13,6 +13,8 @@ from polars.datatypes import DTYPE_TEMPORAL_UNITS, Date, Int32 if TYPE_CHECKING: + from collections.abc import Iterable + from polars import Expr from polars._typing import ( Ambiguous, diff --git a/py-polars/polars/expr/expr.py b/py-polars/polars/expr/expr.py index 1d3b2a07fdcb..341f99876b21 100644 --- a/py-polars/polars/expr/expr.py +++ b/py-polars/polars/expr/expr.py @@ -4,6 +4,7 @@ import math import operator import warnings +from collections.abc import Collection, Mapping, Sequence from datetime import timedelta from functools import reduce from io import BytesIO, StringIO @@ -13,13 +14,7 @@ Any, Callable, ClassVar, - Collection, - FrozenSet, - Iterable, - Mapping, NoReturn, - Sequence, - Set, TypeVar, ) @@ -69,6 +64,7 @@ if TYPE_CHECKING: import sys + from collections.abc import Iterable from io import IOBase from polars import DataFrame, LazyFrame, Series @@ -5771,7 +5767,7 @@ def is_in(self, other: Expr | Collection[Any] | Series) -> Expr: └───────────┴──────────────────┴──────────┘ """ if isinstance(other, Collection) and not isinstance(other, str): - if isinstance(other, (Set, FrozenSet)): + if isinstance(other, (set, frozenset)): other = list(other) other = F.lit(pl.Series(other))._pyexpr else: diff --git a/py-polars/polars/expr/list.py b/py-polars/polars/expr/list.py index 5560f368767b..48b4d1da9c49 100644 --- a/py-polars/polars/expr/list.py +++ b/py-polars/polars/expr/list.py @@ -1,7 +1,8 @@ from __future__ import annotations import copy -from typing import TYPE_CHECKING, Any, Callable, Sequence +from collections.abc import Sequence +from typing import TYPE_CHECKING, Any, Callable import polars._reexport as pl from polars import functions as F diff --git a/py-polars/polars/expr/string.py b/py-polars/polars/expr/string.py index 1b04ab7febad..7582758d5921 100644 --- a/py-polars/polars/expr/string.py +++ b/py-polars/polars/expr/string.py @@ -1,7 +1,8 @@ from __future__ import annotations import warnings -from typing import TYPE_CHECKING, Mapping +from collections.abc import Mapping +from typing import TYPE_CHECKING import polars._reexport as pl from polars import functions as F diff --git a/py-polars/polars/expr/struct.py b/py-polars/polars/expr/struct.py index 09b9c1688fbb..57b8b6eddfb3 100644 --- a/py-polars/polars/expr/struct.py +++ b/py-polars/polars/expr/struct.py @@ -1,12 +1,14 @@ from __future__ import annotations import os -from typing import TYPE_CHECKING, Iterable, Sequence +from typing import TYPE_CHECKING from polars._utils.parse import parse_into_list_of_expressions from polars._utils.wrap import wrap_expr if TYPE_CHECKING: + from collections.abc import Iterable, Sequence + from polars import Expr from polars._typing import IntoExpr diff --git a/py-polars/polars/expr/whenthen.py b/py-polars/polars/expr/whenthen.py index 752ed1b44c06..65ad722f2cce 100644 --- a/py-polars/polars/expr/whenthen.py +++ b/py-polars/polars/expr/whenthen.py @@ -1,6 +1,6 @@ from __future__ import annotations -from typing import TYPE_CHECKING, Any, Iterable +from typing import TYPE_CHECKING, Any import polars.functions as F from polars._utils.parse import ( @@ -11,6 +11,8 @@ from polars.expr.expr import Expr if TYPE_CHECKING: + from collections.abc import Iterable + from polars._typing import IntoExpr from polars.polars import PyExpr diff --git a/py-polars/polars/functions/aggregation/horizontal.py b/py-polars/polars/functions/aggregation/horizontal.py index 121f1bb41497..5406a77d287d 100644 --- a/py-polars/polars/functions/aggregation/horizontal.py +++ b/py-polars/polars/functions/aggregation/horizontal.py @@ -1,7 +1,7 @@ from __future__ import annotations import contextlib -from typing import TYPE_CHECKING, Iterable +from typing import TYPE_CHECKING import polars.functions as F from polars._utils.parse import parse_into_list_of_expressions @@ -12,6 +12,8 @@ import polars.polars as plr if TYPE_CHECKING: + from collections.abc import Iterable + from polars import Expr from polars._typing import IntoExpr diff --git a/py-polars/polars/functions/as_datatype.py b/py-polars/polars/functions/as_datatype.py index 5937e5c5b091..30398daa01d9 100644 --- a/py-polars/polars/functions/as_datatype.py +++ b/py-polars/polars/functions/as_datatype.py @@ -1,7 +1,7 @@ from __future__ import annotations import contextlib -from typing import TYPE_CHECKING, Iterable, overload +from typing import TYPE_CHECKING, overload from polars import functions as F from polars._utils.parse import ( @@ -16,6 +16,7 @@ if TYPE_CHECKING: + from collections.abc import Iterable from typing import Literal from polars import Expr, Series diff --git a/py-polars/polars/functions/business.py b/py-polars/polars/functions/business.py index dc70a4ba3679..2f92c40e987e 100644 --- a/py-polars/polars/functions/business.py +++ b/py-polars/polars/functions/business.py @@ -2,7 +2,7 @@ import contextlib from datetime import date -from typing import TYPE_CHECKING, Iterable +from typing import TYPE_CHECKING from polars._utils.parse import parse_into_expression from polars._utils.wrap import wrap_expr @@ -11,6 +11,8 @@ import polars.polars as plr if TYPE_CHECKING: + from collections.abc import Iterable + from polars import Expr from polars._typing import IntoExprColumn diff --git a/py-polars/polars/functions/col.py b/py-polars/polars/functions/col.py index 01cca3c4fe21..b354cd878176 100644 --- a/py-polars/polars/functions/col.py +++ b/py-polars/polars/functions/col.py @@ -1,7 +1,8 @@ from __future__ import annotations import contextlib -from typing import TYPE_CHECKING, Iterable +from collections.abc import Iterable +from typing import TYPE_CHECKING from polars._utils.wrap import wrap_expr from polars.datatypes import is_polars_dtype diff --git a/py-polars/polars/functions/eager.py b/py-polars/polars/functions/eager.py index e8cbb00e3dca..e65f4aa949c0 100644 --- a/py-polars/polars/functions/eager.py +++ b/py-polars/polars/functions/eager.py @@ -1,9 +1,10 @@ from __future__ import annotations import contextlib +from collections.abc import Sequence from functools import reduce from itertools import chain -from typing import TYPE_CHECKING, Iterable, Sequence, get_args +from typing import TYPE_CHECKING, get_args import polars._reexport as pl from polars import functions as F @@ -16,6 +17,8 @@ import polars.polars as plr if TYPE_CHECKING: + from collections.abc import Iterable + from polars import DataFrame, Expr, LazyFrame, Series from polars._typing import FrameType, JoinStrategy, PolarsType diff --git a/py-polars/polars/functions/lazy.py b/py-polars/polars/functions/lazy.py index c78996e07753..61cead9871d8 100644 --- a/py-polars/polars/functions/lazy.py +++ b/py-polars/polars/functions/lazy.py @@ -1,7 +1,8 @@ from __future__ import annotations import contextlib -from typing import TYPE_CHECKING, Any, Callable, Iterable, Sequence, overload +from collections.abc import Sequence +from typing import TYPE_CHECKING, Any, Callable, overload import polars._reexport as pl import polars.functions as F @@ -20,7 +21,8 @@ import polars.polars as plr if TYPE_CHECKING: - from typing import Awaitable, Collection, Literal + from collections.abc import Awaitable, Collection, Iterable + from typing import Literal from polars import DataFrame, Expr, LazyFrame, Series from polars._typing import ( diff --git a/py-polars/polars/functions/whenthen.py b/py-polars/polars/functions/whenthen.py index e72708d311e7..b9d9040cceb3 100644 --- a/py-polars/polars/functions/whenthen.py +++ b/py-polars/polars/functions/whenthen.py @@ -1,7 +1,7 @@ from __future__ import annotations import contextlib -from typing import TYPE_CHECKING, Any, Iterable +from typing import TYPE_CHECKING, Any import polars._reexport as pl from polars._utils.parse import parse_predicates_constraints_into_expression @@ -10,6 +10,8 @@ import polars.polars as plr if TYPE_CHECKING: + from collections.abc import Iterable + from polars._typing import IntoExprColumn diff --git a/py-polars/polars/interchange/protocol.py b/py-polars/polars/interchange/protocol.py index 4eda7fa95f2d..4865baa35ae6 100644 --- a/py-polars/polars/interchange/protocol.py +++ b/py-polars/polars/interchange/protocol.py @@ -5,11 +5,8 @@ TYPE_CHECKING, Any, ClassVar, - Iterable, Literal, Protocol, - Sequence, - Tuple, TypedDict, ) @@ -17,6 +14,7 @@ if TYPE_CHECKING: import sys + from collections.abc import Iterable, Sequence from polars.interchange.buffer import PolarsBuffer from polars.interchange.column import PolarsColumn @@ -71,7 +69,7 @@ class DtypeKind(IntEnum): CATEGORICAL = 23 -Dtype: TypeAlias = Tuple[DtypeKind, int, str, str] # see Column.dtype +Dtype: TypeAlias = tuple[DtypeKind, int, str, str] # see Column.dtype class ColumnNullType(IntEnum): diff --git a/py-polars/polars/io/_utils.py b/py-polars/polars/io/_utils.py index e8971b08660d..68d4b604d6a6 100644 --- a/py-polars/polars/io/_utils.py +++ b/py-polars/polars/io/_utils.py @@ -5,12 +5,16 @@ from contextlib import contextmanager from io import BytesIO, StringIO from pathlib import Path -from typing import IO, Any, ContextManager, Iterator, Sequence, overload +from typing import IO, TYPE_CHECKING, Any, overload from polars._utils.various import is_int_sequence, is_str_sequence, normalize_filepath from polars.dependencies import _FSSPEC_AVAILABLE, fsspec from polars.exceptions import NoDataError +if TYPE_CHECKING: + from collections.abc import Iterator, Sequence + from contextlib import AbstractContextManager as ContextManager + def parse_columns_arg( columns: Sequence[str] | Sequence[int] | str | int | None, diff --git a/py-polars/polars/io/csv/_utils.py b/py-polars/polars/io/csv/_utils.py index b4bbb055c3a9..bdc910de5aa4 100644 --- a/py-polars/polars/io/csv/_utils.py +++ b/py-polars/polars/io/csv/_utils.py @@ -1,8 +1,10 @@ from __future__ import annotations -from typing import TYPE_CHECKING, Sequence +from typing import TYPE_CHECKING if TYPE_CHECKING: + from collections.abc import Sequence + from polars import DataFrame diff --git a/py-polars/polars/io/csv/batched_reader.py b/py-polars/polars/io/csv/batched_reader.py index 2bbb1583317c..6866a0b79723 100644 --- a/py-polars/polars/io/csv/batched_reader.py +++ b/py-polars/polars/io/csv/batched_reader.py @@ -1,7 +1,8 @@ from __future__ import annotations import contextlib -from typing import TYPE_CHECKING, Sequence +from collections.abc import Sequence +from typing import TYPE_CHECKING from polars._utils.various import ( _process_null_values, diff --git a/py-polars/polars/io/csv/functions.py b/py-polars/polars/io/csv/functions.py index 71f71ef6ce68..2e0562ceaee7 100644 --- a/py-polars/polars/io/csv/functions.py +++ b/py-polars/polars/io/csv/functions.py @@ -2,9 +2,10 @@ import contextlib import os +from collections.abc import Sequence from io import BytesIO, StringIO from pathlib import Path -from typing import IO, TYPE_CHECKING, Any, Callable, Mapping, Sequence +from typing import IO, TYPE_CHECKING, Any, Callable import polars._reexport as pl import polars.functions as F @@ -30,6 +31,8 @@ from polars.polars import PyDataFrame, PyLazyFrame if TYPE_CHECKING: + from collections.abc import Mapping + from polars import DataFrame, LazyFrame from polars._typing import CsvEncoding, PolarsDataType, SchemaDict diff --git a/py-polars/polars/io/database/_cursor_proxies.py b/py-polars/polars/io/database/_cursor_proxies.py index 129f7609759a..d25abc3acce5 100644 --- a/py-polars/polars/io/database/_cursor_proxies.py +++ b/py-polars/polars/io/database/_cursor_proxies.py @@ -1,12 +1,12 @@ from __future__ import annotations -from typing import TYPE_CHECKING, Any, Iterable +from typing import TYPE_CHECKING, Any from polars.io.database._utils import _run_async if TYPE_CHECKING: import sys - from collections.abc import Coroutine + from collections.abc import Coroutine, Iterable import pyarrow as pa diff --git a/py-polars/polars/io/database/_executor.py b/py-polars/polars/io/database/_executor.py index b45301b87da2..278e3e8e0738 100644 --- a/py-polars/polars/io/database/_executor.py +++ b/py-polars/polars/io/database/_executor.py @@ -1,10 +1,10 @@ from __future__ import annotations import re -from collections.abc import Coroutine +from collections.abc import Coroutine, Sequence from contextlib import suppress from inspect import Parameter, signature -from typing import TYPE_CHECKING, Any, Iterable, Sequence +from typing import TYPE_CHECKING, Any from polars import functions as F from polars._utils.various import parse_version @@ -22,7 +22,7 @@ if TYPE_CHECKING: import sys - from collections.abc import Iterator + from collections.abc import Iterable, Iterator from types import TracebackType import pyarrow as pa diff --git a/py-polars/polars/io/database/functions.py b/py-polars/polars/io/database/functions.py index 098dabfdd90f..ac5dcaaac3e7 100644 --- a/py-polars/polars/io/database/functions.py +++ b/py-polars/polars/io/database/functions.py @@ -1,7 +1,7 @@ from __future__ import annotations import re -from typing import TYPE_CHECKING, Any, Iterable, Literal, overload +from typing import TYPE_CHECKING, Any, Literal, overload from polars.datatypes import N_INFER_DEFAULT from polars.dependencies import import_optional @@ -10,6 +10,7 @@ if TYPE_CHECKING: import sys + from collections.abc import Iterable if sys.version_info >= (3, 10): from typing import TypeAlias diff --git a/py-polars/polars/io/ipc/functions.py b/py-polars/polars/io/ipc/functions.py index 43fbc8136de2..6d64a560d094 100644 --- a/py-polars/polars/io/ipc/functions.py +++ b/py-polars/polars/io/ipc/functions.py @@ -3,7 +3,7 @@ import contextlib import os from pathlib import Path -from typing import IO, TYPE_CHECKING, Any, Sequence +from typing import IO, TYPE_CHECKING, Any import polars._reexport as pl import polars.functions as F @@ -28,6 +28,8 @@ from polars.polars import read_ipc_schema as _read_ipc_schema if TYPE_CHECKING: + from collections.abc import Sequence + from polars import DataFrame, DataType, LazyFrame from polars._typing import SchemaDict diff --git a/py-polars/polars/io/ndjson.py b/py-polars/polars/io/ndjson.py index 33e85914ce34..7a5fb2c0d1e6 100644 --- a/py-polars/polars/io/ndjson.py +++ b/py-polars/polars/io/ndjson.py @@ -1,9 +1,10 @@ from __future__ import annotations import contextlib +from collections.abc import Sequence from io import BytesIO, StringIO from pathlib import Path -from typing import IO, TYPE_CHECKING, Any, Sequence +from typing import IO, TYPE_CHECKING, Any from polars._utils.deprecation import deprecate_renamed_parameter from polars._utils.various import is_path_or_str_sequence, normalize_filepath diff --git a/py-polars/polars/io/plugins.py b/py-polars/polars/io/plugins.py index 02f598515c1e..8c9e6581bf7e 100644 --- a/py-polars/polars/io/plugins.py +++ b/py-polars/polars/io/plugins.py @@ -2,13 +2,15 @@ import os import sys -from typing import TYPE_CHECKING, Callable, Iterator +from collections.abc import Iterator +from typing import TYPE_CHECKING, Callable import polars._reexport as pl from polars._utils.unstable import unstable if TYPE_CHECKING: - from typing import Callable, Iterator + from collections.abc import Iterator + from typing import Callable from polars import DataFrame, Expr, LazyFrame from polars._typing import SchemaDict diff --git a/py-polars/polars/io/spreadsheet/_utils.py b/py-polars/polars/io/spreadsheet/_utils.py index c7f647c9b01d..c535662fa33b 100644 --- a/py-polars/polars/io/spreadsheet/_utils.py +++ b/py-polars/polars/io/spreadsheet/_utils.py @@ -2,7 +2,10 @@ from contextlib import contextmanager from pathlib import Path -from typing import Any, Iterator, cast +from typing import TYPE_CHECKING, Any, cast + +if TYPE_CHECKING: + from collections.abc import Iterator @contextmanager diff --git a/py-polars/polars/io/spreadsheet/_write_utils.py b/py-polars/polars/io/spreadsheet/_write_utils.py index ba4032f5293f..8489b359a416 100644 --- a/py-polars/polars/io/spreadsheet/_write_utils.py +++ b/py-polars/polars/io/spreadsheet/_write_utils.py @@ -1,8 +1,9 @@ from __future__ import annotations +from collections.abc import Sequence from io import BytesIO from pathlib import Path -from typing import TYPE_CHECKING, Any, Iterable, Sequence, overload +from typing import TYPE_CHECKING, Any, overload from polars import functions as F from polars.datatypes import ( @@ -21,6 +22,7 @@ from polars.selectors import _expand_selector_dicts, _expand_selectors, numeric if TYPE_CHECKING: + from collections.abc import Iterable from typing import Literal from xlsxwriter import Workbook diff --git a/py-polars/polars/io/spreadsheet/functions.py b/py-polars/polars/io/spreadsheet/functions.py index dd445f9e9c97..2b04fc1ac4dc 100644 --- a/py-polars/polars/io/spreadsheet/functions.py +++ b/py-polars/polars/io/spreadsheet/functions.py @@ -1,10 +1,11 @@ from __future__ import annotations import re +from collections.abc import Sequence from datetime import time from io import BufferedReader, BytesIO, StringIO, TextIOWrapper from pathlib import Path -from typing import IO, TYPE_CHECKING, Any, Callable, NoReturn, Sequence, overload +from typing import IO, TYPE_CHECKING, Any, Callable, NoReturn, overload import polars._reexport as pl from polars import from_arrow diff --git a/py-polars/polars/lazyframe/frame.py b/py-polars/polars/lazyframe/frame.py index 60e8b803421c..9e65f918d385 100644 --- a/py-polars/polars/lazyframe/frame.py +++ b/py-polars/polars/lazyframe/frame.py @@ -3,6 +3,7 @@ import contextlib import os import warnings +from collections.abc import Collection, Mapping from datetime import date, datetime, time, timedelta from functools import lru_cache, partial, reduce from io import BytesIO, StringIO @@ -13,11 +14,7 @@ Any, Callable, ClassVar, - Collection, - Iterable, - Mapping, NoReturn, - Sequence, TypeVar, overload, ) @@ -91,8 +88,9 @@ if TYPE_CHECKING: import sys + from collections.abc import Awaitable, Iterable, Sequence from io import IOBase - from typing import Awaitable, Literal + from typing import Literal import pyarrow as pa diff --git a/py-polars/polars/lazyframe/group_by.py b/py-polars/polars/lazyframe/group_by.py index 8a469e8b1e5d..1143ea9c4831 100644 --- a/py-polars/polars/lazyframe/group_by.py +++ b/py-polars/polars/lazyframe/group_by.py @@ -1,6 +1,6 @@ from __future__ import annotations -from typing import TYPE_CHECKING, Callable, Iterable +from typing import TYPE_CHECKING, Callable from polars import functions as F from polars._utils.deprecation import deprecate_renamed_function @@ -8,6 +8,8 @@ from polars._utils.wrap import wrap_ldf if TYPE_CHECKING: + from collections.abc import Iterable + from polars import DataFrame, LazyFrame from polars._typing import IntoExpr, RollingInterpolationMethod, SchemaDict from polars.polars import PyLazyGroupBy diff --git a/py-polars/polars/ml/torch.py b/py-polars/polars/ml/torch.py index 134e1c260e63..f92f2ab30fe9 100644 --- a/py-polars/polars/ml/torch.py +++ b/py-polars/polars/ml/torch.py @@ -1,7 +1,7 @@ # mypy: disable-error-code="unused-ignore" from __future__ import annotations -from typing import TYPE_CHECKING, Sequence +from typing import TYPE_CHECKING from polars._utils.unstable import issue_unstable_warning from polars.dataframe import DataFrame @@ -10,6 +10,7 @@ if TYPE_CHECKING: import sys + from collections.abc import Sequence from torch import Tensor, memory_format diff --git a/py-polars/polars/plugins.py b/py-polars/polars/plugins.py index e295b87a63a6..a501f8b5ed09 100644 --- a/py-polars/polars/plugins.py +++ b/py-polars/polars/plugins.py @@ -3,7 +3,7 @@ import contextlib from functools import lru_cache from pathlib import Path -from typing import TYPE_CHECKING, Any, Iterable +from typing import TYPE_CHECKING, Any from polars._utils.parse import parse_into_list_of_expressions from polars._utils.wrap import wrap_expr @@ -12,6 +12,8 @@ import polars.polars as plr if TYPE_CHECKING: + from collections.abc import Iterable + from polars import Expr from polars._typing import IntoExpr diff --git a/py-polars/polars/schema.py b/py-polars/polars/schema.py index a099b67aac2c..72eb8b86d25e 100644 --- a/py-polars/polars/schema.py +++ b/py-polars/polars/schema.py @@ -2,18 +2,18 @@ from collections import OrderedDict from collections.abc import Mapping -from typing import TYPE_CHECKING, Iterable +from typing import TYPE_CHECKING +from polars.datatypes import DataType from polars.datatypes._parse import parse_into_dtype +BaseSchema = OrderedDict[str, DataType] + if TYPE_CHECKING: + from collections.abc import Iterable + from polars._typing import PythonDataType - from polars.datatypes import DataType - BaseSchema = OrderedDict[str, DataType] -else: - # Python 3.8 does not support generic OrderedDict at runtime - BaseSchema = OrderedDict __all__ = ["Schema"] diff --git a/py-polars/polars/selectors.py b/py-polars/polars/selectors.py index 36b26068a77f..2631f222612a 100644 --- a/py-polars/polars/selectors.py +++ b/py-polars/polars/selectors.py @@ -1,16 +1,14 @@ from __future__ import annotations +from collections.abc import Collection, Mapping, Sequence from datetime import timezone from functools import reduce from operator import or_ from typing import ( TYPE_CHECKING, Any, - Collection, Literal, - Mapping, NoReturn, - Sequence, overload, ) diff --git a/py-polars/polars/series/array.py b/py-polars/polars/series/array.py index 3051228ca941..877ec303fcfb 100644 --- a/py-polars/polars/series/array.py +++ b/py-polars/polars/series/array.py @@ -1,12 +1,13 @@ from __future__ import annotations -from typing import TYPE_CHECKING, Callable, Sequence +from typing import TYPE_CHECKING, Callable from polars import functions as F from polars._utils.wrap import wrap_s from polars.series.utils import expr_dispatch if TYPE_CHECKING: + from collections.abc import Sequence from datetime import date, datetime, time from polars import Series diff --git a/py-polars/polars/series/datetime.py b/py-polars/polars/series/datetime.py index e4da43de78c7..dcf65ff15312 100644 --- a/py-polars/polars/series/datetime.py +++ b/py-polars/polars/series/datetime.py @@ -1,6 +1,6 @@ from __future__ import annotations -from typing import TYPE_CHECKING, Iterable +from typing import TYPE_CHECKING from polars._utils.deprecation import deprecate_function from polars._utils.unstable import unstable @@ -9,6 +9,7 @@ if TYPE_CHECKING: import datetime as dt + from collections.abc import Iterable from polars import Expr, Series from polars._typing import ( diff --git a/py-polars/polars/series/list.py b/py-polars/polars/series/list.py index c6480e3ddbff..cf70f5225f56 100644 --- a/py-polars/polars/series/list.py +++ b/py-polars/polars/series/list.py @@ -1,12 +1,13 @@ from __future__ import annotations -from typing import TYPE_CHECKING, Any, Callable, Sequence +from typing import TYPE_CHECKING, Any, Callable from polars import functions as F from polars._utils.wrap import wrap_s from polars.series.utils import expr_dispatch if TYPE_CHECKING: + from collections.abc import Sequence from datetime import date, datetime, time from polars import Expr, Series diff --git a/py-polars/polars/series/series.py b/py-polars/polars/series/series.py index 506c52ed9f7a..c66d1f0a4abc 100644 --- a/py-polars/polars/series/series.py +++ b/py-polars/polars/series/series.py @@ -3,6 +3,7 @@ import contextlib import math import os +from collections.abc import Iterable, Sequence from contextlib import nullcontext from datetime import date, datetime, time, timedelta from decimal import Decimal as PyDecimal @@ -11,13 +12,8 @@ Any, Callable, ClassVar, - Collection, - Generator, - Iterable, Literal, - Mapping, NoReturn, - Sequence, Union, overload, ) @@ -114,6 +110,7 @@ if TYPE_CHECKING: import sys + from collections.abc import Collection, Generator, Mapping import jax import numpy.typing as npt diff --git a/py-polars/polars/series/string.py b/py-polars/polars/series/string.py index e2104f67af44..af9ce66850c4 100644 --- a/py-polars/polars/series/string.py +++ b/py-polars/polars/series/string.py @@ -1,6 +1,6 @@ from __future__ import annotations -from typing import TYPE_CHECKING, Mapping +from typing import TYPE_CHECKING from polars._utils.deprecation import deprecate_function from polars._utils.unstable import unstable @@ -9,6 +9,8 @@ from polars.series.utils import expr_dispatch if TYPE_CHECKING: + from collections.abc import Mapping + from polars import Expr, Series from polars._typing import ( Ambiguous, diff --git a/py-polars/polars/series/struct.py b/py-polars/polars/series/struct.py index 750ccfd51275..e8137a23be32 100644 --- a/py-polars/polars/series/struct.py +++ b/py-polars/polars/series/struct.py @@ -1,6 +1,6 @@ from __future__ import annotations -from typing import TYPE_CHECKING, Sequence +from typing import TYPE_CHECKING from polars._utils.various import BUILDING_SPHINX_DOCS, sphinx_accessor from polars._utils.wrap import wrap_df @@ -8,6 +8,8 @@ from polars.series.utils import expr_dispatch if TYPE_CHECKING: + from collections.abc import Sequence + from polars import DataFrame, Series from polars.polars import PySeries elif BUILDING_SPHINX_DOCS: diff --git a/py-polars/polars/sql/context.py b/py-polars/polars/sql/context.py index 85101e65ed4b..c48290e547c4 100644 --- a/py-polars/polars/sql/context.py +++ b/py-polars/polars/sql/context.py @@ -5,9 +5,7 @@ from typing import ( TYPE_CHECKING, Callable, - Collection, Generic, - Mapping, Union, overload, ) @@ -30,6 +28,7 @@ if TYPE_CHECKING: import sys + from collections.abc import Collection, Mapping from types import TracebackType from typing import Any, Final, Literal diff --git a/py-polars/polars/testing/parametric/strategies/core.py b/py-polars/polars/testing/parametric/strategies/core.py index b3aef2e51301..984853afe057 100644 --- a/py-polars/polars/testing/parametric/strategies/core.py +++ b/py-polars/polars/testing/parametric/strategies/core.py @@ -1,7 +1,8 @@ from __future__ import annotations +from collections.abc import Mapping from dataclasses import dataclass -from typing import TYPE_CHECKING, Any, Collection, Mapping, Sequence, overload +from typing import TYPE_CHECKING, Any, overload import hypothesis.strategies as st from hypothesis.errors import InvalidArgument @@ -16,6 +17,7 @@ from polars.testing.parametric.strategies.dtype import _instantiate_dtype, dtypes if TYPE_CHECKING: + from collections.abc import Collection, Sequence from typing import Literal from hypothesis.strategies import DrawFn, SearchStrategy diff --git a/py-polars/polars/testing/parametric/strategies/data.py b/py-polars/polars/testing/parametric/strategies/data.py index adf94379326b..f63b6d8a8945 100644 --- a/py-polars/polars/testing/parametric/strategies/data.py +++ b/py-polars/polars/testing/parametric/strategies/data.py @@ -3,8 +3,9 @@ from __future__ import annotations import decimal +from collections.abc import Mapping from datetime import datetime, timedelta, timezone -from typing import TYPE_CHECKING, Any, Literal, Mapping, Sequence +from typing import TYPE_CHECKING, Any, Literal import hypothesis.strategies as st from hypothesis.errors import InvalidArgument @@ -60,6 +61,7 @@ ) if TYPE_CHECKING: + from collections.abc import Sequence from datetime import date, time from hypothesis.strategies import SearchStrategy diff --git a/py-polars/polars/testing/parametric/strategies/dtype.py b/py-polars/polars/testing/parametric/strategies/dtype.py index 96dd928d2b84..fff1ad58c726 100644 --- a/py-polars/polars/testing/parametric/strategies/dtype.py +++ b/py-polars/polars/testing/parametric/strategies/dtype.py @@ -1,6 +1,6 @@ from __future__ import annotations -from typing import TYPE_CHECKING, Collection, Sequence +from typing import TYPE_CHECKING import hypothesis.strategies as st from hypothesis.errors import InvalidArgument @@ -35,6 +35,8 @@ ) if TYPE_CHECKING: + from collections.abc import Collection, Sequence + from hypothesis.strategies import DrawFn, SearchStrategy from polars._typing import CategoricalOrdering, PolarsDataType, TimeUnit diff --git a/py-polars/polars/testing/parametric/strategies/legacy.py b/py-polars/polars/testing/parametric/strategies/legacy.py index 7e238594a5c3..e5bc328f1ed5 100644 --- a/py-polars/polars/testing/parametric/strategies/legacy.py +++ b/py-polars/polars/testing/parametric/strategies/legacy.py @@ -1,6 +1,7 @@ from __future__ import annotations -from typing import TYPE_CHECKING, Any, Sequence +from collections.abc import Sequence +from typing import TYPE_CHECKING, Any import hypothesis.strategies as st from hypothesis.errors import InvalidArgument diff --git a/py-polars/pyproject.toml b/py-polars/pyproject.toml index fed084292021..0cfc6b9e43c7 100644 --- a/py-polars/pyproject.toml +++ b/py-polars/pyproject.toml @@ -10,7 +10,7 @@ authors = [ { name = "Ritchie Vink", email = "ritchie46@gmail.com" }, ] license = { file = "LICENSE" } -requires-python = ">=3.8" +requires-python = ">=3.9" keywords = ["dataframe", "arrow", "out-of-core"] classifiers = [ @@ -22,7 +22,6 @@ classifiers = [ "Programming Language :: Python", "Programming Language :: Python :: 3", "Programming Language :: Python :: 3 :: Only", - "Programming Language :: Python :: 3.8", "Programming Language :: Python :: 3.9", "Programming Language :: Python :: 3.10", "Programming Language :: Python :: 3.11", diff --git a/py-polars/tests/docs/run_doctest.py b/py-polars/tests/docs/run_doctest.py index 353b9cfb0dd0..404070deae85 100644 --- a/py-polars/tests/docs/run_doctest.py +++ b/py-polars/tests/docs/run_doctest.py @@ -37,11 +37,12 @@ import warnings from pathlib import Path from tempfile import TemporaryDirectory -from typing import TYPE_CHECKING, Any, Iterator +from typing import TYPE_CHECKING, Any import polars as pl if TYPE_CHECKING: + from collections.abc import Iterator from types import ModuleType diff --git a/py-polars/tests/docs/test_user_guide.py b/py-polars/tests/docs/test_user_guide.py index 7cae31e8c0c3..69586f002e8d 100644 --- a/py-polars/tests/docs/test_user_guide.py +++ b/py-polars/tests/docs/test_user_guide.py @@ -2,8 +2,8 @@ import os import runpy +from collections.abc import Iterator from pathlib import Path -from typing import Iterator import matplotlib as mpl import pytest diff --git a/py-polars/tests/unit/conftest.py b/py-polars/tests/unit/conftest.py index 7335f8f46835..ead5bee2c265 100644 --- a/py-polars/tests/unit/conftest.py +++ b/py-polars/tests/unit/conftest.py @@ -6,7 +6,7 @@ import string import sys import tracemalloc -from typing import Any, Generator, List, cast +from typing import TYPE_CHECKING, Any, cast import numpy as np import pytest @@ -14,6 +14,9 @@ import polars as pl from polars.testing.parametric import load_profile +if TYPE_CHECKING: + from collections.abc import Generator + load_profile( profile=os.environ.get("POLARS_HYPOTHESIS_PROFILE", "fast"), # type: ignore[arg-type] ) @@ -128,7 +131,7 @@ def str_ints_df() -> pl.DataFrame: @pytest.fixture(params=ISO8601_FORMATS_DATETIME) def iso8601_format_datetime(request: pytest.FixtureRequest) -> list[str]: - return cast(List[str], request.param) + return cast(list[str], request.param) ISO8601_TZ_AWARE_FORMATS_DATETIME = [] @@ -151,7 +154,7 @@ def iso8601_format_datetime(request: pytest.FixtureRequest) -> list[str]: @pytest.fixture(params=ISO8601_TZ_AWARE_FORMATS_DATETIME) def iso8601_tz_aware_format_datetime(request: pytest.FixtureRequest) -> list[str]: - return cast(List[str], request.param) + return cast(list[str], request.param) ISO8601_FORMATS_DATE = [] @@ -163,7 +166,7 @@ def iso8601_tz_aware_format_datetime(request: pytest.FixtureRequest) -> list[str @pytest.fixture(params=ISO8601_FORMATS_DATE) def iso8601_format_date(request: pytest.FixtureRequest) -> list[str]: - return cast(List[str], request.param) + return cast(list[str], request.param) class MemoryUsage: diff --git a/py-polars/tests/unit/constructors/test_constructors.py b/py-polars/tests/unit/constructors/test_constructors.py index dbfe77b46c97..d340433ddf10 100644 --- a/py-polars/tests/unit/constructors/test_constructors.py +++ b/py-polars/tests/unit/constructors/test_constructors.py @@ -4,7 +4,7 @@ from datetime import date, datetime, time, timedelta, timezone from decimal import Decimal from random import shuffle -from typing import TYPE_CHECKING, Any, List, Literal, NamedTuple +from typing import TYPE_CHECKING, Any, Literal, NamedTuple import numpy as np import pandas as pd @@ -22,7 +22,6 @@ if TYPE_CHECKING: from collections.abc import Callable - from zoneinfo import ZoneInfo from polars._typing import PolarsDataType @@ -281,7 +280,7 @@ class PageView(BaseModel): "top": 123 }] """ - adapter: TypeAdapter[Any] = TypeAdapter(List[PageView]) + adapter: TypeAdapter[Any] = TypeAdapter(list[PageView]) models = adapter.validate_json(data_json) result = pl.DataFrame(models) diff --git a/py-polars/tests/unit/constructors/test_dataframe.py b/py-polars/tests/unit/constructors/test_dataframe.py index 3703475aa438..5e56630e7552 100644 --- a/py-polars/tests/unit/constructors/test_dataframe.py +++ b/py-polars/tests/unit/constructors/test_dataframe.py @@ -2,13 +2,17 @@ import sys from collections import OrderedDict -from typing import Any, Iterator, Mapping +from collections.abc import Mapping +from typing import TYPE_CHECKING, Any import pytest import polars as pl from polars.exceptions import DataOrientationWarning, InvalidOperationError +if TYPE_CHECKING: + from collections.abc import Iterator + def test_df_mixed_dtypes_string() -> None: data = {"x": [["abc", 12, 34.5]], "y": [1]} diff --git a/py-polars/tests/unit/dataframe/test_df.py b/py-polars/tests/unit/dataframe/test_df.py index 8798dc89ed22..2389bef36c88 100644 --- a/py-polars/tests/unit/dataframe/test_df.py +++ b/py-polars/tests/unit/dataframe/test_df.py @@ -7,7 +7,7 @@ from decimal import Decimal from io import BytesIO from operator import floordiv, truediv -from typing import TYPE_CHECKING, Any, Callable, Iterator, Sequence, cast +from typing import TYPE_CHECKING, Any, Callable, cast import numpy as np import pyarrow as pa @@ -32,6 +32,7 @@ from tests.unit.conftest import INTEGER_DTYPES if TYPE_CHECKING: + from collections.abc import Iterator, Sequence from zoneinfo import ZoneInfo from polars import Expr diff --git a/py-polars/tests/unit/dataframe/test_upsample.py b/py-polars/tests/unit/dataframe/test_upsample.py index 21160ad54df8..28bcbf13f401 100644 --- a/py-polars/tests/unit/dataframe/test_upsample.py +++ b/py-polars/tests/unit/dataframe/test_upsample.py @@ -11,7 +11,6 @@ if TYPE_CHECKING: from datetime import timezone - from zoneinfo import ZoneInfo from polars._typing import FillNullStrategy, PolarsIntegerType diff --git a/py-polars/tests/unit/datatypes/test_parse.py b/py-polars/tests/unit/datatypes/test_parse.py index c95763033b32..0979292e8e50 100644 --- a/py-polars/tests/unit/datatypes/test_parse.py +++ b/py-polars/tests/unit/datatypes/test_parse.py @@ -4,12 +4,9 @@ from typing import ( TYPE_CHECKING, Any, - Dict, ForwardRef, - List, NamedTuple, Optional, - Tuple, Union, ) @@ -63,9 +60,9 @@ def test_parse_py_type_into_dtype(input: Any, expected: PolarsDataType) -> None: @pytest.mark.parametrize( ("input", "expected"), [ - (List[int], pl.List(pl.Int64())), - (Tuple[str, ...], pl.List(pl.String())), - (Tuple[datetime, datetime], pl.List(pl.Datetime("us"))), + (list[int], pl.List(pl.Int64())), + (tuple[str, ...], pl.List(pl.String())), + (tuple[datetime, datetime], pl.List(pl.Datetime("us"))), ], ) def test_parse_generic_into_dtype(input: Any, expected: PolarsDataType) -> None: @@ -76,9 +73,9 @@ def test_parse_generic_into_dtype(input: Any, expected: PolarsDataType) -> None: @pytest.mark.parametrize( "input", [ - Dict[str, float], - Tuple[int, str], - Tuple[int, float, float], + dict[str, float], + tuple[int, str], + tuple[int, float, float], ], ) def test_parse_generic_into_dtype_invalid(input: Any) -> None: diff --git a/py-polars/tests/unit/io/cloud/test_aws.py b/py-polars/tests/unit/io/cloud/test_aws.py index 9a004a7d825f..6f2116421822 100644 --- a/py-polars/tests/unit/io/cloud/test_aws.py +++ b/py-polars/tests/unit/io/cloud/test_aws.py @@ -1,7 +1,7 @@ from __future__ import annotations import multiprocessing -from typing import TYPE_CHECKING, Any, Callable, Iterator +from typing import TYPE_CHECKING, Any, Callable import boto3 import pytest @@ -11,6 +11,7 @@ from polars.testing import assert_frame_equal if TYPE_CHECKING: + from collections.abc import Iterator from pathlib import Path pytestmark = [ diff --git a/py-polars/tests/unit/io/database/test_async.py b/py-polars/tests/unit/io/database/test_async.py index a8492e7a5276..3bdea8207d2c 100644 --- a/py-polars/tests/unit/io/database/test_async.py +++ b/py-polars/tests/unit/io/database/test_async.py @@ -2,7 +2,7 @@ import asyncio from math import ceil -from typing import TYPE_CHECKING, Any, Iterable, overload +from typing import TYPE_CHECKING, Any, overload import pytest import sqlalchemy @@ -13,6 +13,7 @@ from polars.testing import assert_frame_equal if TYPE_CHECKING: + from collections.abc import Iterable from pathlib import Path SURREAL_MOCK_DATA: list[dict[str, Any]] = [ diff --git a/py-polars/tests/unit/io/database/test_read.py b/py-polars/tests/unit/io/database/test_read.py index 967379f356f7..deb44a5a79f4 100644 --- a/py-polars/tests/unit/io/database/test_read.py +++ b/py-polars/tests/unit/io/database/test_read.py @@ -32,7 +32,7 @@ def adbc_sqlite_connect(*args: Any, **kwargs: Any) -> Any: - with suppress(ModuleNotFoundError): # not available on 3.8/windows + with suppress(ModuleNotFoundError): # not available on windows from adbc_driver_sqlite.dbapi import connect args = tuple(str(a) if isinstance(a, Path) else a for a in args) diff --git a/py-polars/tests/unit/io/database/test_write.py b/py-polars/tests/unit/io/database/test_write.py index da9550d7126a..0e50044030f4 100644 --- a/py-polars/tests/unit/io/database/test_write.py +++ b/py-polars/tests/unit/io/database/test_write.py @@ -28,16 +28,16 @@ "adbc", True, marks=pytest.mark.skipif( - sys.version_info < (3, 9) or sys.platform == "win32", - reason="adbc not available on Windows or <= Python 3.8", + sys.platform == "win32", + reason="adbc not available on Windows", ), ), pytest.param( "adbc", False, marks=pytest.mark.skipif( - sys.version_info < (3, 9) or sys.platform == "win32", - reason="adbc not available on Windows or <= Python 3.8", + sys.platform == "win32", + reason="adbc not available on Windows", ), ), ], diff --git a/py-polars/tests/unit/io/test_plugins.py b/py-polars/tests/unit/io/test_plugins.py index 98c25edc3f4a..6303df166962 100644 --- a/py-polars/tests/unit/io/test_plugins.py +++ b/py-polars/tests/unit/io/test_plugins.py @@ -7,7 +7,7 @@ from polars.testing import assert_frame_equal if TYPE_CHECKING: - from typing import Iterator + from collections.abc import Iterator # A simple python source. But this can dispatch into a rust IO source as well. diff --git a/py-polars/tests/unit/io/test_spreadsheet.py b/py-polars/tests/unit/io/test_spreadsheet.py index 06af29659ba0..7483f371b51c 100644 --- a/py-polars/tests/unit/io/test_spreadsheet.py +++ b/py-polars/tests/unit/io/test_spreadsheet.py @@ -5,7 +5,7 @@ from datetime import date, datetime from io import BytesIO from pathlib import Path -from typing import TYPE_CHECKING, Any, Callable, Sequence +from typing import TYPE_CHECKING, Any, Callable import pytest @@ -17,6 +17,8 @@ from tests.unit.conftest import FLOAT_DTYPES, NUMERIC_DTYPES if TYPE_CHECKING: + from collections.abc import Sequence + from polars._typing import ExcelSpreadsheetEngine, SelectorType pytestmark = pytest.mark.slow() diff --git a/py-polars/tests/unit/io/test_utils.py b/py-polars/tests/unit/io/test_utils.py index 7c2173469ebb..e115aec4f71f 100644 --- a/py-polars/tests/unit/io/test_utils.py +++ b/py-polars/tests/unit/io/test_utils.py @@ -1,11 +1,14 @@ from __future__ import annotations -from typing import Sequence +from typing import TYPE_CHECKING import pytest from polars.io._utils import looks_like_url, parse_columns_arg, parse_row_index_args +if TYPE_CHECKING: + from collections.abc import Sequence + @pytest.mark.parametrize( ("columns", "expected"), diff --git a/py-polars/tests/unit/operations/map/test_map_elements.py b/py-polars/tests/unit/operations/map/test_map_elements.py index ce147be9ef27..0ef231bc8943 100644 --- a/py-polars/tests/unit/operations/map/test_map_elements.py +++ b/py-polars/tests/unit/operations/map/test_map_elements.py @@ -340,9 +340,10 @@ def test_map_elements_chunked_14390() -> None: def test_cabbage_strategy_14396() -> None: df = pl.DataFrame({"x": [1, 2, 3]}) - with pytest.raises( - ValueError, match="strategy 'cabbage' is not supported" - ), pytest.warns(PolarsInefficientMapWarning): + with ( + pytest.raises(ValueError, match="strategy 'cabbage' is not supported"), + pytest.warns(PolarsInefficientMapWarning), + ): df.select(pl.col("x").map_elements(lambda x: 2 * x, strategy="cabbage")) # type: ignore[arg-type] 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 d675d43e28f6..772f4d088249 100644 --- a/py-polars/tests/unit/operations/map/test_map_groups.py +++ b/py-polars/tests/unit/operations/map/test_map_groups.py @@ -1,6 +1,6 @@ from __future__ import annotations -from typing import Any, Sequence +from typing import TYPE_CHECKING, Any import numpy as np import pytest @@ -9,6 +9,9 @@ from polars.exceptions import ComputeError from polars.testing import assert_frame_equal +if TYPE_CHECKING: + from collections.abc import Sequence + def test_map_groups() -> None: df = pl.DataFrame( diff --git a/py-polars/tests/unit/operations/namespaces/temporal/test_add_business_days.py b/py-polars/tests/unit/operations/namespaces/temporal/test_add_business_days.py index 5fe78225670e..4d646e2bba49 100644 --- a/py-polars/tests/unit/operations/namespaces/temporal/test_add_business_days.py +++ b/py-polars/tests/unit/operations/namespaces/temporal/test_add_business_days.py @@ -1,7 +1,6 @@ from __future__ import annotations import datetime as dt -import sys from datetime import date, datetime, timedelta from typing import TYPE_CHECKING @@ -11,19 +10,13 @@ from hypothesis import assume, given import polars as pl -from polars.dependencies import _ZONEINFO_AVAILABLE from polars.exceptions import ComputeError, InvalidOperationError from polars.testing import assert_series_equal if TYPE_CHECKING: from polars._typing import Roll, TimeUnit -if sys.version_info >= (3, 9): - from zoneinfo import ZoneInfo -elif _ZONEINFO_AVAILABLE: - # Import from submodule due to typing issue with backports.zoneinfo package: - # https://github.com/pganssle/zoneinfo/issues/125 - from backports.zoneinfo._zoneinfo import ZoneInfo +from zoneinfo import ZoneInfo def test_add_business_days() -> None: 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 5bb09a0f8d1c..f07106ee76ac 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 @@ -1,25 +1,17 @@ from __future__ import annotations -import sys from datetime import date, datetime from typing import TYPE_CHECKING +from zoneinfo import ZoneInfo import hypothesis.strategies as st import pytest from hypothesis import given import polars as pl -from polars.dependencies import _ZONEINFO_AVAILABLE from polars.exceptions import ComputeError, InvalidOperationError from polars.testing import assert_series_equal -if sys.version_info >= (3, 9): - from zoneinfo import ZoneInfo -elif _ZONEINFO_AVAILABLE: - # Import from submodule due to typing issue with backports.zoneinfo package: - # https://github.com/pganssle/zoneinfo/issues/125 - from backports.zoneinfo._zoneinfo import ZoneInfo - if TYPE_CHECKING: from hypothesis.strategies import DrawFn diff --git a/py-polars/tests/unit/operations/test_comparison.py b/py-polars/tests/unit/operations/test_comparison.py index 72771ff64eab..26f95e269339 100644 --- a/py-polars/tests/unit/operations/test_comparison.py +++ b/py-polars/tests/unit/operations/test_comparison.py @@ -2,7 +2,7 @@ import math from contextlib import nullcontext -from typing import TYPE_CHECKING, Any, ContextManager +from typing import TYPE_CHECKING, Any import pytest @@ -11,6 +11,8 @@ from polars.testing import assert_frame_equal if TYPE_CHECKING: + from contextlib import AbstractContextManager as ContextManager + from polars._typing import PolarsDataType diff --git a/py-polars/tests/unit/operations/test_cross_join.py b/py-polars/tests/unit/operations/test_cross_join.py index 94830371ed35..f424da5ab170 100644 --- a/py-polars/tests/unit/operations/test_cross_join.py +++ b/py-polars/tests/unit/operations/test_cross_join.py @@ -1,17 +1,8 @@ -import sys from datetime import datetime +from zoneinfo import ZoneInfo import pytest -from polars.dependencies import _ZONEINFO_AVAILABLE - -if sys.version_info >= (3, 9): - from zoneinfo import ZoneInfo -elif _ZONEINFO_AVAILABLE: - # Import from submodule due to typing issue with backports.zoneinfo package: - # https://github.com/pganssle/zoneinfo/issues/125 - from backports.zoneinfo._zoneinfo import ZoneInfo - import polars as pl diff --git a/py-polars/tests/unit/operations/test_ewm_by.py b/py-polars/tests/unit/operations/test_ewm_by.py index e804f6b6d5d8..8004303238d4 100644 --- a/py-polars/tests/unit/operations/test_ewm_by.py +++ b/py-polars/tests/unit/operations/test_ewm_by.py @@ -1,25 +1,18 @@ from __future__ import annotations -import sys from datetime import date, datetime, timedelta from typing import TYPE_CHECKING import pytest import polars as pl -from polars.dependencies import _ZONEINFO_AVAILABLE from polars.exceptions import InvalidOperationError from polars.testing import assert_frame_equal, assert_series_equal if TYPE_CHECKING: from polars._typing import PolarsIntegerType, TimeUnit -if sys.version_info >= (3, 9): - from zoneinfo import ZoneInfo -elif _ZONEINFO_AVAILABLE: - # Import from submodule due to typing issue with backports.zoneinfo package: - # https://github.com/pganssle/zoneinfo/issues/125 - from backports.zoneinfo._zoneinfo import ZoneInfo +from zoneinfo import ZoneInfo @pytest.mark.parametrize("sort", [True, False]) diff --git a/py-polars/tests/unit/operations/test_interpolate.py b/py-polars/tests/unit/operations/test_interpolate.py index e80d4102466c..bac0693205da 100644 --- a/py-polars/tests/unit/operations/test_interpolate.py +++ b/py-polars/tests/unit/operations/test_interpolate.py @@ -1,24 +1,17 @@ from __future__ import annotations -import sys from datetime import date, datetime, time, timedelta from typing import TYPE_CHECKING, Any import pytest import polars as pl -from polars.dependencies import _ZONEINFO_AVAILABLE from polars.testing import assert_frame_equal if TYPE_CHECKING: from polars._typing import PolarsDataType, PolarsTemporalType -if sys.version_info >= (3, 9): - from zoneinfo import ZoneInfo -elif _ZONEINFO_AVAILABLE: - # Import from submodule due to typing issue with backports.zoneinfo package: - # https://github.com/pganssle/zoneinfo/issues/125 - from backports.zoneinfo._zoneinfo import ZoneInfo +from zoneinfo import ZoneInfo @pytest.mark.parametrize( diff --git a/py-polars/tests/unit/operations/test_transpose.py b/py-polars/tests/unit/operations/test_transpose.py index a43a6e7f629e..d81ab5b1404e 100644 --- a/py-polars/tests/unit/operations/test_transpose.py +++ b/py-polars/tests/unit/operations/test_transpose.py @@ -1,6 +1,6 @@ import io +from collections.abc import Iterator from datetime import date, datetime -from typing import Iterator import pytest diff --git a/py-polars/tests/unit/series/test_series.py b/py-polars/tests/unit/series/test_series.py index 0f45cdfb6e21..491890b5dc13 100644 --- a/py-polars/tests/unit/series/test_series.py +++ b/py-polars/tests/unit/series/test_series.py @@ -2,7 +2,7 @@ import math from datetime import date, datetime, time, timedelta -from typing import TYPE_CHECKING, Any, Iterator, cast +from typing import TYPE_CHECKING, Any, cast import numpy as np import pandas as pd @@ -31,6 +31,7 @@ from tests.unit.utils.pycapsule_utils import PyCapsuleStreamHolder if TYPE_CHECKING: + from collections.abc import Iterator from zoneinfo import ZoneInfo from polars._typing import EpochTimeUnit, PolarsDataType, TimeUnit diff --git a/py-polars/tests/unit/sql/test_joins.py b/py-polars/tests/unit/sql/test_joins.py index 3a1e90bed1b4..43c00ed8b3d5 100644 --- a/py-polars/tests/unit/sql/test_joins.py +++ b/py-polars/tests/unit/sql/test_joins.py @@ -295,10 +295,13 @@ def test_join_misc_16255() -> None: ) def test_non_equi_joins(constraint: str) -> None: # no support (yet) for non equi-joins in polars joins - with pytest.raises( - SQLInterfaceError, - match=r"only equi-join constraints are supported", - ), pl.SQLContext({"tbl": pl.DataFrame({"a": [1, 2, 3], "b": [4, 3, 2]})}) as ctx: + with ( + pytest.raises( + SQLInterfaceError, + match=r"only equi-join constraints are supported", + ), + pl.SQLContext({"tbl": pl.DataFrame({"a": [1, 2, 3], "b": [4, 3, 2]})}) as ctx, + ): ctx.execute( f""" SELECT * @@ -310,12 +313,19 @@ def test_non_equi_joins(constraint: str) -> None: def test_implicit_joins() -> None: # no support for this yet; ensure we catch it - with pytest.raises( - SQLInterfaceError, - match=r"not currently supported .* use explicit JOIN syntax instead", - ), pl.SQLContext( - {"tbl": pl.DataFrame({"a": [1, 2, 3], "b": [4, 3, 2], "c": ["x", "y", "z"]})} - ) as ctx: + with ( + pytest.raises( + SQLInterfaceError, + match=r"not currently supported .* use explicit JOIN syntax instead", + ), + pl.SQLContext( + { + "tbl": pl.DataFrame( + {"a": [1, 2, 3], "b": [4, 3, 2], "c": ["x", "y", "z"]} + ) + } + ) as ctx, + ): ctx.execute( """ SELECT t1.* diff --git a/py-polars/tests/unit/test_config.py b/py-polars/tests/unit/test_config.py index f6cd9411bebf..cd0491b5435f 100644 --- a/py-polars/tests/unit/test_config.py +++ b/py-polars/tests/unit/test_config.py @@ -2,7 +2,7 @@ import os from pathlib import Path -from typing import Any, Iterator +from typing import TYPE_CHECKING, Any import pytest @@ -11,6 +11,9 @@ from polars._utils.unstable import issue_unstable_warning from polars.config import _POLARS_CFG_ENV_VARS +if TYPE_CHECKING: + from collections.abc import Iterator + @pytest.fixture(autouse=True) def _environ() -> Iterator[None]: diff --git a/py-polars/tests/unit/test_format.py b/py-polars/tests/unit/test_format.py index 2461f100209b..44c754e4884b 100644 --- a/py-polars/tests/unit/test_format.py +++ b/py-polars/tests/unit/test_format.py @@ -2,7 +2,7 @@ import string from decimal import Decimal as D -from typing import TYPE_CHECKING, Any, Iterator +from typing import TYPE_CHECKING, Any import pytest @@ -10,6 +10,8 @@ from polars.exceptions import InvalidOperationError if TYPE_CHECKING: + from collections.abc import Iterator + from polars._typing import PolarsDataType @@ -290,8 +292,9 @@ def test_fmt_float_full() -> None: def test_fmt_list_12188() -> None: # set max_items to 1 < 4(size of failed list) to touch the testing branch. - with pl.Config(fmt_table_cell_list_len=1), pytest.raises( - InvalidOperationError, match="from `i64` to `u8` failed" + with ( + pl.Config(fmt_table_cell_list_len=1), + pytest.raises(InvalidOperationError, match="from `i64` to `u8` failed"), ): pl.DataFrame( { diff --git a/py-polars/tests/unit/test_selectors.py b/py-polars/tests/unit/test_selectors.py index a5d573d10379..bf44ff87bac5 100644 --- a/py-polars/tests/unit/test_selectors.py +++ b/py-polars/tests/unit/test_selectors.py @@ -1,26 +1,18 @@ -import sys from collections import OrderedDict from datetime import datetime from typing import Any +from zoneinfo import ZoneInfo import pytest import polars as pl import polars.selectors as cs from polars._typing import SelectorType -from polars.dependencies import _ZONEINFO_AVAILABLE from polars.exceptions import ColumnNotFoundError, InvalidOperationError from polars.selectors import expand_selector, is_selector from polars.testing import assert_frame_equal from tests.unit.conftest import INTEGER_DTYPES, TEMPORAL_DTYPES -if sys.version_info >= (3, 9): - from zoneinfo import ZoneInfo -elif _ZONEINFO_AVAILABLE: - # Import from submodule due to typing issue with backports.zoneinfo package: - # https://github.com/pganssle/zoneinfo/issues/125 - from backports.zoneinfo._zoneinfo import ZoneInfo - def assert_repr_equals(item: Any, expected: str) -> None: """Assert that the repr of an item matches the expected string.""" diff --git a/py-polars/tests/unit/test_string_cache.py b/py-polars/tests/unit/test_string_cache.py index b54b08d48a86..def1f15db07a 100644 --- a/py-polars/tests/unit/test_string_cache.py +++ b/py-polars/tests/unit/test_string_cache.py @@ -1,4 +1,4 @@ -from typing import Iterator +from collections.abc import Iterator import pytest diff --git a/py-polars/tests/unit/utils/test_utils.py b/py-polars/tests/unit/utils/test_utils.py index f6eca92215da..96730c91434b 100644 --- a/py-polars/tests/unit/utils/test_utils.py +++ b/py-polars/tests/unit/utils/test_utils.py @@ -1,7 +1,7 @@ from __future__ import annotations from datetime import date, datetime, time, timedelta -from typing import TYPE_CHECKING, Any, Sequence +from typing import TYPE_CHECKING, Any import numpy as np import pytest @@ -25,6 +25,7 @@ ) if TYPE_CHECKING: + from collections.abc import Sequence from zoneinfo import ZoneInfo from polars._typing import TimeUnit