From d264a784eeaf5053374224eb4f40698bcb40b91d Mon Sep 17 00:00:00 2001 From: Iliya Malecki Date: Sun, 17 Nov 2024 14:36:52 +0100 Subject: [PATCH 1/2] fix generator typing --- py-polars/polars/io/database/_executor.py | 6 +++--- py-polars/polars/io/database/functions.py | 8 ++++---- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/py-polars/polars/io/database/_executor.py b/py-polars/polars/io/database/_executor.py index 070e24f177c8..2c8ea5ed2f16 100644 --- a/py-polars/polars/io/database/_executor.py +++ b/py-polars/polars/io/database/_executor.py @@ -207,7 +207,7 @@ def _from_arrow( iter_batches: bool, schema_overrides: SchemaDict | None, infer_schema_length: int | None, - ) -> DataFrame | Iterable[DataFrame] | None: + ) -> DataFrame | Iterator[DataFrame] | None: """Return resultset data in Arrow format for frame init.""" from polars import DataFrame @@ -253,7 +253,7 @@ def _from_rows( iter_batches: bool, schema_overrides: SchemaDict | None, infer_schema_length: int | None, - ) -> DataFrame | Iterable[DataFrame] | None: + ) -> DataFrame | Iterator[DataFrame] | None: """Return resultset data row-wise for frame init.""" from polars import DataFrame @@ -529,7 +529,7 @@ def to_polars( batch_size: int | None = None, schema_overrides: SchemaDict | None = None, infer_schema_length: int | None = N_INFER_DEFAULT, - ) -> DataFrame | Iterable[DataFrame]: + ) -> DataFrame | Iterator[DataFrame]: """ Convert the result set to a DataFrame. diff --git a/py-polars/polars/io/database/functions.py b/py-polars/polars/io/database/functions.py index 21e436dc0557..c70fbec53457 100644 --- a/py-polars/polars/io/database/functions.py +++ b/py-polars/polars/io/database/functions.py @@ -10,7 +10,7 @@ if TYPE_CHECKING: import sys - from collections.abc import Iterable + from collections.abc import Iterator if sys.version_info >= (3, 10): from typing import TypeAlias @@ -51,7 +51,7 @@ def read_database( schema_overrides: SchemaDict | None = ..., infer_schema_length: int | None = ..., execute_options: dict[str, Any] | None = ..., -) -> Iterable[DataFrame]: ... +) -> Iterator[DataFrame]: ... @overload @@ -64,7 +64,7 @@ def read_database( schema_overrides: SchemaDict | None = ..., infer_schema_length: int | None = ..., execute_options: dict[str, Any] | None = ..., -) -> DataFrame | Iterable[DataFrame]: ... +) -> DataFrame | Iterator[DataFrame]: ... def read_database( @@ -76,7 +76,7 @@ def read_database( schema_overrides: SchemaDict | None = None, infer_schema_length: int | None = N_INFER_DEFAULT, execute_options: dict[str, Any] | None = None, -) -> DataFrame | Iterable[DataFrame]: +) -> DataFrame | Iterator[DataFrame]: """ Read the results of a SQL query into a DataFrame, given a connection object. From 04da097c60d12cd5126bd8e5a8baeb36fdb39cfb Mon Sep 17 00:00:00 2001 From: Iliya Malecki Date: Sun, 17 Nov 2024 14:44:49 +0100 Subject: [PATCH 2/2] fix Coroutine conditional imports in a if TYPE_CHECKING block no runtime behavior can exist, such as try-excepts. They were replaced by a single import --- py-polars/polars/io/database/_executor.py | 15 +++------------ py-polars/polars/io/database/_utils.py | 11 ----------- py-polars/polars/io/database/functions.py | 14 ++------------ 3 files changed, 5 insertions(+), 35 deletions(-) diff --git a/py-polars/polars/io/database/_executor.py b/py-polars/polars/io/database/_executor.py index 2c8ea5ed2f16..552d6cc18aa3 100644 --- a/py-polars/polars/io/database/_executor.py +++ b/py-polars/polars/io/database/_executor.py @@ -29,26 +29,17 @@ from polars.io.database._arrow_registry import ArrowDriverProperties - if sys.version_info >= (3, 10): - from typing import TypeAlias - else: - from typing_extensions import TypeAlias - if sys.version_info >= (3, 11): from typing import Self else: from typing_extensions import Self + from sqlalchemy.sql.elements import TextClause + from sqlalchemy.sql.expression import Selectable + from polars import DataFrame from polars._typing import ConnectionOrCursor, Cursor, SchemaDict - try: - from sqlalchemy.sql.expression import Selectable - except ImportError: - Selectable: TypeAlias = Any # type: ignore[no-redef] - - from sqlalchemy.sql.elements import TextClause - _INVALID_QUERY_TYPES = { "ALTER", "ANALYZE", diff --git a/py-polars/polars/io/database/_utils.py b/py-polars/polars/io/database/_utils.py index c67b4cc9d631..6ea01772cc98 100644 --- a/py-polars/polars/io/database/_utils.py +++ b/py-polars/polars/io/database/_utils.py @@ -7,22 +7,11 @@ from polars.dependencies import import_optional if TYPE_CHECKING: - import sys from collections.abc import Coroutine - if sys.version_info >= (3, 10): - from typing import TypeAlias - else: - from typing_extensions import TypeAlias - from polars import DataFrame from polars._typing import SchemaDict - try: - from sqlalchemy.sql.expression import Selectable - except ImportError: - Selectable: TypeAlias = Any # type: ignore[no-redef] - def _run_async(co: Coroutine[Any, Any, Any]) -> Any: """Run asynchronous code as if it was synchronous.""" diff --git a/py-polars/polars/io/database/functions.py b/py-polars/polars/io/database/functions.py index c70fbec53457..311f92948c49 100644 --- a/py-polars/polars/io/database/functions.py +++ b/py-polars/polars/io/database/functions.py @@ -9,24 +9,14 @@ from polars.io.database._executor import ConnectionExecutor if TYPE_CHECKING: - import sys from collections.abc import Iterator - if sys.version_info >= (3, 10): - from typing import TypeAlias - else: - from typing_extensions import TypeAlias + from sqlalchemy.sql.elements import TextClause + from sqlalchemy.sql.expression import Selectable from polars import DataFrame from polars._typing import ConnectionOrCursor, DbReadEngine, SchemaDict - try: - from sqlalchemy.sql.expression import Selectable - except ImportError: - Selectable: TypeAlias = Any # type: ignore[no-redef] - - from sqlalchemy.sql.elements import TextClause - @overload def read_database(