Skip to content

Commit

Permalink
refactor(python)!: Remove re-export of type aliases (#17032)
Browse files Browse the repository at this point in the history
  • Loading branch information
stinodego authored Jun 18, 2024
1 parent 915eb08 commit 2aec475
Show file tree
Hide file tree
Showing 40 changed files with 184 additions and 133 deletions.
3 changes: 0 additions & 3 deletions py-polars/polars/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,6 @@
enable_string_cache,
using_string_cache,
)
from polars.type_aliases import PolarsDataType

__version__: str = _get_polars_version()
del _get_polars_version
Expand Down Expand Up @@ -323,8 +322,6 @@
"NESTED_DTYPES",
"NUMERIC_DTYPES",
"TEMPORAL_DTYPES",
# polars.type_aliases
"PolarsDataType",
# polars.io
"read_avro",
"read_clipboard",
Expand Down
15 changes: 0 additions & 15 deletions py-polars/polars/datatypes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,6 @@
supported_numpy_char_code,
unpack_dtypes,
)
from polars.type_aliases import (
OneOrMoreDataTypes,
PolarsDataType,
PolarsTemporalType,
PythonDataType,
SchemaDefinition,
SchemaDict,
)

__all__ = [
# classes
Expand Down Expand Up @@ -134,11 +126,4 @@
"py_type_to_dtype",
"supported_numpy_char_code",
"unpack_dtypes",
# type_aliases
"OneOrMoreDataTypes",
"PolarsDataType",
"PolarsTemporalType",
"PythonDataType",
"SchemaDefinition",
"SchemaDict",
]
8 changes: 6 additions & 2 deletions py-polars/polars/datatypes/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,12 @@
)

if TYPE_CHECKING:
from polars.datatypes import PolarsDataType
from polars.type_aliases import PolarsIntegerType, PolarsTemporalType, TimeUnit
from polars.type_aliases import (
PolarsDataType,
PolarsIntegerType,
PolarsTemporalType,
TimeUnit,
)


DTYPE_TEMPORAL_UNITS: frozenset[TimeUnit] = frozenset(["ns", "us", "ms"])
Expand Down
3 changes: 1 addition & 2 deletions py-polars/polars/selectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@
import sys

from polars import DataFrame, LazyFrame
from polars.datatypes import PolarsDataType
from polars.type_aliases import SelectorType, TimeUnit
from polars.type_aliases import PolarsDataType, SelectorType, TimeUnit

if sys.version_info >= (3, 11):
from typing import Self
Expand Down
18 changes: 9 additions & 9 deletions py-polars/polars/type_aliases.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
from __future__ import annotations

from datetime import date, datetime, time, timedelta
from decimal import Decimal
from typing import (
TYPE_CHECKING,
Any,
Expand All @@ -21,6 +19,8 @@

if TYPE_CHECKING:
import sys
from datetime import date, datetime, time, timedelta
from decimal import Decimal

from sqlalchemy.engine import Connection, Engine
from sqlalchemy.orm import Session
Expand All @@ -47,14 +47,14 @@
Type[float],
Type[bool],
Type[str],
Type[date],
Type[time],
Type[datetime],
Type[timedelta],
Type["date"],
Type["time"],
Type["datetime"],
Type["timedelta"],
Type[List[Any]],
Type[Tuple[Any, ...]],
Type[bytes],
Type[Decimal],
Type["Decimal"],
Type[None],
]

Expand All @@ -64,8 +64,8 @@
]
SchemaDict: TypeAlias = Mapping[str, PolarsDataType]

NumericLiteral: TypeAlias = Union[int, float, Decimal]
TemporalLiteral: TypeAlias = Union[date, time, datetime, timedelta]
NumericLiteral: TypeAlias = Union[int, float, "Decimal"]
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]]
Expand Down
15 changes: 9 additions & 6 deletions py-polars/tests/unit/constructors/test_any_value_fallbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@

from datetime import date, datetime, time, timedelta
from decimal import Decimal as D
from typing import Any
from typing import TYPE_CHECKING, Any

import pytest

import polars as pl
from polars._utils.wrap import wrap_s
from polars.polars import PySeries

if TYPE_CHECKING:
from polars.type_aliases import PolarsDataType


