Skip to content

Commit d10e22a

Browse files
authored
Make the spec implementation in this repo type checkable (#187)
1 parent a0f757b commit d10e22a

File tree

9 files changed

+125
-82
lines changed

9 files changed

+125
-82
lines changed

.github/workflows/mypy.yml

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
name: mypy
2+
3+
on:
4+
pull_request:
5+
push:
6+
branches: [main]
7+
8+
jobs:
9+
tox:
10+
strategy:
11+
matrix:
12+
python-version: ["3.8", "3.11"]
13+
os: [ubuntu-latest]
14+
15+
runs-on: ${{ matrix.os }}
16+
steps:
17+
- uses: actions/checkout@v3
18+
- uses: actions/setup-python@v4
19+
with:
20+
python-version: ${{ matrix.python-version }}
21+
- name: Cache multiple paths
22+
uses: actions/cache@v3
23+
with:
24+
path: |
25+
~/.cache/pip
26+
$RUNNER_TOOL_CACHE/Python/*
27+
~\AppData\Local\pip\Cache
28+
key: ${{ runner.os }}-build-${{ matrix.python-version }}
29+
- name: install-reqs
30+
run: python -m pip install --upgrade mypy==1.4.0
31+
- name: run mypy
32+
run: cd spec/API_specification && mypy dataframe_api

spec/API_specification/.mypy.ini

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[mypy]
2+
strict=True
3+
4+
[mypy-dataframe_api.*]
5+
disable_error_code=empty-body

spec/API_specification/dataframe_api/__init__.py

+18-20
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,19 @@
66
from typing import Mapping, Sequence, Any
77

88
from .column_object import *
9-
from .dataframe_object import *
9+
from .dataframe_object import DataFrame
1010
from .groupby_object import *
11-
11+
from ._types import DType
1212

1313
__all__ = [
14-
"__dataframe_api_version",
14+
"__dataframe_api_version__",
15+
"DataFrame",
16+
"Column",
1517
"column_from_sequence",
1618
"concat",
1719
"dataframe_from_dict",
1820
"is_null",
1921
"null",
20-
"DType",
2122
"Int64",
2223
"Int32",
2324
"Int16",
@@ -59,7 +60,7 @@ def concat(dataframes: Sequence[DataFrame]) -> DataFrame:
5960
"""
6061
...
6162

62-
def column_from_sequence(sequence: Sequence[object], *, dtype: DType) -> Column:
63+
def column_from_sequence(sequence: Sequence[Any], *, dtype: Any) -> Column[Any]:
6364
"""
6465
Construct Column from sequence of elements.
6566
@@ -78,7 +79,7 @@ def column_from_sequence(sequence: Sequence[object], *, dtype: DType) -> Column:
7879
"""
7980
...
8081

81-
def dataframe_from_dict(data: Mapping[str, Column]) -> DataFrame:
82+
def dataframe_from_dict(data: Mapping[str, Column[Any]]) -> DataFrame:
8283
"""
8384
Construct DataFrame from map of column names to Columns.
8485
@@ -144,38 +145,35 @@ def is_null(value: object, /) -> bool:
144145
# Dtypes #
145146
##########
146147

147-
class DType:
148-
"""Base class for all dtypes."""
149-
150-
class Int64(DType):
148+
class Int64:
151149
"""Integer type with 64 bits of precision."""
152150

153-
class Int32(DType):
151+
class Int32:
154152
"""Integer type with 32 bits of precision."""
155153

156-
class Int16(DType):
154+
class Int16:
157155
"""Integer type with 16 bits of precision."""
158156

159-
class Int8(DType):
157+
class Int8:
160158
"""Integer type with 8 bits of precision."""
161159

162-
class UInt64(DType):
160+
class UInt64:
163161
"""Unsigned integer type with 64 bits of precision."""
164162

165-
class UInt32(DType):
163+
class UInt32:
166164
"""Unsigned integer type with 32 bits of precision."""
167165

168-
class UInt16(DType):
166+
class UInt16:
169167
"""Unsigned integer type with 16 bits of precision."""
170168

171-
class UInt8(DType):
169+
class UInt8:
172170
"""Unsigned integer type with 8 bits of precision."""
173171

174-
class Float64(DType):
172+
class Float64:
175173
"""Floating point type with 64 bits of precision."""
176174

177-
class Float32(DType):
175+
class Float32:
178176
"""Floating point type with 32 bits of precision."""
179177

180-
class Bool(DType):
178+
class Bool:
181179
"""Boolean type with 8 bits of precision."""

spec/API_specification/dataframe_api/_types.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,11 @@
2020
)
2121
from enum import Enum
2222

23+
# Type alias: Mypy needs Any, but for readability we need to make clear this
24+
# is a Python scalar (i.e., an instance of `bool`, `int`, `float`, `str`, etc.)
25+
Scalar = Any
26+
2327
array = TypeVar("array")
24-
Scalar = TypeVar("Scalar")
2528
device = TypeVar("device")
2629
DType = TypeVar("DType")
2730
SupportsDLPack = TypeVar("SupportsDLPack")

0 commit comments

Comments
 (0)