Skip to content

Commit

Permalink
feat(python)!: Remove default class variable values on DataTypes (pol…
Browse files Browse the repository at this point in the history
  • Loading branch information
stinodego authored and Wouittone committed Jun 22, 2024
1 parent 5e54c78 commit c566985
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 30 deletions.
28 changes: 6 additions & 22 deletions py-polars/polars/datatypes/classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -434,23 +434,12 @@ class Datetime(TemporalType):
epoch.
"""

time_unit: TimeUnit | None = None
time_zone: str | None = None
time_unit: TimeUnit
time_zone: str | None

def __init__(
self, time_unit: TimeUnit = "us", time_zone: str | timezone | None = None
):
if time_unit is None:
from polars._utils.deprecation import issue_deprecation_warning

issue_deprecation_warning(
"Passing `time_unit=None` to the Datetime constructor is deprecated."
" Either avoid passing a time unit to use the default value ('us'),"
" or pass a valid time unit instead ('ms', 'us', 'ns').",
version="0.20.11",
)
time_unit = "us"

if time_unit not in ("ms", "us", "ns"):
msg = (
"invalid `time_unit`"
Expand Down Expand Up @@ -501,7 +490,7 @@ class Duration(TemporalType):
negative time offsets.
"""

time_unit: TimeUnit | None = None
time_unit: TimeUnit

def __init__(self, time_unit: TimeUnit = "us"):
if time_unit not in ("ms", "us", "ns"):
Expand Down Expand Up @@ -680,7 +669,7 @@ class List(NestedType):
└───────────────┴─────────────┘
"""

inner: PolarsDataType | None = None
inner: PolarsDataType

def __init__(self, inner: PolarsDataType | PythonDataType):
self.inner = polars.datatypes.py_type_to_dtype(inner)
Expand All @@ -696,10 +685,7 @@ def __eq__(self, other: PolarsDataType) -> bool: # type: ignore[override]
if type(other) is DataTypeClass and issubclass(other, List):
return True
elif isinstance(other, List):
if self.inner is None or other.inner is None:
return True
else:
return self.inner == other.inner
return self.inner == other.inner
else:
return False

Expand Down Expand Up @@ -734,7 +720,7 @@ class Array(NestedType):
]
"""

inner: PolarsDataType | None = None
inner: PolarsDataType
size: int
shape: tuple[int, ...]

Expand Down Expand Up @@ -790,8 +776,6 @@ def __eq__(self, other: PolarsDataType) -> bool: # type: ignore[override]
elif isinstance(other, Array):
if self.shape != other.shape:
return False
elif self.inner is None or other.inner is None:
return True
else:
return self.inner == other.inner
else:
Expand Down
4 changes: 2 additions & 2 deletions py-polars/polars/interchange/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,14 @@ def polars_dtype_to_dtype(dtype: PolarsDataType) -> Dtype:


def _datetime_to_dtype(dtype: Datetime) -> Dtype:
tu = dtype.time_unit[0] if dtype.time_unit is not None else "u"
tu = dtype.time_unit[0]
tz = dtype.time_zone if dtype.time_zone is not None else ""
arrow_c_type = f"ts{tu}:{tz}"
return DtypeKind.DATETIME, 64, arrow_c_type, NE


def _duration_to_dtype(dtype: Duration) -> Dtype:
tu = dtype.time_unit[0] if dtype.time_unit is not None else "u"
tu = dtype.time_unit[0]
arrow_c_type = f"tD{tu}"
return DtypeKind.DATETIME, 64, arrow_c_type, NE

Expand Down
6 changes: 0 additions & 6 deletions py-polars/tests/unit/datatypes/test_temporal.py
Original file line number Diff line number Diff line change
Expand Up @@ -2791,9 +2791,3 @@ def test_misc_precision_any_value_conversion(time_zone: Any, warn: bool) -> None
def test_pytime_conversion(tm: time) -> None:
s = pl.Series("tm", [tm])
assert s.to_list() == [tm]


def test_datetime_time_unit_none_deprecated() -> None:
with pytest.deprecated_call():
dtype = pl.Datetime(time_unit=None) # type: ignore[arg-type]
assert dtype.time_unit == "us"

0 comments on commit c566985

Please sign in to comment.