11""" define extension dtypes """
22import re
3- from typing import Any , Dict , Optional , Tuple , Type
3+ from typing import Any , Dict , List , Optional , Tuple , Type , Union
44import warnings
55
66import numpy as np
1818str_type = str
1919
2020
21- def register_extension_dtype (cls ):
21+ def register_extension_dtype (cls : Type [ExtensionDtype ],
22+ ) -> Type [ExtensionDtype ]:
2223 """
2324 Register an ExtensionType with pandas as class decorator.
2425
@@ -60,9 +61,9 @@ class Registry:
6061 These are tried in order.
6162 """
6263 def __init__ (self ):
63- self .dtypes = []
64+ self .dtypes = [] # type: List[Type[ExtensionDtype]]
6465
65- def register (self , dtype ) :
66+ def register (self , dtype : Type [ ExtensionDtype ]) -> None :
6667 """
6768 Parameters
6869 ----------
@@ -73,11 +74,13 @@ def register(self, dtype):
7374
7475 self .dtypes .append (dtype )
7576
76- def find (self , dtype ):
77+ def find (self ,
78+ dtype : Union [Type [ExtensionDtype ], str ],
79+ ) -> Optional [Type [ExtensionDtype ]]:
7780 """
7881 Parameters
7982 ----------
80- dtype : PandasExtensionDtype or string
83+ dtype : Type[ExtensionDtype] or string
8184
8285 Returns
8386 -------
@@ -126,28 +129,28 @@ class PandasExtensionDtype(ExtensionDtype):
126129 isnative = 0
127130 _cache = {} # type: Dict[str_type, 'PandasExtensionDtype']
128131
129- def __str__ (self ):
132+ def __str__ (self ) -> str_type :
130133 """
131134 Return a string representation for a particular Object
132135 """
133136 return self .name
134137
135- def __repr__ (self ):
138+ def __repr__ (self ) -> str_type :
136139 """
137140 Return a string representation for a particular object.
138141 """
139142 return str (self )
140143
141- def __hash__ (self ):
144+ def __hash__ (self ) -> int :
142145 raise NotImplementedError ("sub-classes should implement an __hash__ "
143146 "method" )
144147
145- def __getstate__ (self ):
148+ def __getstate__ (self ) -> Dict [ str_type , Any ] :
146149 # pickle support; we don't want to pickle the cache
147150 return {k : getattr (self , k , None ) for k in self ._metadata }
148151
149152 @classmethod
150- def reset_cache (cls ):
153+ def reset_cache (cls ) -> None :
151154 """ clear the cache """
152155 cls ._cache = {}
153156
@@ -211,17 +214,24 @@ class CategoricalDtype(PandasExtensionDtype, ExtensionDtype):
211214 _metadata = ('categories' , 'ordered' )
212215 _cache = {} # type: Dict[str_type, PandasExtensionDtype]
213216
214- def __init__ (self , categories = None , ordered = None ):
217+ def __init__ (self , categories = None , ordered : bool = None ):
215218 self ._finalize (categories , ordered , fastpath = False )
216219
217220 @classmethod
218- def _from_fastpath (cls , categories = None , ordered = None ):
221+ def _from_fastpath (cls ,
222+ categories = None ,
223+ ordered : bool = None
224+ ) -> 'CategoricalDtype' :
219225 self = cls .__new__ (cls )
220226 self ._finalize (categories , ordered , fastpath = True )
221227 return self
222228
223229 @classmethod
224- def _from_categorical_dtype (cls , dtype , categories = None , ordered = None ):
230+ def _from_categorical_dtype (cls ,
231+ dtype : 'CategoricalDtype' ,
232+ categories = None ,
233+ ordered : bool = None ,
234+ ) -> 'CategoricalDtype' :
225235 if categories is ordered is None :
226236 return dtype
227237 if categories is None :
@@ -231,8 +241,12 @@ def _from_categorical_dtype(cls, dtype, categories=None, ordered=None):
231241 return cls (categories , ordered )
232242
233243 @classmethod
234- def _from_values_or_dtype (cls , values = None , categories = None , ordered = None ,
235- dtype = None ):
244+ def _from_values_or_dtype (cls ,
245+ values = None ,
246+ categories = None ,
247+ ordered : bool = None ,
248+ dtype : 'CategoricalDtype' = None ,
249+ ) -> 'CategoricalDtype' :
236250 """
237251 Construct dtype from the input parameters used in :class:`Categorical`.
238252
@@ -314,7 +328,11 @@ def _from_values_or_dtype(cls, values=None, categories=None, ordered=None,
314328
315329 return dtype
316330
317- def _finalize (self , categories , ordered , fastpath = False ):
331+ def _finalize (self ,
332+ categories ,
333+ ordered : Optional [bool ],
334+ fastpath : bool = False ,
335+ ) -> None :
318336
319337 if ordered is not None :
320338 self .validate_ordered (ordered )
@@ -326,14 +344,14 @@ def _finalize(self, categories, ordered, fastpath=False):
326344 self ._categories = categories
327345 self ._ordered = ordered
328346
329- def __setstate__ (self , state ) :
347+ def __setstate__ (self , state : Dict [ str_type , Any ]) -> None :
330348 # for pickle compat. __get_state__ is defined in the
331349 # PandasExtensionDtype superclass and uses the public properties to
332350 # pickle -> need to set the settable private ones here (see GH26067)
333351 self ._categories = state .pop ('categories' , None )
334352 self ._ordered = state .pop ('ordered' , False )
335353
336- def __hash__ (self ):
354+ def __hash__ (self ) -> int :
337355 # _hash_categories returns a uint64, so use the negative
338356 # space for when we have unknown categories to avoid a conflict
339357 if self .categories is None :
@@ -344,7 +362,7 @@ def __hash__(self):
344362 # We *do* want to include the real self.ordered here
345363 return int (self ._hash_categories (self .categories , self .ordered ))
346364
347- def __eq__ (self , other ) :
365+ def __eq__ (self , other : Any ) -> bool :
348366 """
349367 Rules for CDT equality:
350368 1) Any CDT is equal to the string 'category'
@@ -391,7 +409,7 @@ def __repr__(self):
391409 return tpl .format (data , self .ordered )
392410
393411 @staticmethod
394- def _hash_categories (categories , ordered = True ):
412+ def _hash_categories (categories , ordered : Optional [ bool ] = True ) -> int :
395413 from pandas .core .util .hashing import (
396414 hash_array , _combine_hash_arrays , hash_tuples
397415 )
@@ -441,7 +459,7 @@ def construct_array_type(cls):
441459 return Categorical
442460
443461 @staticmethod
444- def validate_ordered (ordered ) :
462+ def validate_ordered (ordered : bool ) -> None :
445463 """
446464 Validates that we have a valid ordered parameter. If
447465 it is not a boolean, a TypeError will be raised.
@@ -461,7 +479,7 @@ def validate_ordered(ordered):
461479 raise TypeError ("'ordered' must either be 'True' or 'False'" )
462480
463481 @staticmethod
464- def validate_categories (categories , fastpath = False ):
482+ def validate_categories (categories , fastpath : bool = False ):
465483 """
466484 Validates that we have good categories
467485
@@ -475,7 +493,7 @@ def validate_categories(categories, fastpath=False):
475493 -------
476494 categories : Index
477495 """
478- from pandas import Index
496+ from pandas . core . indexes . base import Index
479497
480498 if not fastpath and not is_list_like (categories ):
481499 msg = "Parameter 'categories' must be list-like, was {!r}"
@@ -496,7 +514,7 @@ def validate_categories(categories, fastpath=False):
496514
497515 return categories
498516
499- def update_dtype (self , dtype ) :
517+ def update_dtype (self , dtype : 'CategoricalDtype' ) -> 'CategoricalDtype' :
500518 """
501519 Returns a CategoricalDtype with categories and ordered taken from dtype
502520 if specified, otherwise falling back to self if unspecified
@@ -538,14 +556,14 @@ def categories(self):
538556 return self ._categories
539557
540558 @property
541- def ordered (self ):
559+ def ordered (self ) -> Optional [ bool ] :
542560 """
543561 Whether the categories have an ordered relationship.
544562 """
545563 return self ._ordered
546564
547565 @property
548- def _is_boolean (self ):
566+ def _is_boolean (self ) -> bool :
549567 from pandas .core .dtypes .common import is_bool_dtype
550568
551569 return is_bool_dtype (self .categories )
0 commit comments