Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ repos:
pass_filenames: false
entry: python -m utils.sort_api_reference
language: python
- id: check-slotted-classes
name: check-slotted-classes
pass_filenames: false
entry: python -m utils.check_slotted_classes
language: python
- id: imports-are-banned
name: import are banned (use `get_pandas` instead of `import pandas`)
entry: python utils/import_check.py
Expand Down
68 changes: 68 additions & 0 deletions narwhals/dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ def _validate_into_dtype(dtype: Any) -> None:


class DType:
__slots__ = ()

def __repr__(self) -> str: # pragma: no cover
return self.__class__.__qualname__

Expand Down Expand Up @@ -152,30 +154,44 @@ def __hash__(self) -> int:
class NumericType(DType):
"""Base class for numeric data types."""

__slots__ = ()


class IntegerType(NumericType):
"""Base class for integer data types."""

__slots__ = ()


class SignedIntegerType(IntegerType):
"""Base class for signed integer data types."""

__slots__ = ()


class UnsignedIntegerType(IntegerType):
"""Base class for unsigned integer data types."""

__slots__ = ()


class FloatType(NumericType):
"""Base class for float data types."""

__slots__ = ()


class TemporalType(DType):
"""Base class for temporal data types."""

__slots__ = ()


class NestedType(DType):
"""Base class for nested data types."""

__slots__ = ()


class Decimal(NumericType):
"""Decimal type.
Expand All @@ -188,6 +204,8 @@ class Decimal(NumericType):
Decimal
"""

__slots__ = ()


class Int128(SignedIntegerType):
"""128-bit signed integer type.
Expand All @@ -208,6 +226,8 @@ class Int128(SignedIntegerType):
Int128
"""

__slots__ = ()


class Int64(SignedIntegerType):
"""64-bit signed integer type.
Expand All @@ -221,6 +241,8 @@ class Int64(SignedIntegerType):
Int64
"""

__slots__ = ()


class Int32(SignedIntegerType):
"""32-bit signed integer type.
Expand All @@ -234,6 +256,8 @@ class Int32(SignedIntegerType):
Int32
"""

__slots__ = ()


class Int16(SignedIntegerType):
"""16-bit signed integer type.
Expand All @@ -247,6 +271,8 @@ class Int16(SignedIntegerType):
Int16
"""

__slots__ = ()


class Int8(SignedIntegerType):
"""8-bit signed integer type.
Expand All @@ -260,6 +286,8 @@ class Int8(SignedIntegerType):
Int8
"""

__slots__ = ()


class UInt128(UnsignedIntegerType):
"""128-bit unsigned integer type.
Expand All @@ -274,6 +302,8 @@ class UInt128(UnsignedIntegerType):
UInt128
"""

__slots__ = ()


class UInt64(UnsignedIntegerType):
"""64-bit unsigned integer type.
Expand All @@ -287,6 +317,8 @@ class UInt64(UnsignedIntegerType):
UInt64
"""

__slots__ = ()


class UInt32(UnsignedIntegerType):
"""32-bit unsigned integer type.
Expand All @@ -300,6 +332,8 @@ class UInt32(UnsignedIntegerType):
UInt32
"""

__slots__ = ()


class UInt16(UnsignedIntegerType):
"""16-bit unsigned integer type.
Expand All @@ -313,6 +347,8 @@ class UInt16(UnsignedIntegerType):
UInt16
"""

__slots__ = ()


class UInt8(UnsignedIntegerType):
"""8-bit unsigned integer type.
Expand All @@ -326,6 +362,8 @@ class UInt8(UnsignedIntegerType):
UInt8
"""

__slots__ = ()


class Float64(FloatType):
"""64-bit floating point type.
Expand All @@ -339,6 +377,8 @@ class Float64(FloatType):
Float64
"""

__slots__ = ()


class Float32(FloatType):
"""32-bit floating point type.
Expand All @@ -352,6 +392,8 @@ class Float32(FloatType):
Float32
"""

__slots__ = ()


class String(DType):
"""UTF-8 encoded string type.
Expand All @@ -364,6 +406,8 @@ class String(DType):
String
"""

__slots__ = ()


class Boolean(DType):
"""Boolean type.
Expand All @@ -376,6 +420,8 @@ class Boolean(DType):
Boolean
"""

__slots__ = ()


class Object(DType):
"""Data type for wrapping arbitrary Python objects.
Expand All @@ -389,6 +435,8 @@ class Object(DType):
Object
"""

