Skip to content

remove unused typevars, make dtype type alias #265

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 28, 2023
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
55 changes: 10 additions & 45 deletions spec/API_specification/dataframe_api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
"""
from __future__ import annotations

from typing import Mapping, Sequence, Any
from typing import Mapping, Sequence, Any, TYPE_CHECKING

from .column_object import *
from .dataframe_object import DataFrame
from .groupby_object import *
from ._types import DType
from .dtypes import *

if TYPE_CHECKING:
from ._types import DType

__all__ = [
"__dataframe_api_version__",
Expand Down Expand Up @@ -63,7 +66,7 @@ def concat(dataframes: Sequence[DataFrame]) -> DataFrame:
"""
...

def column_from_sequence(sequence: Sequence[Any], *, dtype: Any, name: str = '', api_version: str | None = None) -> Column[Any]:
def column_from_sequence(sequence: Sequence[Any], *, dtype: DType, name: str = '', api_version: str | None = None) -> Column:
"""
Construct Column from sequence of elements.

Expand Down Expand Up @@ -91,7 +94,7 @@ def column_from_sequence(sequence: Sequence[Any], *, dtype: Any, name: str = '',
"""
...

def dataframe_from_dict(data: Mapping[str, Column[Any]], *, api_version: str | None = None) -> DataFrame:
def dataframe_from_dict(data: Mapping[str, Column], *, api_version: str | None = None) -> DataFrame:
"""
Construct DataFrame from map of column names to Columns.

Expand Down Expand Up @@ -123,7 +126,7 @@ def dataframe_from_dict(data: Mapping[str, Column[Any]], *, api_version: str | N
...


def column_from_1d_array(array: Any, *, dtype: Any, name: str = '', api_version: str | None = None) -> Column[Any]:
def column_from_1d_array(array: Any, *, dtype: DType, name: str = '', api_version: str | None = None) -> Column:
"""
Construct Column from 1D array.

Expand Down Expand Up @@ -232,51 +235,13 @@ def is_null(value: object, /) -> bool:

"""

##########
# Dtypes #
##########

class Int64:
"""Integer type with 64 bits of precision."""

class Int32:
"""Integer type with 32 bits of precision."""

class Int16:
"""Integer type with 16 bits of precision."""

class Int8:
"""Integer type with 8 bits of precision."""

class UInt64:
"""Unsigned integer type with 64 bits of precision."""

class UInt32:
"""Unsigned integer type with 32 bits of precision."""

class UInt16:
"""Unsigned integer type with 16 bits of precision."""

class UInt8:
"""Unsigned integer type with 8 bits of precision."""

class Float64:
"""Floating point type with 64 bits of precision."""

class Float32:
"""Floating point type with 32 bits of precision."""

class Bool:
"""Boolean type with 8 bits of precision."""


def is_dtype(dtype: Any, kind: str | tuple[str, ...]) -> bool:
def is_dtype(dtype: DType, kind: str | tuple[str, ...]) -> bool:
"""
Returns a boolean indicating whether a provided dtype is of a specified data type “kind”.

Parameters
----------
dtype: Any
dtype: DType
The input dtype.
kind: str
data type kind.
Expand Down
43 changes: 18 additions & 25 deletions spec/API_specification/dataframe_api/_types.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
"""
Types for type annotations used in the dataframe API standard.

The type variables should be replaced with the actual types for a given
library, e.g., for Pandas TypeVar('DataFrame') would be replaced with pd.DataFrame.
"""
from __future__ import annotations

Expand All @@ -14,39 +11,35 @@
Optional,
Sequence,
Tuple,
TypeVar,
Union,
Protocol,
TYPE_CHECKING,
)
from enum import Enum

if TYPE_CHECKING:
from .dtypes import (
Bool,
Float64,
Float32,
Int64,
Int32,
Int16,
Int8,
UInt64,
UInt32,
UInt16,
UInt8,
)

DType = Union[Bool, Float64, Float32, Int64, Int32, Int16, Int8, UInt64, UInt32, UInt16, UInt8]

# Type alias: Mypy needs Any, but for readability we need to make clear this
# is a Python scalar (i.e., an instance of `bool`, `int`, `float`, `str`, etc.)
Scalar = Any
# null is a special object which represents a missing value.
# It is not valid as a type.
NullType = Any

array = TypeVar("array")
device = TypeVar("device")
DType = TypeVar("DType")
SupportsDLPack = TypeVar("SupportsDLPack")
SupportsBufferProtocol = TypeVar("SupportsBufferProtocol")
PyCapsule = TypeVar("PyCapsule")
# ellipsis cannot actually be imported from anywhere, so include a dummy here
# to keep pyflakes happy. https://github.com/python/typeshed/issues/3556
ellipsis = TypeVar("ellipsis")

_T_co = TypeVar("_T_co", covariant=True)


class NestedSequence(Protocol[_T_co]):
def __getitem__(self, key: int, /) -> Union[_T_co, NestedSequence[_T_co]]:
...

def __len__(self, /) -> int:
...


__all__ = [
"Any",
Expand Down
Loading