@pytest.mark.parametrize(
("dtype", "values"),
Expand All @@ -36,7 +39,7 @@
)
@pytest.mark.parametrize("strict", [True, False])
def test_fallback_with_dtype_strict(
dtype: pl.PolarsDataType, values: list[Any], strict: bool
dtype: PolarsDataType, values: list[Any], strict: bool
) -> None:
result = wrap_s(
PySeries.new_from_any_values_and_dtype("", values, dtype, strict=strict)
Expand Down Expand Up @@ -71,7 +74,7 @@ def test_fallback_with_dtype_strict(
],
)
def test_fallback_with_dtype_strict_failure(
dtype: pl.PolarsDataType, values: list[Any]
dtype: PolarsDataType, values: list[Any]
) -> None:
with pytest.raises(TypeError, match="unexpected value"):
PySeries.new_from_any_values_and_dtype("", values, dtype, strict=True)
Expand Down Expand Up @@ -242,7 +245,7 @@ def test_fallback_with_dtype_strict_failure(
],
)
def test_fallback_with_dtype_nonstrict(
dtype: pl.PolarsDataType, values: list[Any], expected: list[Any]
dtype: PolarsDataType, values: list[Any], expected: list[Any]
) -> None:
result = wrap_s(
PySeries.new_from_any_values_and_dtype("", values, dtype, strict=False)
Expand Down Expand Up @@ -275,7 +278,7 @@ def test_fallback_with_dtype_nonstrict(
)
@pytest.mark.parametrize("strict", [True, False])
def test_fallback_without_dtype(
expected_dtype: pl.PolarsDataType, values: list[Any], strict: bool
expected_dtype: PolarsDataType, values: list[Any], strict: bool
) -> None:
result = wrap_s(PySeries.new_from_any_values("", values, strict=strict))
assert result.to_list() == values
Expand Down Expand Up @@ -340,7 +343,7 @@ def test_fallback_without_dtype_strict_failure(values: list[Any]) -> None:
)
def test_fallback_without_dtype_nonstrict_mixed_types(
values: list[Any],
expected_dtype: pl.PolarsDataType,
expected_dtype: PolarsDataType,
expected: list[Any],
) -> None:
result = wrap_s(PySeries.new_from_any_values("", values, strict=False))
Expand Down
11 changes: 6 additions & 5 deletions py-polars/tests/unit/constructors/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

import polars as pl
from polars._utils.construction.utils import try_get_type_hints
from polars.datatypes import PolarsDataType, numpy_char_code_to_dtype
from polars.datatypes import numpy_char_code_to_dtype
from polars.dependencies import dataclasses, pydantic
from polars.exceptions import ShapeError
from polars.testing import assert_frame_equal, assert_series_equal
Expand All @@ -24,7 +24,8 @@

from zoneinfo import ZoneInfo

from polars.datatypes import PolarsDataType
from polars.type_aliases import PolarsDataType

else:
from polars._utils.convert import string_to_zoneinfo as ZoneInfo

Expand Down Expand Up @@ -1300,7 +1301,7 @@ def test_from_records_nullable_structs() -> None:
{"id": 1, "items": [{"item_id": 100, "description": "hi"}]},
]

schema: list[tuple[str, pl.PolarsDataType]] = [
schema: list[tuple[str, PolarsDataType]] = [
("id", pl.UInt16),
(
"items",
Expand All @@ -1312,7 +1313,7 @@ def test_from_records_nullable_structs() -> None:
),
]

schema_options: list[list[tuple[str, pl.PolarsDataType]] | None] = [schema, None]
schema_options: list[list[tuple[str, PolarsDataType]] | None] = [schema, None]
for s in schema_options:
result = pl.DataFrame(records, schema=s, orient="row")
expected = {
Expand All @@ -1330,7 +1331,7 @@ def test_from_records_nullable_structs() -> None:
assert df.to_dict(as_series=False) == {"id": [], "items": []}
assert df.schema == dict_schema

dtype: pl.PolarsDataType = dict_schema["items"]
dtype: PolarsDataType = dict_schema["items"]
series = pl.Series("items", dtype=dtype)
assert series.to_frame().to_dict(as_series=False) == {"items": []}
assert series.dtype == dict_schema["items"]
Expand Down
9 changes: 6 additions & 3 deletions py-polars/tests/unit/constructors/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import re
from datetime import date, datetime, timedelta
from typing import Any
from typing import TYPE_CHECKING, Any

import numpy as np
import pytest
Expand All @@ -11,6 +11,9 @@
from polars.exceptions import InvalidOperationError
from polars.testing.asserts.series import assert_series_equal

if TYPE_CHECKING:
from polars.type_aliases import PolarsDataType


def test_series_mixed_dtypes_list() -> None:
values = [[0.1, 1]]
Expand Down Expand Up @@ -49,7 +52,7 @@ def test_series_mixed_dtypes_object() -> None:

# https://github.com/pola-rs/polars/issues/15139
@pytest.mark.parametrize("dtype", [pl.List(pl.Int64), None])
def test_sequence_of_series_with_dtype(dtype: pl.PolarsDataType | None) -> None:
def test_sequence_of_series_with_dtype(dtype: PolarsDataType | None) -> None:
values = [1, 2, 3]
int_series = pl.Series(values)
list_series = pl.Series([int_series], dtype=dtype)
Expand All @@ -72,7 +75,7 @@ def test_sequence_of_series_with_dtype(dtype: pl.PolarsDataType | None) -> None:
],
)
def test_upcast_primitive_and_strings(
values: list[Any], dtype: pl.PolarsDataType, expected_dtype: pl.PolarsDataType
values: list[Any], dtype: PolarsDataType, expected_dtype: PolarsDataType
) -> None:
with pytest.raises(TypeError):
pl.Series(values, dtype=dtype, strict=True)
Expand Down
2 changes: 1 addition & 1 deletion py-polars/tests/unit/datatypes/test_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from polars.testing import assert_frame_equal, assert_series_equal

