1818 Callable , Hashable , Iterable , Mapping , MutableMapping , Sequence
1919)
2020from types import TracebackType
21- from typing import Any , Literal , TypeVar , cast
21+ from typing import Any , Literal , cast
2222
2323import cftime
2424import netCDF4
3535DEFAULT_CALENDAR = 'proleptic_gregorian'
3636
3737
38- _T = TypeVar ("_T" )
39- _Exception = TypeVar ("_Exception" , bound = BaseException )
40-
41-
4238class PerfTimer :
4339 __slots__ = ('_start' , '_stop' , 'running' )
4440
@@ -56,10 +52,10 @@ def __enter__(self) -> 'PerfTimer':
5652 self ._start = time .perf_counter ()
5753 return self
5854
59- def __exit__ (
55+ def __exit__ [ E : BaseException ] (
6056 self ,
61- exc_type : type [_Exception ] | None ,
62- exc_value : _Exception | None ,
57+ exc_type : type [E ] | None ,
58+ exc_value : E | None ,
6359 traceback : TracebackType
6460 ) -> bool | None :
6561 self ._stop = time .perf_counter ()
@@ -75,7 +71,7 @@ def elapsed(self) -> float:
7571 return self ._stop - self ._start
7672
7773
78- def timed_func ( fn : Callable [..., _T ] ) -> Callable [..., _T ] :
74+ def timed_func [ F : Callable ]( fn : F ) -> F :
7975 """
8076 Log the execution time of the decorated function.
8177 Logs "Calling ``<func.__qualname__>``" before the wrapped function is called,
@@ -101,13 +97,13 @@ def polygons(self):
10197 fn_logger = logging .getLogger (fn .__module__ )
10298
10399 @functools .wraps (fn )
104- def wrapper (* args : Any , ** kwargs : Any ) -> _T :
100+ def wrapper (* args , ** kwargs ): # type: ignore
105101 fn_logger .debug ("Calling %s" , fn .__qualname__ )
106102 with PerfTimer () as timer :
107103 value = fn (* args , ** kwargs )
108104 fn_logger .debug ("Completed %s in %fs" , fn .__qualname__ , timer .elapsed )
109105 return value
110- return wrapper
106+ return cast ( F , wrapper )
111107
112108
113109def to_netcdf_with_fixes (
@@ -376,7 +372,7 @@ def extract_vars(
376372 return dataset .drop_vars (drop_vars )
377373
378374
379- def pairwise (iterable : Iterable [_T ]) -> Iterable [tuple [_T , _T ]]:
375+ def pairwise [ T ] (iterable : Iterable [T ]) -> Iterable [tuple [T , T ]]:
380376 """
381377 Iterate over values in an iterator in pairs.
382378
@@ -734,15 +730,15 @@ def __init__(self, extra: str) -> None:
734730 self .extra = extra
735731
736732
737- def requires_extra (
733+ def requires_extra [ T ] (
738734 extra : str ,
739735 import_error : ImportError | None ,
740736 exception_class : type [RequiresExtraException ] = RequiresExtraException ,
741- ) -> Callable [[_T ], _T ]:
737+ ) -> Callable [[T ], T ]:
742738 if import_error is None :
743739 return lambda fn : fn
744740
745- def error_decorator (fn : _T ) -> _T :
741+ def error_decorator (fn : T ) -> T :
746742 @functools .wraps (fn ) # type: ignore
747743 def error (* args : Any , ** kwargs : Any ) -> Any :
748744 raise exception_class (extra ) from import_error
0 commit comments