Skip to content

Commit 22c2b73

Browse files
gwromeWillAyd
authored andcommitted
Fix type annotations in pandas.core.dtypes (#26029)
1 parent feefca8 commit 22c2b73

File tree

4 files changed

+28
-27
lines changed

4 files changed

+28
-27
lines changed

mypy.ini

-6
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,6 @@ ignore_errors=True
5959
[mypy-pandas.core.config_init]
6060
ignore_errors=True
6161

62-
[mypy-pandas.core.dtypes.dtypes]
63-
ignore_errors=True
64-
65-
[mypy-pandas.core.dtypes.missing]
66-
ignore_errors=True
67-
6862
[mypy-pandas.core.frame]
6963
ignore_errors=True
7064

pandas/core/dtypes/base.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""Extend pandas with custom array types"""
2-
from typing import List, Optional, Type
2+
from typing import List, Optional, Tuple, Type
33

44
import numpy as np
55

@@ -24,7 +24,7 @@ class _DtypeOpsMixin(object):
2424
# of the NA value, not the physical NA vaalue for storage.
2525
# e.g. for JSONArray, this is an empty dictionary.
2626
na_value = np.nan
27-
_metadata = ()
27+
_metadata = () # type: Tuple[str, ...]
2828

2929
def __eq__(self, other):
3030
"""Check whether 'other' is equal to self.
@@ -219,8 +219,7 @@ def type(self) -> Type:
219219
raise AbstractMethodError(self)
220220

221221
@property
222-
def kind(self):
223-
# type () -> str
222+
def kind(self) -> str:
224223
"""
225224
A character code (one of 'biufcmMOSUV'), default 'O'
226225

pandas/core/dtypes/dtypes.py

+23-16
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
""" define extension dtypes """
22
import re
3+
from typing import Any, Dict, Optional, Tuple, Type
34
import warnings
45

56
import numpy as np
@@ -16,6 +17,8 @@
1617
from .base import ExtensionDtype, _DtypeOpsMixin
1718
from .inference import is_list_like
1819

20+
str_type = str
21+
1922

2023
def register_extension_dtype(cls):
2124
"""
@@ -104,17 +107,21 @@ class PandasExtensionDtype(_DtypeOpsMixin):
104107
105108
THIS IS NOT A REAL NUMPY DTYPE
106109
"""
107-
type = None
110+
type = None # type: Any
111+
kind = None # type: Any
112+
# The Any type annotations above are here only because mypy seems to have a
113+
# problem dealing with with multiple inheritance from PandasExtensionDtype
114+
# and ExtensionDtype's @properties in the subclasses below. The kind and
115+
# type variables in those subclasses are explicitly typed below.
108116
subdtype = None
109-
kind = None
110-
str = None
117+
str = None # type: Optional[str_type]
111118
num = 100
112-
shape = tuple()
119+
shape = tuple() # type: Tuple[int, ...]
113120
itemsize = 8
114121
base = None
115122
isbuiltin = 0
116123
isnative = 0
117-
_cache = {}
124+
_cache = {} # type: Dict[str_type, 'PandasExtensionDtype']
118125

119126
def __unicode__(self):
120127
return self.name
@@ -217,12 +224,12 @@ class CategoricalDtype(PandasExtensionDtype, ExtensionDtype):
217224
"""
218225
# TODO: Document public vs. private API
219226
name = 'category'
220-
type = CategoricalDtypeType
221-
kind = 'O'
227+
type = CategoricalDtypeType # type: Type[CategoricalDtypeType]
228+
kind = 'O' # type: str_type
222229
str = '|O08'
223230
base = np.dtype('O')
224231
_metadata = ('categories', 'ordered')
225-
_cache = {}
232+
_cache = {} # type: Dict[str_type, PandasExtensionDtype]
226233

227234
def __init__(self, categories=None, ordered=None):
228235
self._finalize(categories, ordered, fastpath=False)
@@ -584,15 +591,15 @@ class DatetimeTZDtype(PandasExtensionDtype, ExtensionDtype):
584591
THIS IS NOT A REAL NUMPY DTYPE, but essentially a sub-class of
585592
np.datetime64[ns]
586593
"""
587-
type = Timestamp
588-
kind = 'M'
594+
type = Timestamp # type: Type[Timestamp]
595+
kind = 'M' # type: str_type
589596
str = '|M8[ns]'
590597
num = 101
591598
base = np.dtype('M8[ns]')
592599
na_value = NaT
593600
_metadata = ('unit', 'tz')
594601
_match = re.compile(r"(datetime64|M8)\[(?P<unit>.+), (?P<tz>.+)\]")
595-
_cache = {}
602+
_cache = {} # type: Dict[str_type, PandasExtensionDtype]
596603

597604
def __init__(self, unit="ns", tz=None):
598605
"""
@@ -736,14 +743,14 @@ class PeriodDtype(ExtensionDtype, PandasExtensionDtype):
736743
737744
THIS IS NOT A REAL NUMPY DTYPE, but essentially a sub-class of np.int64.
738745
"""
739-
type = Period
740-
kind = 'O'
746+
type = Period # type: Type[Period]
747+
kind = 'O' # type: str_type
741748
str = '|O08'
742749
base = np.dtype('O')
743750
num = 102
744751
_metadata = ('freq',)
745752
_match = re.compile(r"(P|p)eriod\[(?P<freq>.+)\]")
746-
_cache = {}
753+
_cache = {} # type: Dict[str_type, PandasExtensionDtype]
747754

748755
def __new__(cls, freq=None):
749756
"""
@@ -860,13 +867,13 @@ class IntervalDtype(PandasExtensionDtype, ExtensionDtype):
860867
THIS IS NOT A REAL NUMPY DTYPE
861868
"""
862869
name = 'interval'
863-
kind = None
870+
kind = None # type: Optional[str_type]
864871
str = '|O08'
865872
base = np.dtype('O')
866873
num = 103
867874
_metadata = ('subtype',)
868875
_match = re.compile(r"(I|i)nterval\[(?P<subtype>.+)\]")
869-
_cache = {}
876+
_cache = {} # type: Dict[str_type, PandasExtensionDtype]
870877

871878
def __new__(cls, subtype=None):
872879
"""

pandas/core/dtypes/missing.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
"""
44
import numpy as np
55

6-
from pandas._libs import lib, missing as libmissing
6+
from pandas._libs import lib
7+
import pandas._libs.missing as libmissing
78
from pandas._libs.tslibs import NaT, iNaT
89

910
from .common import (

0 commit comments

Comments
 (0)