51
51
from collections import namedtuple
52
52
from contextlib import contextmanager
53
53
import re
54
- from typing import Any , Dict , Iterable , List
54
+ from typing import (
55
+ Any ,
56
+ Callable ,
57
+ Dict ,
58
+ Iterable ,
59
+ List ,
60
+ Optional ,
61
+ Tuple ,
62
+ Type ,
63
+ TypeVar ,
64
+ cast ,
65
+ )
55
66
import warnings
56
67
57
68
DeprecatedOption = namedtuple ("DeprecatedOption" , "key msg rkey removal_ver" )
@@ -80,7 +91,7 @@ class OptionError(AttributeError, KeyError):
80
91
# User API
81
92
82
93
83
- def _get_single_key (pat , silent ) :
94
+ def _get_single_key (pat : str , silent : bool ) -> str :
84
95
keys = _select_options (pat )
85
96
if len (keys ) == 0 :
86
97
if not silent :
@@ -98,15 +109,15 @@ def _get_single_key(pat, silent):
98
109
return key
99
110
100
111
101
- def _get_option (pat , silent = False ):
112
+ def _get_option (pat : str , silent : bool = False ):
102
113
key = _get_single_key (pat , silent )
103
114
104
115
# walk the nested dict
105
116
root , k = _get_root (key )
106
117
return root [k ]
107
118
108
119
109
- def _set_option (* args , ** kwargs ):
120
+ def _set_option (* args , ** kwargs ) -> None :
110
121
# must at least 1 arg deal with constraints later
111
122
nargs = len (args )
112
123
if not nargs or nargs % 2 != 0 :
@@ -138,7 +149,7 @@ def _set_option(*args, **kwargs):
138
149
o .cb (key )
139
150
140
151
141
- def _describe_option (pat = "" , _print_desc = True ):
152
+ def _describe_option (pat : str = "" , _print_desc : bool = True ):
142
153
143
154
keys = _select_options (pat )
144
155
if len (keys ) == 0 :
@@ -154,7 +165,7 @@ def _describe_option(pat="", _print_desc=True):
154
165
return s
155
166
156
167
157
- def _reset_option (pat , silent = False ):
168
+ def _reset_option (pat : str , silent : bool = False ) -> None :
158
169
159
170
keys = _select_options (pat )
160
171
@@ -172,19 +183,19 @@ def _reset_option(pat, silent=False):
172
183
_set_option (k , _registered_options [k ].defval , silent = silent )
173
184
174
185
175
- def get_default_val (pat ):
186
+ def get_default_val (pat : str ):
176
187
key = _get_single_key (pat , silent = True )
177
188
return _get_registered_option (key ).defval
178
189
179
190
180
191
class DictWrapper :
181
192
""" provide attribute-style access to a nested dict"""
182
193
183
- def __init__ (self , d , prefix = "" ):
194
+ def __init__ (self , d : Dict [ str , Any ], prefix : str = "" ):
184
195
object .__setattr__ (self , "d" , d )
185
196
object .__setattr__ (self , "prefix" , prefix )
186
197
187
- def __setattr__ (self , key , val ) :
198
+ def __setattr__ (self , key : str , val : Any ) -> None :
188
199
prefix = object .__getattribute__ (self , "prefix" )
189
200
if prefix :
190
201
prefix += "."
@@ -210,7 +221,7 @@ def __getattr__(self, key: str):
210
221
else :
211
222
return _get_option (prefix )
212
223
213
- def __dir__ (self ):
224
+ def __dir__ (self ) -> Iterable [ str ] :
214
225
return list (self .d .keys ())
215
226
216
227
@@ -411,23 +422,31 @@ def __exit__(self, *args):
411
422
_set_option (pat , val , silent = True )
412
423
413
424
414
- def register_option (key : str , defval : object , doc = "" , validator = None , cb = None ):
415
- """Register an option in the package-wide pandas config object
425
+ def register_option (
426
+ key : str ,
427
+ defval : object ,
428
+ doc : str = "" ,
429
+ validator : Optional [Callable [[Any ], Any ]] = None ,
430
+ cb : Optional [Callable [[str ], Any ]] = None ,
431
+ ) -> None :
432
+ """
433
+ Register an option in the package-wide pandas config object
416
434
417
435
Parameters
418
436
----------
419
- key - a fully-qualified key, e.g. "x.y.option - z".
420
- defval - the default value of the option
421
- doc - a string description of the option
422
- validator - a function of a single argument, should raise `ValueError` if
423
- called with a value which is not a legal value for the option.
424
- cb - a function of a single argument "key", which is called
425
- immediately after an option value is set/reset. key is
426
- the full name of the option.
427
-
428
- Returns
429
- -------
430
- Nothing.
437
+ key : str
438
+ Fully-qualified key, e.g. "x.y.option - z".
439
+ defval : object
440
+ Default value of the option.
441
+ doc : str
442
+ Description of the option.
443
+ validator : Callable, optional
444
+ Function of a single argument, should raise `ValueError` if
445
+ called with a value which is not a legal value for the option.
446
+ cb
447
+ a function of a single argument "key", which is called
448
+ immediately after an option value is set/reset. key is
449
+ the full name of the option.
431
450
432
451
Raises
433
452
------
@@ -480,7 +499,9 @@ def register_option(key: str, defval: object, doc="", validator=None, cb=None):
480
499
)
481
500
482
501
483
- def deprecate_option (key , msg = None , rkey = None , removal_ver = None ):
502
+ def deprecate_option (
503
+ key : str , msg : Optional [str ] = None , rkey : Optional [str ] = None , removal_ver = None
504
+ ) -> None :
484
505
"""
485
506
Mark option `key` as deprecated, if code attempts to access this option,
486
507
a warning will be produced, using `msg` if given, or a default message
@@ -493,32 +514,27 @@ def deprecate_option(key, msg=None, rkey=None, removal_ver=None):
493
514
494
515
Parameters
495
516
----------
496
- key - the name of the option to be deprecated. must be a fully-qualified
497
- option name (e.g "x.y.z.rkey").
498
-
499
- msg - (Optional) a warning message to output when the key is referenced.
500
- if no message is given a default message will be emitted.
501
-
502
- rkey - (Optional) the name of an option to reroute access to.
503
- If specified, any referenced `key` will be re-routed to `rkey`
504
- including set/get/reset.
505
- rkey must be a fully-qualified option name (e.g "x.y.z.rkey").
506
- used by the default message if no `msg` is specified.
507
-
508
- removal_ver - (Optional) specifies the version in which this option will
509
- be removed. used by the default message if no `msg`
510
- is specified.
511
-
512
- Returns
513
- -------
514
- Nothing
517
+ key : str
518
+ Name of the option to be deprecated.
519
+ must be a fully-qualified option name (e.g "x.y.z.rkey").
520
+ msg : str, optional
521
+ Warning message to output when the key is referenced.
522
+ if no message is given a default message will be emitted.
523
+ rkey : str, optional
524
+ Name of an option to reroute access to.
525
+ If specified, any referenced `key` will be
526
+ re-routed to `rkey` including set/get/reset.
527
+ rkey must be a fully-qualified option name (e.g "x.y.z.rkey").
528
+ used by the default message if no `msg` is specified.
529
+ removal_ver : optional
530
+ Specifies the version in which this option will
531
+ be removed. used by the default message if no `msg` is specified.
515
532
516
533
Raises
517
534
------
518
- OptionError - if key has already been deprecated.
519
-
535
+ OptionError
536
+ If the specified key has already been deprecated.
520
537
"""
521
-
522
538
key = key .lower ()
523
539
524
540
if key in _deprecated_options :
@@ -531,7 +547,7 @@ def deprecate_option(key, msg=None, rkey=None, removal_ver=None):
531
547
# functions internal to the module
532
548
533
549
534
- def _select_options (pat ) :
550
+ def _select_options (pat : str ) -> List [ str ] :
535
551
"""returns a list of keys matching `pat`
536
552
537
553
if pat=="all", returns all registered options
@@ -549,22 +565,22 @@ def _select_options(pat):
549
565
return [k for k in keys if re .search (pat , k , re .I )]
550
566
551
567
552
- def _get_root (key ) :
568
+ def _get_root (key : str ) -> Tuple [ Dict [ str , Any ], str ] :
553
569
path = key .split ("." )
554
570
cursor = _global_config
555
571
for p in path [:- 1 ]:
556
572
cursor = cursor [p ]
557
573
return cursor , path [- 1 ]
558
574
559
575
560
- def _is_deprecated (key ) :
576
+ def _is_deprecated (key : str ) -> bool :
561
577
""" Returns True if the given option has been deprecated """
562
578
563
579
key = key .lower ()
564
580
return key in _deprecated_options
565
581
566
582
567
- def _get_deprecated_option (key ):
583
+ def _get_deprecated_option (key : str ):
568
584
"""
569
585
Retrieves the metadata for a deprecated option, if `key` is deprecated.
570
586
@@ -581,7 +597,7 @@ def _get_deprecated_option(key):
581
597
return d
582
598
583
599
584
- def _get_registered_option (key ):
600
+ def _get_registered_option (key : str ):
585
601
"""
586
602
Retrieves the option metadata if `key` is a registered option.
587
603
@@ -592,7 +608,7 @@ def _get_registered_option(key):
592
608
return _registered_options .get (key )
593
609
594
610
595
- def _translate_key (key ) :
611
+ def _translate_key (key : str ) -> str :
596
612
"""
597
613
if key id deprecated and a replacement key defined, will return the
598
614
replacement key, otherwise returns `key` as - is
@@ -605,7 +621,7 @@ def _translate_key(key):
605
621
return key
606
622
607
623
608
- def _warn_if_deprecated (key ) :
624
+ def _warn_if_deprecated (key : str ) -> bool :
609
625
"""
610
626
Checks if `key` is a deprecated option and if so, prints a warning.
611
627
@@ -633,7 +649,7 @@ def _warn_if_deprecated(key):
633
649
return False
634
650
635
651
636
- def _build_option_description (k ) :
652
+ def _build_option_description (k : str ) -> str :
637
653
""" Builds a formatted description of a registered option and prints it """
638
654
639
655
o = _get_registered_option (k )
@@ -658,7 +674,7 @@ def _build_option_description(k):
658
674
return s
659
675
660
676
661
- def pp_options_list (keys , width = 80 , _print = False ):
677
+ def pp_options_list (keys : Iterable [ str ] , width = 80 , _print : bool = False ):
662
678
""" Builds a concise listing of available options, grouped by prefix """
663
679
664
680
from textwrap import wrap
@@ -696,6 +712,9 @@ def pp(name: str, ks: Iterable[str]) -> List[str]:
696
712
#
697
713
# helpers
698
714
715
+ FuncType = Callable [..., Any ]
716
+ F = TypeVar ("F" , bound = FuncType )
717
+
699
718
700
719
@contextmanager
701
720
def config_prefix (prefix ):
@@ -727,12 +746,12 @@ def config_prefix(prefix):
727
746
728
747
global register_option , get_option , set_option , reset_option
729
748
730
- def wrap (func ) :
731
- def inner (key , * args , ** kwds ):
749
+ def wrap (func : F ) -> F :
750
+ def inner (key : str , * args , ** kwds ):
732
751
pkey = f"{ prefix } .{ key } "
733
752
return func (pkey , * args , ** kwds )
734
753
735
- return inner
754
+ return cast ( F , inner )
736
755
737
756
_register_option = register_option
738
757
_get_option = get_option
@@ -750,7 +769,7 @@ def inner(key, *args, **kwds):
750
769
# arg in register_option
751
770
752
771
753
- def is_type_factory (_type ) :
772
+ def is_type_factory (_type : Type [ Any ]) -> Callable [[ Any ], None ] :
754
773
"""
755
774
756
775
Parameters
@@ -764,14 +783,14 @@ def is_type_factory(_type):
764
783
765
784
"""
766
785
767
- def inner (x ):
786
+ def inner (x ) -> None :
768
787
if type (x ) != _type :
769
788
raise ValueError (f"Value must have type '{ _type } '" )
770
789
771
790
return inner
772
791
773
792
774
- def is_instance_factory (_type ):
793
+ def is_instance_factory (_type ) -> Callable [[ Any ], None ] :
775
794
"""
776
795
777
796
Parameters
@@ -791,19 +810,19 @@ def is_instance_factory(_type):
791
810
else :
792
811
type_repr = f"'{ _type } '"
793
812
794
- def inner (x ):
813
+ def inner (x ) -> None :
795
814
if not isinstance (x , _type ):
796
815
raise ValueError (f"Value must be an instance of { type_repr } " )
797
816
798
817
return inner
799
818
800
819
801
- def is_one_of_factory (legal_values ):
820
+ def is_one_of_factory (legal_values ) -> Callable [[ Any ], None ] :
802
821
803
822
callables = [c for c in legal_values if callable (c )]
804
823
legal_values = [c for c in legal_values if not callable (c )]
805
824
806
- def inner (x ):
825
+ def inner (x ) -> None :
807
826
if x not in legal_values :
808
827
809
828
if not any (c (x ) for c in callables ):
@@ -817,7 +836,7 @@ def inner(x):
817
836
return inner
818
837
819
838
820
- def is_nonnegative_int (value ) :
839
+ def is_nonnegative_int (value : Optional [ int ]) -> None :
821
840
"""
822
841
Verify that value is None or a positive int.
823
842
@@ -852,7 +871,7 @@ def is_nonnegative_int(value):
852
871
is_text = is_instance_factory ((str , bytes ))
853
872
854
873
855
- def is_callable (obj ):
874
+ def is_callable (obj ) -> bool :
856
875
"""
857
876
858
877
Parameters
0 commit comments