__slots__ = ()


class Unknown(DType):
"""Type representing DataType values that could not be determined statically.
Expand All @@ -401,6 +449,8 @@ class Unknown(DType):
Unknown
"""

__slots__ = ()


class _DatetimeMeta(type):
@property
Expand Down Expand Up @@ -438,6 +488,8 @@ class Datetime(TemporalType, metaclass=_DatetimeMeta):
Datetime(time_unit='ms', time_zone='Africa/Accra')
"""

__slots__ = ("time_unit", "time_zone")

def __init__(
self, time_unit: TimeUnit = "us", time_zone: str | timezone | None = None
) -> None:
Expand Down Expand Up @@ -521,6 +573,8 @@ class Duration(TemporalType, metaclass=_DurationMeta):
Duration(time_unit='ms')
"""

__slots__ = ("time_unit",)

def __init__(self, time_unit: TimeUnit = "us") -> None:
if time_unit not in {"s", "ms", "us", "ns"}:
msg = (
Expand Down Expand Up @@ -573,6 +627,8 @@ class Categorical(DType):
Categorical
"""

__slots__ = ()


class Enum(DType):
"""A fixed categorical encoding of a unique set of strings.
Expand All @@ -586,6 +642,8 @@ class Enum(DType):
Enum(categories=['beluga', 'narwhal', 'orca'])
"""

__slots__ = ("_cached_categories", "_delayed_categories")

def __init__(self, categories: Iterable[str] | type[enum.Enum]) -> None:
self._delayed_categories: _DeferredIterable[str] | None = None
self._cached_categories: tuple[str, ...] | None = None
Expand Down Expand Up @@ -655,6 +713,7 @@ class Field:
[Field('a', Int64), Field('b', List(String))]
"""

__slots__ = ("dtype", "name")
name: str
"""The name of the field within its parent `Struct`."""
dtype: IntoDType
Expand Down Expand Up @@ -713,6 +772,7 @@ class Struct(NestedType):
Struct({'a': Int64, 'b': List(String)})
"""

__slots__ = ("fields",)
fields: list[Field]
"""The fields that make up the struct."""

Expand Down Expand Up @@ -782,6 +842,7 @@ class List(NestedType):
List(String)
"""

__slots__ = ("inner",)
inner: IntoDType
"""The DType of the values within each list."""

Expand Down Expand Up @@ -832,6 +893,7 @@ class Array(NestedType):
Array(Int32, shape=(2,))
"""

__slots__ = ("inner", "shape", "size")
inner: IntoDType
"""The DType of the values within each array."""
size: int
Expand Down Expand Up @@ -910,6 +972,8 @@ class Date(TemporalType):
Date
"""

__slots__ = ()


class Time(TemporalType):
"""Data type representing the time of day.
Expand All @@ -935,6 +999,8 @@ class Time(TemporalType):
Time
"""

__slots__ = ()


class Binary(DType):
"""Binary type.
Expand All @@ -958,3 +1024,5 @@ class Binary(DType):
>>> nw.from_native(rel).collect_schema()["t"]
Binary
"""

__slots__ = ()
6 changes: 6 additions & 0 deletions narwhals/stable/v1/_dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@


class Datetime(NwDatetime):
__slots__ = NwDatetime.__slots__

@inherit_doc(NwDatetime)
def __init__(
self, time_unit: TimeUnit = "us", time_zone: str | timezone | None = None
Expand All @@ -59,6 +61,8 @@ def __hash__(self) -> int:


class Duration(NwDuration):
__slots__ = NwDuration.__slots__

@inherit_doc(NwDuration)
def __init__(self, time_unit: TimeUnit = "us") -> None:
super().__init__(time_unit)
Expand All @@ -81,6 +85,8 @@ class Enum(NwEnum):
Enum
"""

__slots__ = ()

def __init__(self) -> None:
super(NwEnum, self).__init__()

Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,8 @@ extend-ignore-names = [
"PLR0916", # too-many-boolean-expressions
]
"tpch/tests/*" = ["S101"]
"utils/*" = ["S311"]
"utils/bump_version.py" = ["S603", "S607", "T201"]
"utils/*" = ["S311", "T201"]
"utils/bump_version.py" = ["S603", "S607"]
"tpch/execute/*" = ["T201"]
"tpch/notebooks/*" = [
"ANN001",
Expand Down
Loading
Loading