33"""
44
55import 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+ )
718
819import numpy as np
920import pytz
1627from pandas .core .dtypes .generic import ABCCategoricalIndex , ABCDateOffset , ABCIndexClass
1728from pandas .core .dtypes .inference import is_bool , is_list_like
1829
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+
1939str_type = str
2040
2141
@@ -68,7 +88,7 @@ def register(self, dtype: Type[ExtensionDtype]) -> None:
6888 """
6989 Parameters
7090 ----------
71- dtype : ExtensionDtype
91+ dtype : ExtensionDtype class
7292 """
7393 if not issubclass (dtype , ExtensionDtype ):
7494 raise ValueError ("can only register pandas extension dtypes" )
@@ -122,7 +142,7 @@ class PandasExtensionDtype(ExtensionDtype):
122142 # and ExtensionDtype's @properties in the subclasses below. The kind and
123143 # type variables in those subclasses are explicitly typed below.
124144 subdtype = None
125- str : Optional [ str_type ] = None
145+ str : str_type
126146 num = 100
127147 shape : Tuple [int , ...] = tuple ()
128148 itemsize = 8
@@ -500,15 +520,15 @@ def _hash_categories(categories, ordered: Ordered = True) -> int:
500520 return np .bitwise_xor .reduce (hashed )
501521
502522 @classmethod
503- def construct_array_type (cls ):
523+ def construct_array_type (cls ) -> Type [ "Categorical" ] :
504524 """
505525 Return the array type associated with this dtype.
506526
507527 Returns
508528 -------
509529 type
510530 """
511- from pandas import Categorical
531+ from pandas import Categorical # noqa: F811
512532
513533 return Categorical
514534
@@ -672,9 +692,9 @@ class DatetimeTZDtype(PandasExtensionDtype):
672692 _match = re .compile (r"(datetime64|M8)\[(?P<unit>.+), (?P<tz>.+)\]" )
673693 _cache : Dict [str_type , PandasExtensionDtype ] = {}
674694
675- def __init__ (self , unit = "ns" , tz = None ):
695+ def __init__ (self , unit : Union [ str_type , "DatetimeTZDtype" ] = "ns" , tz = None ):
676696 if isinstance (unit , DatetimeTZDtype ):
677- unit , tz = unit .unit , unit .tz
697+ unit , tz = unit .unit , unit .tz # type: ignore
678698
679699 if unit != "ns" :
680700 if isinstance (unit , str ) and tz is None :
@@ -704,7 +724,7 @@ def __init__(self, unit="ns", tz=None):
704724 self ._tz = tz
705725
706726 @property
707- def unit (self ):
727+ def unit (self ) -> str_type :
708728 """
709729 The precision of the datetime data.
710730 """
@@ -718,20 +738,20 @@ def tz(self):
718738 return self ._tz
719739
720740 @classmethod
721- def construct_array_type (cls ):
741+ def construct_array_type (cls ) -> Type [ "DatetimeArray" ] :
722742 """
723743 Return the array type associated with this dtype.
724744
725745 Returns
726746 -------
727747 type
728748 """
729- from pandas .core .arrays import DatetimeArray
749+ from pandas .core .arrays import DatetimeArray # noqa: F811
730750
731751 return DatetimeArray
732752
733753 @classmethod
734- def construct_from_string (cls , string : str_type ):
754+ def construct_from_string (cls , string : str_type ) -> "DatetimeTZDtype" :
735755 """
736756 Construct a DatetimeTZDtype from a string.
737757
@@ -789,7 +809,7 @@ def __eq__(self, other: Any) -> bool:
789809 and str (self .tz ) == str (other .tz )
790810 )
791811
792- def __setstate__ (self , state ):
812+ def __setstate__ (self , state ) -> None :
793813 # for pickle compat. __get_state__ is defined in the
794814 # PandasExtensionDtype superclass and uses the public properties to
795815 # pickle -> need to set the settable private ones here (see GH26067)
@@ -884,7 +904,7 @@ def _parse_dtype_strict(cls, freq):
884904 raise ValueError ("could not construct PeriodDtype" )
885905
886906 @classmethod
887- def construct_from_string (cls , string ) :
907+ def construct_from_string (cls , string : str_type ) -> "PeriodDtype" :
888908 """
889909 Strict construction from a string, raise a TypeError if not
890910 possible
@@ -934,7 +954,7 @@ def __setstate__(self, state):
934954 self ._freq = state ["freq" ]
935955
936956 @classmethod
937- def is_dtype (cls , dtype ) -> bool :
957+ def is_dtype (cls , dtype : object ) -> bool :
938958 """
939959 Return a boolean if we if the passed type is an actual dtype that we
940960 can match (via string or type)
@@ -955,7 +975,7 @@ def is_dtype(cls, dtype) -> bool:
955975 return super ().is_dtype (dtype )
956976
957977 @classmethod
958- def construct_array_type (cls ):
978+ def construct_array_type (cls ) -> Type [ "PeriodArray" ] :
959979 """
960980 Return the array type associated with this dtype.
961981
@@ -967,9 +987,13 @@ def construct_array_type(cls):
967987
968988 return PeriodArray
969989
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
973997 from pandas .core .arrays import PeriodArray
974998 from pandas .core .arrays ._arrow_utils import pyarrow_array_to_numpy_and_mask
975999
@@ -1075,7 +1099,7 @@ def subtype(self):
10751099 return self ._subtype
10761100
10771101 @classmethod
1078- def construct_array_type (cls ):
1102+ def construct_array_type (cls ) -> Type [ "IntervalArray" ] :
10791103 """
10801104 Return the array type associated with this dtype.
10811105
@@ -1142,7 +1166,7 @@ def __setstate__(self, state):
11421166 self ._subtype = state ["subtype" ]
11431167
11441168 @classmethod
1145- def is_dtype (cls , dtype ) -> bool :
1169+ def is_dtype (cls , dtype : object ) -> bool :
11461170 """
11471171 Return a boolean if we if the passed type is an actual dtype that we
11481172 can match (via string or type)
@@ -1160,9 +1184,13 @@ def is_dtype(cls, dtype) -> bool:
11601184 return False
11611185 return super ().is_dtype (dtype )
11621186
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
11661194 from pandas .core .arrays import IntervalArray
11671195
11681196 if isinstance (array , pyarrow .Array ):
0 commit comments