if TYPE_CHECKING:
from polars import PolarsDataType
from polars.type_aliases import PolarsDataType


def test_dtype() -> None:
Expand Down
2 changes: 1 addition & 1 deletion py-polars/tests/unit/datatypes/test_struct.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from polars.testing import assert_frame_equal, assert_series_equal

if TYPE_CHECKING:
from polars.datatypes import PolarsDataType
from polars.type_aliases import PolarsDataType


def test_struct_to_list() -> None:
Expand Down
4 changes: 3 additions & 1 deletion py-polars/tests/unit/expr/test_exprs.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

if TYPE_CHECKING:
from zoneinfo import ZoneInfo

from polars.type_aliases import PolarsDataType
else:
from polars._utils.convert import string_to_zoneinfo as ZoneInfo

Expand Down Expand Up @@ -466,7 +468,7 @@ def test_logical_boolean() -> None:


def test_lit_dtypes() -> None:
def lit_series(value: Any, dtype: pl.PolarsDataType | None) -> pl.Series:
def lit_series(value: Any, dtype: PolarsDataType | None) -> pl.Series:
return pl.select(pl.lit(value, dtype=dtype)).to_series()

d = datetime(2049, 10, 5, 1, 2, 3, 987654)
Expand Down
3 changes: 1 addition & 2 deletions py-polars/tests/unit/functions/range/test_datetime_range.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
if TYPE_CHECKING:
from zoneinfo import ZoneInfo

from polars.datatypes import PolarsDataType
from polars.type_aliases import ClosedInterval, TimeUnit
from polars.type_aliases import ClosedInterval, PolarsDataType, TimeUnit
else:
from polars._utils.convert import string_to_zoneinfo as ZoneInfo

Expand Down
7 changes: 5 additions & 2 deletions py-polars/tests/unit/functions/test_lit.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import enum
from datetime import datetime, timedelta
from decimal import Decimal
from typing import Any
from typing import TYPE_CHECKING, Any

import numpy as np
import pytest
Expand All @@ -14,6 +14,9 @@
from polars.testing.parametric.strategies import series
from polars.testing.parametric.strategies.data import datetimes

if TYPE_CHECKING:
from polars.type_aliases import PolarsDataType


@pytest.mark.parametrize(
"input",
Expand Down Expand Up @@ -90,7 +93,7 @@ def test_list_datetime_11571() -> None:
pytest.param(2**63, pl.UInt64, id="above i64 max"),
],
)
def test_lit_int_return_type(input: int, dtype: pl.PolarsDataType) -> None:
def test_lit_int_return_type(input: int, dtype: PolarsDataType) -> None:
assert pl.select(pl.lit(input)).to_series().dtype == dtype


Expand Down
13 changes: 8 additions & 5 deletions py-polars/tests/unit/functions/test_repeat.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
from __future__ import annotations

from datetime import date, datetime, time, timedelta
from typing import Any
from typing import TYPE_CHECKING, Any

import pytest

import polars as pl
from polars.exceptions import ComputeError, OutOfBoundsError, SchemaError
from polars.testing import assert_frame_equal, assert_series_equal

if TYPE_CHECKING:
from polars.type_aliases import PolarsDataType


@pytest.mark.parametrize(
("value", "n", "dtype", "expected_dtype"),
Expand Down Expand Up @@ -36,8 +39,8 @@
def test_repeat(
value: Any,
n: int,
dtype: pl.PolarsDataType,
expected_dtype: pl.PolarsDataType,
dtype: PolarsDataType,
expected_dtype: PolarsDataType,
) -> None:
expected = pl.Series("repeat", [value] * n).cast(expected_dtype)

Expand Down Expand Up @@ -106,7 +109,7 @@ def test_repeat_n_negative() -> None:
def test_ones(
n: int,
value: Any,
dtype: pl.PolarsDataType,
dtype: PolarsDataType,
) -> None:
expected = pl.Series("ones", [value] * n, dtype=dtype)

Expand Down Expand Up @@ -135,7 +138,7 @@ def test_ones(
def test_zeros(
n: int,
value: Any,
dtype: pl.PolarsDataType,
dtype: PolarsDataType,
) -> None:
expected = pl.Series("zeros", [value] * n, dtype=dtype)

Expand Down
Loading

0 comments on commit 2aec475

Please sign in to comment.