Skip to content

Commit

Permalink
Address distinction between DataType and DataType() (#3857)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-beedie committed Jun 30, 2022
1 parent 3b4ea61 commit 95f4683
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
15 changes: 12 additions & 3 deletions py-polars/polars/datatypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,16 @@


class DataType:
"""Base class for all Polars data types"""
"""
Base class for all Polars data types.
"""

def __new__(cls, *args, **kwargs) -> "PolarsDataType": # type: ignore
# this formulation allows for equivalent use of "pl.Type" and "pl.Type()", while
# still respecting types that take initialisation params (eg: Duration/Datetime)
if args or kwargs:
return super().__new__(cls)
return cls

@classmethod
def string_repr(cls) -> str:
Expand Down Expand Up @@ -182,7 +191,7 @@ def __eq__(self, other: Type[DataType]) -> bool: # type: ignore
# allow comparing object instances to class
if type(other) is type and issubclass(other, Datetime):
return True
if isinstance(other, Datetime):
elif isinstance(other, Datetime):
return self.tu == other.tu and self.tz == other.tz
else:
return False
Expand All @@ -209,7 +218,7 @@ def __eq__(self, other: Type[DataType]) -> bool: # type: ignore
# allow comparing object instances to class
if type(other) is type and issubclass(other, Duration):
return True
if isinstance(other, Duration):
elif isinstance(other, Duration):
return self.tu == other.tu
else:
return False
Expand Down
2 changes: 2 additions & 0 deletions py-polars/polars/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -709,6 +709,8 @@ def draw_frames(draw: Callable) -> Union[DataFrame, LazyFrame]:
)
dtypes_ = [draw(sampled_from(selectable_dtypes)) for _ in range(n)]
coldefs = columns(cols=n, dtype=dtypes_)
elif isinstance(cols, column):
coldefs = [cols]
else:
coldefs = list(cols) # type: ignore[arg-type]

Expand Down
28 changes: 28 additions & 0 deletions py-polars/tests/test_datatypes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import inspect

import polars as pl
import polars.datatypes as datatypes


def test_dtype_init_equivalence() -> None:
# check "DataType.__new__" behaviour for all datatypes
all_datatypes = set(
dtype
for dtype in (getattr(datatypes, attr) for attr in dir(datatypes))
if inspect.isclass(dtype) and issubclass(dtype, datatypes.DataType)
)
for dtype in all_datatypes:
assert dtype == dtype()


def test_dtype_temporal_units() -> None:
# check (in)equality behaviour of temporal types that take units
for tu in datatypes.DTYPE_TEMPORAL_UNITS:
assert pl.Datetime == pl.Datetime(tu)
assert pl.Duration == pl.Duration(tu)

assert pl.Datetime(tu) == pl.Datetime() # type: ignore[operator]
assert pl.Duration(tu) == pl.Duration() # type: ignore[operator]

assert pl.Datetime("ms") != pl.Datetime("ns")
assert pl.Duration("ns") != pl.Duration("us")

0 comments on commit 95f4683

Please sign in to comment.