3
3
"""
4
4
5
5
import re
6
- from typing import Any , Dict , List , MutableMapping , Optional , Tuple , Type , Union , cast
6
+ from typing import (
7
+ TYPE_CHECKING ,
8
+ Any ,
9
+ Dict ,
10
+ List ,
11
+ MutableMapping ,
12
+ Optional ,
13
+ Tuple ,
14
+ Type ,
15
+ Union ,
16
+ cast ,
17
+ )
7
18
8
19
import numpy as np
9
20
import pytz
16
27
from pandas .core .dtypes .generic import ABCCategoricalIndex , ABCDateOffset , ABCIndexClass
17
28
from pandas .core .dtypes .inference import is_bool , is_list_like
18
29
30
+ if TYPE_CHECKING :
31
+ import pyarrow # noqa: F401
32
+ from pandas .core .arrays import ( # noqa: F401
33
+ IntervalArray ,
34
+ PeriodArray ,
35
+ DatetimeArray ,
36
+ )
37
+ from pandas import Categorical # noqa: F401
38
+
19
39
str_type = str
20
40
21
41
@@ -68,7 +88,7 @@ def register(self, dtype: Type[ExtensionDtype]) -> None:
68
88
"""
69
89
Parameters
70
90
----------
71
- dtype : ExtensionDtype
91
+ dtype : ExtensionDtype class
72
92
"""
73
93
if not issubclass (dtype , ExtensionDtype ):
74
94
raise ValueError ("can only register pandas extension dtypes" )
@@ -122,7 +142,7 @@ class PandasExtensionDtype(ExtensionDtype):
122
142
# and ExtensionDtype's @properties in the subclasses below. The kind and
123
143
# type variables in those subclasses are explicitly typed below.
124
144
subdtype = None
125
- str : Optional [ str_type ] = None
145
+ str : str_type
126
146
num = 100
127
147
shape : Tuple [int , ...] = tuple ()
128
148
itemsize = 8
@@ -500,15 +520,15 @@ def _hash_categories(categories, ordered: Ordered = True) -> int:
500
520
return np .bitwise_xor .reduce (hashed )
501
521
502
522
@classmethod
503
- def construct_array_type (cls ):
523
+ def construct_array_type (cls ) -> Type [ "Categorical" ] :
504
524
"""
505
525
Return the array type associated with this dtype.
506
526
507
527
Returns
508
528
-------
509
529
type
510
530
"""
511
- from pandas import Categorical
531
+ from pandas import Categorical # noqa: F811
512
532
513
533
return Categorical
514
534
@@ -672,9 +692,9 @@ class DatetimeTZDtype(PandasExtensionDtype):
672
692
_match = re .compile (r"(datetime64|M8)\[(?P<unit>.+), (?P<tz>.+)\]" )
673
693
_cache : Dict [str_type , PandasExtensionDtype ] = {}
674
694
675
- def __init__ (self , unit = "ns" , tz = None ):
695
+ def __init__ (self , unit : Union [ str_type , "DatetimeTZDtype" ] = "ns" , tz = None ):
676
696
if isinstance (unit , DatetimeTZDtype ):
677
- unit , tz = unit .unit , unit .tz
697
+ unit , tz = unit .unit , unit .tz # type: ignore
678
698
679
699
if unit != "ns" :
680
700
if isinstance (unit , str ) and tz is None :
@@ -704,7 +724,7 @@ def __init__(self, unit="ns", tz=None):
704
724
self ._tz = tz
705
725
706
726
@property
707
- def unit (self ):
727
+ def unit (self ) -> str_type :
708
728
"""
709
729
The precision of the datetime data.
710
730
"""
@@ -718,20 +738,20 @@ def tz(self):
718
738
return self ._tz
719
739
720
740
@classmethod
721
- def construct_array_type (cls ):
741
+ def construct_array_type (cls ) -> Type [ "DatetimeArray" ] :
722
742
"""
723
743
Return the array type associated with this dtype.
724
744
725
745
Returns
726
746
-------
727
747
type
728
748
"""
729
- from pandas .core .arrays import DatetimeArray
749
+ from pandas .core .arrays import DatetimeArray # noqa: F811
730
750
731
751
return DatetimeArray
732
752
733
753
@classmethod
734
- def construct_from_string (cls , string : str_type ):
754
+ def construct_from_string (cls , string : str_type ) -> "DatetimeTZDtype" :
735
755
"""
736
756
Construct a DatetimeTZDtype from a string.
737
757
@@ -789,7 +809,7 @@ def __eq__(self, other: Any) -> bool:
789
809
and str (self .tz ) == str (other .tz )
790
810
)
791
811
792
- def __setstate__ (self , state ):
812
+ def __setstate__ (self , state ) -> None :
793
813
# for pickle compat. __get_state__ is defined in the
794
814
# PandasExtensionDtype superclass and uses the public properties to
795
815
# pickle -> need to set the settable private ones here (see GH26067)
@@ -884,7 +904,7 @@ def _parse_dtype_strict(cls, freq):
884
904
raise ValueError ("could not construct PeriodDtype" )
885
905
886
906
@classmethod
887
- def construct_from_string (cls , string ) :
907
+ def construct_from_string (cls , string : str_type ) -> "PeriodDtype" :
888
908
"""
889
909
Strict construction from a string, raise a TypeError if not
890
910
possible
@@ -934,7 +954,7 @@ def __setstate__(self, state):
934
954
self ._freq = state ["freq" ]
935
955
936
956
@classmethod
937
- def is_dtype (cls , dtype ) -> bool :
957
+ def is_dtype (cls , dtype : object ) -> bool :
938
958
"""
939
959
Return a boolean if we if the passed type is an actual dtype that we
940
960
can match (via string or type)
@@ -955,7 +975,7 @@ def is_dtype(cls, dtype) -> bool:
955
975
return super ().is_dtype (dtype )
956
976
957
977
@classmethod
958
- def construct_array_type (cls ):
978
+ def construct_array_type (cls ) -> Type [ "PeriodArray" ] :
959
979
"""
960
980
Return the array type associated with this dtype.
961
981
@@ -967,9 +987,13 @@ def construct_array_type(cls):
967
987
968
988
return PeriodArray
969
989
970
- def __from_arrow__ (self , array ):
971
- """Construct PeriodArray from pyarrow Array/ChunkedArray."""
972
- import pyarrow
990
+ def __from_arrow__ (
991
+ self , array : Union ["pyarrow.Array" , "pyarrow.ChunkedArray" ]
992
+ ) -> "PeriodArray" :
993
+ """
994
+ Construct PeriodArray from pyarrow Array/ChunkedArray.
995
+ """
996
+ import pyarrow # noqa: F811
973
997
from pandas .core .arrays import PeriodArray
974
998
from pandas .core .arrays ._arrow_utils import pyarrow_array_to_numpy_and_mask
975
999
@@ -1075,7 +1099,7 @@ def subtype(self):
1075
1099
return self ._subtype
1076
1100
1077
1101
@classmethod
1078
- def construct_array_type (cls ):
1102
+ def construct_array_type (cls ) -> Type [ "IntervalArray" ] :
1079
1103
"""
1080
1104
Return the array type associated with this dtype.
1081
1105
@@ -1142,7 +1166,7 @@ def __setstate__(self, state):
1142
1166
self ._subtype = state ["subtype" ]
1143
1167
1144
1168
@classmethod
1145
- def is_dtype (cls , dtype ) -> bool :
1169
+ def is_dtype (cls , dtype : object ) -> bool :
1146
1170
"""
1147
1171
Return a boolean if we if the passed type is an actual dtype that we
1148
1172
can match (via string or type)
@@ -1160,9 +1184,13 @@ def is_dtype(cls, dtype) -> bool:
1160
1184
return False
1161
1185
return super ().is_dtype (dtype )
1162
1186
1163
- def __from_arrow__ (self , array ):
1164
- """Construct IntervalArray from pyarrow Array/ChunkedArray."""
1165
- import pyarrow
1187
+ def __from_arrow__ (
1188
+ self , array : Union ["pyarrow.Array" , "pyarrow.ChunkedArray" ]
1189
+ ) -> "IntervalArray" :
1190
+ """
1191
+ Construct IntervalArray from pyarrow Array/ChunkedArray.
1192
+ """
1193
+ import pyarrow # noqa: F811
1166
1194
from pandas .core .arrays import IntervalArray
1167
1195
1168
1196
if isinstance (array , pyarrow .Array ):
0 commit comments