284284T_co = TypeVar ("T_co" , covariant = True )
285285T_contra = TypeVar ("T_contra" , contravariant = True )
286286Ts = TypeVarTuple ("Ts" )
287- R = TypeVar ("R " , covariant = True )
287+ R_co = TypeVar ("R_co " , covariant = True )
288288F = TypeVar ("F" , bound = Callable [..., Any ])
289289P = ParamSpec ("P" )
290290K = TypeVar ("K" )
299299
300300# Undocumented on purpose for now, unclear that this is a great idea, given
301301# that typing.deprecated exists.
302- class MovedFunctionDeprecationWrapper (Generic [P , R ]):
303- f : Callable [P , R ]
302+ class MovedFunctionDeprecationWrapper (Generic [P , R_co ]):
303+ f : Callable [P , R_co ]
304304 deadline : int | str
305305
306306 def __init__ (self ,
307- f : Callable [P , R ],
307+ f : Callable [P , R_co ],
308308 deadline : int | str | None = None ) -> None :
309309 if deadline is None :
310310 deadline = "the future"
311311
312312 self .f = f
313313 self .deadline = deadline
314314
315- def __call__ (self , * args : P .args , ** kwargs : P .kwargs ) -> R :
315+ def __call__ (self , * args : P .args , ** kwargs : P .kwargs ) -> R_co :
316316 from warnings import warn
317317 warn (f"This function is deprecated and will go away in { self .deadline } . "
318318 f"Use { self .f .__module__ } .{ self .f .__name__ } instead." ,
@@ -587,21 +587,21 @@ def set(self, value: T) -> None:
587587 self .value = value
588588
589589
590- class FakeList (Generic [R ]):
591- def __init__ (self , f : Callable [[int ], R ], length : int ) -> None :
590+ class FakeList (Generic [R_co ]):
591+ def __init__ (self , f : Callable [[int ], R_co ], length : int ) -> None :
592592 self ._Length : int = length
593- self ._Function : Callable [[int ], R ] = f
593+ self ._Function : Callable [[int ], R_co ] = f
594594
595595 def __len__ (self ) -> int :
596596 return self ._Length
597597
598598 @overload
599- def __getitem__ (self , index : int ) -> R : ...
599+ def __getitem__ (self , index : int ) -> R_co : ...
600600
601601 @overload
602- def __getitem__ (self , index : slice ) -> Sequence [R ]: ...
602+ def __getitem__ (self , index : slice ) -> Sequence [R_co ]: ...
603603
604- def __getitem__ (self , index : object ) -> R | Sequence [R ]:
604+ def __getitem__ (self , index : object ) -> R_co | Sequence [R_co ]:
605605 if isinstance (index , int ):
606606 return self ._Function (index )
607607 elif isinstance (index , slice ):
@@ -613,17 +613,17 @@ def __getitem__(self, index: object) -> R | Sequence[R]:
613613
614614# {{{ dependent dictionary
615615
616- class DependentDictionary (Generic [T , R ]):
616+ class DependentDictionary (Generic [T , R_co ]):
617617 def __init__ (self ,
618- f : Callable [[dict [T , R ], T ], R ],
619- start : dict [T , R ] | None = None ) -> None :
618+ f : Callable [[dict [T , R_co ], T ], R_co ],
619+ start : dict [T , R_co ] | None = None ) -> None :
620620 if start is None :
621621 start = {}
622622
623- self ._Function : Callable [[dict [T , R ], T ], R ] = f
624- self ._Dictionary : dict [T , R ] = start .copy ()
623+ self ._Function : Callable [[dict [T , R_co ], T ], R_co ] = f
624+ self ._Dictionary : dict [T , R_co ] = start .copy ()
625625
626- def copy (self ) -> DependentDictionary [T , R ]:
626+ def copy (self ) -> DependentDictionary [T , R_co ]:
627627 return DependentDictionary (self ._Function , self ._Dictionary )
628628
629629 def __contains__ (self , key : T ) -> bool :
@@ -633,25 +633,25 @@ def __contains__(self, key: T) -> bool:
633633 except KeyError :
634634 return False
635635
636- def __getitem__ (self , key : T ) -> R :
636+ def __getitem__ (self , key : T ) -> R_co :
637637 try :
638638 return self ._Dictionary [key ]
639639 except KeyError :
640640 return self ._Function (self ._Dictionary , key )
641641
642- def __setitem__ (self , key : T , value : R ) -> None :
642+ def __setitem__ (self , key : T , value : R_co ) -> None :
643643 self ._Dictionary [key ] = value
644644
645645 def genuineKeys (self ): # noqa: N802
646646 return list (self ._Dictionary .keys ())
647647
648- def iteritems (self ) -> Iterable [tuple [T , R ]]:
648+ def iteritems (self ) -> Iterable [tuple [T , R_co ]]:
649649 return self ._Dictionary .items ()
650650
651651 def iterkeys (self ) -> Iterable [T ]:
652652 return self ._Dictionary .keys ()
653653
654- def itervalues (self ) -> Iterable [R ]:
654+ def itervalues (self ) -> Iterable [R_co ]:
655655 return self ._Dictionary .values ()
656656
657657# }}}
@@ -810,8 +810,8 @@ class _HasKwargs:
810810
811811
812812def memoize_on_first_arg (
813- function : Callable [Concatenate [T , P ], R ], * ,
814- cache_dict_name : str | None = None ) -> Callable [Concatenate [T , P ], R ]:
813+ function : Callable [Concatenate [T , P ], R_co ], * ,
814+ cache_dict_name : str | None = None ) -> Callable [Concatenate [T , P ], R_co ]:
815815 """Like :func:`memoize_method`, but for functions that take the object
816816 in which do memoization information is stored as first argument.
817817
@@ -823,7 +823,7 @@ def memoize_on_first_arg(
823823 f"_memoize_dic_{ function .__module__ } { function .__name__ } "
824824 )
825825
826- def wrapper (obj : T , * args : P .args , ** kwargs : P .kwargs ) -> R :
826+ def wrapper (obj : T , * args : P .args , ** kwargs : P .kwargs ) -> R_co :
827827 key = (_HasKwargs , frozenset (kwargs .items ()), * args ) if kwargs else args
828828
829829 assert cache_dict_name is not None
@@ -856,8 +856,8 @@ def clear_cache(obj):
856856
857857
858858def memoize_method (
859- method : Callable [Concatenate [T , P ], R ]
860- ) -> Callable [Concatenate [T , P ], R ]:
859+ method : Callable [Concatenate [T , P ], R_co ]
860+ ) -> Callable [Concatenate [T , P ], R_co ]:
861861 """Supports cache deletion via ``method_name.clear_cache(self)``.
862862
863863 .. versionchanged:: 2021.2
@@ -870,7 +870,7 @@ def memoize_method(
870870 cache_dict_name = intern (f"_memoize_dic_{ method .__name__ } " ))
871871
872872
873- class keyed_memoize_on_first_arg (Generic [T , P , R ]): # noqa: N801
873+ class keyed_memoize_on_first_arg (Generic [T , P , R_co ]): # noqa: N801
874874 """Like :func:`memoize_method`, but for functions that take the object
875875 in which memoization information is stored as first argument.
876876
@@ -891,19 +891,19 @@ def __init__(self,
891891 self .cache_dict_name = cache_dict_name
892892
893893 def _default_cache_dict_name (self ,
894- function : Callable [Concatenate [T , P ], R ]) -> str :
894+ function : Callable [Concatenate [T , P ], R_co ]) -> str :
895895 return intern (f"_memoize_dic_{ function .__module__ } { function .__name__ } " )
896896
897897 def __call__ (
898- self , function : Callable [Concatenate [T , P ], R ]
899- ) -> Callable [Concatenate [T , P ], R ]:
898+ self , function : Callable [Concatenate [T , P ], R_co ]
899+ ) -> Callable [Concatenate [T , P ], R_co ]:
900900 cache_dict_name = self .cache_dict_name
901901 key = self .key
902902
903903 if cache_dict_name is None :
904904 cache_dict_name = self ._default_cache_dict_name (function )
905905
906- def wrapper (obj : T , * args : P .args , ** kwargs : P .kwargs ) -> R :
906+ def wrapper (obj : T , * args : P .args , ** kwargs : P .kwargs ) -> R_co :
907907 cache_key = key (* args , ** kwargs )
908908
909909 assert cache_dict_name is not None
@@ -983,9 +983,9 @@ def __init__(self, container: Any, identifier: Hashable) -> None:
983983
984984 self .cache_dict = memoize_in_dict .setdefault (identifier , {})
985985
986- def __call__ (self , inner : Callable [P , R ]) -> Callable [P , R ]:
986+ def __call__ (self , inner : Callable [P , R_co ]) -> Callable [P , R_co ]:
987987 @wraps (inner )
988- def new_inner (* args : P .args , ** kwargs : P .kwargs ) -> R :
988+ def new_inner (* args : P .args , ** kwargs : P .kwargs ) -> R_co :
989989 assert not kwargs
990990
991991 try :
@@ -1021,9 +1021,9 @@ def __init__(self,
10211021 self .cache_dict = memoize_in_dict .setdefault (identifier , {})
10221022 self .key = key
10231023
1024- def __call__ (self , inner : Callable [P , R ]) -> Callable [P , R ]:
1024+ def __call__ (self , inner : Callable [P , R_co ]) -> Callable [P , R_co ]:
10251025 @wraps (inner )
1026- def new_inner (* args : P .args , ** kwargs : P .kwargs ) -> R :
1026+ def new_inner (* args : P .args , ** kwargs : P .kwargs ) -> R_co :
10271027 assert not kwargs
10281028 key = self .key (* args , ** kwargs )
10291029
@@ -1609,7 +1609,7 @@ def generate_all_integer_tuples_below(
16091609 n , length , least_abs ))
16101610
16111611
1612- class _ConcatenableSequence (Generic [T_co ], Protocol ):
1612+ class _ConcatenableSequence (Protocol , Generic [T_co ]):
16131613 """
16141614 A protocol that supports the following:
16151615
@@ -2185,9 +2185,8 @@ def invoke_editor(s: str, filename: str = "edit.txt", descr: str = "the file"):
21852185 "dropped directly into an editor next time.)" )
21862186 input (f"Edit { descr } at { full_path } now, then hit [Enter]:" )
21872187
2188- result = full_path .read_text ()
2188+ return full_path .read_text ()
21892189
2190- return result
21912190
21922191# }}}
21932192
@@ -2285,7 +2284,7 @@ def __enter__(self) -> None:
22852284 self .draw ()
22862285
22872286 def __exit__ (self ,
2288- exc_type : type [BaseException ],
2287+ exc_type : type [BaseException ] | None ,
22892288 exc_val : BaseException | None ,
22902289 exc_tb : types .TracebackType | None ) -> None :
22912290 self .finished ()
@@ -2515,7 +2514,7 @@ def __enter__(self) -> None:
25152514 sys .setrecursionlimit (new_limit )
25162515
25172516 def __exit__ (self ,
2518- exc_type : type [BaseException ],
2517+ exc_type : type [BaseException ] | None ,
25192518 exc_val : BaseException | None ,
25202519 exc_tb : types .TracebackType | None ) -> None :
25212520 # Deep recursion can produce deeply nested data structures
@@ -2678,7 +2677,7 @@ def __enter__(self) -> Self:
26782677 return self
26792678
26802679 def __exit__ (self ,
2681- exc_type : type [BaseException ],
2680+ exc_type : type [BaseException ] | None ,
26822681 exc_val : BaseException | None ,
26832682 exc_tb : types .TracebackType | None ) -> None :
26842683 self .done ()
@@ -2841,7 +2840,7 @@ def __enter__(self):
28412840 pass
28422841
28432842 def __exit__ (self ,
2844- exc_type : type [BaseException ],
2843+ exc_type : type [BaseException ] | None ,
28452844 exc_val : BaseException | None ,
28462845 exc_tb : types .TracebackType | None ) -> None :
28472846 self .done ()
@@ -2871,23 +2870,22 @@ def __init__(self,
28712870 self .description = description
28722871 self .long_threshold_seconds = long_threshold_seconds
28732872
2874- def __call__ (self , wrapped : Callable [P , R ]) -> Callable [P , R ]:
2873+ def __call__ (self , wrapped : Callable [P , R_co ]) -> Callable [P , R_co ]:
28752874 if self .description :
28762875 description = f"{ wrapped .__qualname__ } ({ self .description } )"
28772876 else :
28782877 description = wrapped .__qualname__
28792878
2880- def wrapper (* args : P .args , ** kwargs : P .kwargs ) -> R :
2879+ def wrapper (* args : P .args , ** kwargs : P .kwargs ) -> R_co :
28812880 with ProcessLogger (
28822881 self .logger ,
28832882 description ,
28842883 long_threshold_seconds = self .long_threshold_seconds ):
28852884 return wrapped (* args , ** kwargs )
28862885
28872886 from functools import update_wrapper
2888- new_wrapper = update_wrapper (wrapper , wrapped )
2887+ return update_wrapper (wrapper , wrapped )
28892888
2890- return new_wrapper
28912889
28922890# }}}
28932891
0 commit comments