|
1 | 1 | """ define extension dtypes """
|
2 | 2 | import re
|
| 3 | +from typing import Any, Dict, Optional, Tuple, Type |
3 | 4 | import warnings
|
4 | 5 |
|
5 | 6 | import numpy as np
|
|
16 | 17 | from .base import ExtensionDtype, _DtypeOpsMixin
|
17 | 18 | from .inference import is_list_like
|
18 | 19 |
|
| 20 | +str_type = str |
| 21 | + |
19 | 22 |
|
20 | 23 | def register_extension_dtype(cls):
|
21 | 24 | """
|
@@ -104,17 +107,21 @@ class PandasExtensionDtype(_DtypeOpsMixin):
|
104 | 107 |
|
105 | 108 | THIS IS NOT A REAL NUMPY DTYPE
|
106 | 109 | """
|
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. |
108 | 116 | subdtype = None
|
109 |
| - kind = None |
110 |
| - str = None |
| 117 | + str = None # type: Optional[str_type] |
111 | 118 | num = 100
|
112 |
| - shape = tuple() |
| 119 | + shape = tuple() # type: Tuple[int, ...] |
113 | 120 | itemsize = 8
|
114 | 121 | base = None
|
115 | 122 | isbuiltin = 0
|
116 | 123 | isnative = 0
|
117 |
| - _cache = {} |
| 124 | + _cache = {} # type: Dict[str_type, 'PandasExtensionDtype'] |
118 | 125 |
|
119 | 126 | def __unicode__(self):
|
120 | 127 | return self.name
|
@@ -217,12 +224,12 @@ class CategoricalDtype(PandasExtensionDtype, ExtensionDtype):
|
217 | 224 | """
|
218 | 225 | # TODO: Document public vs. private API
|
219 | 226 | name = 'category'
|
220 |
| - type = CategoricalDtypeType |
221 |
| - kind = 'O' |
| 227 | + type = CategoricalDtypeType # type: Type[CategoricalDtypeType] |
| 228 | + kind = 'O' # type: str_type |
222 | 229 | str = '|O08'
|
223 | 230 | base = np.dtype('O')
|
224 | 231 | _metadata = ('categories', 'ordered')
|
225 |
| - _cache = {} |
| 232 | + _cache = {} # type: Dict[str_type, PandasExtensionDtype] |
226 | 233 |
|
227 | 234 | def __init__(self, categories=None, ordered=None):
|
228 | 235 | self._finalize(categories, ordered, fastpath=False)
|
@@ -584,15 +591,15 @@ class DatetimeTZDtype(PandasExtensionDtype, ExtensionDtype):
|
584 | 591 | THIS IS NOT A REAL NUMPY DTYPE, but essentially a sub-class of
|
585 | 592 | np.datetime64[ns]
|
586 | 593 | """
|
587 |
| - type = Timestamp |
588 |
| - kind = 'M' |
| 594 | + type = Timestamp # type: Type[Timestamp] |
| 595 | + kind = 'M' # type: str_type |
589 | 596 | str = '|M8[ns]'
|
590 | 597 | num = 101
|
591 | 598 | base = np.dtype('M8[ns]')
|
592 | 599 | na_value = NaT
|
593 | 600 | _metadata = ('unit', 'tz')
|
594 | 601 | _match = re.compile(r"(datetime64|M8)\[(?P<unit>.+), (?P<tz>.+)\]")
|
595 |
| - _cache = {} |
| 602 | + _cache = {} # type: Dict[str_type, PandasExtensionDtype] |
596 | 603 |
|
597 | 604 | def __init__(self, unit="ns", tz=None):
|
598 | 605 | """
|
@@ -736,14 +743,14 @@ class PeriodDtype(ExtensionDtype, PandasExtensionDtype):
|
736 | 743 |
|
737 | 744 | THIS IS NOT A REAL NUMPY DTYPE, but essentially a sub-class of np.int64.
|
738 | 745 | """
|
739 |
| - type = Period |
740 |
| - kind = 'O' |
| 746 | + type = Period # type: Type[Period] |
| 747 | + kind = 'O' # type: str_type |
741 | 748 | str = '|O08'
|
742 | 749 | base = np.dtype('O')
|
743 | 750 | num = 102
|
744 | 751 | _metadata = ('freq',)
|
745 | 752 | _match = re.compile(r"(P|p)eriod\[(?P<freq>.+)\]")
|
746 |
| - _cache = {} |
| 753 | + _cache = {} # type: Dict[str_type, PandasExtensionDtype] |
747 | 754 |
|
748 | 755 | def __new__(cls, freq=None):
|
749 | 756 | """
|
@@ -860,13 +867,13 @@ class IntervalDtype(PandasExtensionDtype, ExtensionDtype):
|
860 | 867 | THIS IS NOT A REAL NUMPY DTYPE
|
861 | 868 | """
|
862 | 869 | name = 'interval'
|
863 |
| - kind = None |
| 870 | + kind = None # type: Optional[str_type] |
864 | 871 | str = '|O08'
|
865 | 872 | base = np.dtype('O')
|
866 | 873 | num = 103
|
867 | 874 | _metadata = ('subtype',)
|
868 | 875 | _match = re.compile(r"(I|i)nterval\[(?P<subtype>.+)\]")
|
869 |
| - _cache = {} |
| 876 | + _cache = {} # type: Dict[str_type, PandasExtensionDtype] |
870 | 877 |
|
871 | 878 | def __new__(cls, subtype=None):
|
872 | 879 | """
|
|
0 commit comments