109109)
110110
111111
112- T = TypeVar ("T " )
112+ _T = TypeVar ("_T " )
113113# Type aliases
114114_NestedStr = Union [str , Iterable [Union [str , Iterable ["_NestedStr" ]]]]
115115_InstallerType = Callable [["Requirement" ], Optional ["Distribution" ]]
118118_MetadataType = Optional ["IResourceProvider" ]
119119# Any object works, but let's indicate we expect something like a module (optionally has __loader__ or __file__)
120120_ModuleLike = Union [object , types .ModuleType ]
121- _AdapterType = Callable [..., Any ] # Incomplete
121+ _ProviderFactoryType = Callable [[_ModuleLike ], "IResourceProvider" ]
122+ _DistFinderType = Callable [[_T , str , bool ], Iterable ["Distribution" ]]
123+ _NSHandlerType = Callable [[_T , str , str , types .ModuleType ], Optional [str ]]
124+ _AdapterT = TypeVar (
125+ "_AdapterT" , _DistFinderType [Any ], _ProviderFactoryType , _NSHandlerType [Any ]
126+ )
122127
123128
124129# Use _typeshed.importlib.LoaderProtocol once available https://github.com/python/typeshed/pull/11890
@@ -142,7 +147,7 @@ class PEP440Warning(RuntimeWarning):
142147_state_vars : Dict [str , str ] = {}
143148
144149
145- def _declare_state (vartype : str , varname : str , initial_value : T ) -> T :
150+ def _declare_state (vartype : str , varname : str , initial_value : _T ) -> _T :
146151 _state_vars [varname ] = vartype
147152 return initial_value
148153
@@ -377,7 +382,7 @@ class UnknownExtra(ResolutionError):
377382 """Distribution doesn't have an "extra feature" of the given name"""
378383
379384
380- _provider_factories : Dict [Type [_ModuleLike ], _AdapterType ] = {}
385+ _provider_factories : Dict [Type [_ModuleLike ], _ProviderFactoryType ] = {}
381386
382387PY_MAJOR = '{}.{}' .format (* sys .version_info )
383388EGG_DIST = 3
@@ -388,7 +393,7 @@ class UnknownExtra(ResolutionError):
388393
389394
390395def register_loader_type (
391- loader_type : Type [_ModuleLike ], provider_factory : _AdapterType
396+ loader_type : Type [_ModuleLike ], provider_factory : _ProviderFactoryType
392397):
393398 """Register `provider_factory` to make providers for `loader_type`
394399
@@ -2097,12 +2102,12 @@ def __init__(self, importer: zipimport.zipimporter):
20972102 self ._setup_prefix ()
20982103
20992104
2100- _distribution_finders : Dict [
2101- type , Callable [[ object , str , bool ], Iterable [ "Distribution" ]]
2102- ] = _declare_state ( 'dict' , '_distribution_finders' , {} )
2105+ _distribution_finders : Dict [type , _DistFinderType [ Any ]] = _declare_state (
2106+ 'dict' , '_distribution_finders' , {}
2107+ )
21032108
21042109
2105- def register_finder (importer_type : type , distribution_finder : _AdapterType ):
2110+ def register_finder (importer_type : Type [ _T ] , distribution_finder : _DistFinderType [ _T ] ):
21062111 """Register `distribution_finder` to find distributions in sys.path items
21072112
21082113 `importer_type` is the type or class of a PEP 302 "Importer" (sys.path item
@@ -2276,15 +2281,17 @@ def resolve_egg_link(path):
22762281
22772282register_finder (importlib .machinery .FileFinder , find_on_path )
22782283
2279- _namespace_handlers : Dict [
2280- type , Callable [[ object , str , str , types . ModuleType ], Optional [ str ]]
2281- ] = _declare_state ( 'dict' , '_namespace_handlers' , {} )
2284+ _namespace_handlers : Dict [type , _NSHandlerType [ Any ]] = _declare_state (
2285+ 'dict' , '_namespace_handlers' , {}
2286+ )
22822287_namespace_packages : Dict [Optional [str ], List [str ]] = _declare_state (
22832288 'dict' , '_namespace_packages' , {}
22842289)
22852290
22862291
2287- def register_namespace_handler (importer_type : type , namespace_handler : _AdapterType ):
2292+ def register_namespace_handler (
2293+ importer_type : Type [_T ], namespace_handler : _NSHandlerType [_T ]
2294+ ):
22882295 """Register `namespace_handler` to declare namespace packages
22892296
22902297 `importer_type` is the type or class of a PEP 302 "Importer" (sys.path item
@@ -2429,9 +2436,9 @@ def fixup_namespace_packages(path_item: str, parent: Optional[str] = None):
24292436
24302437
24312438def file_ns_handler (
2432- importer : Optional [ importlib . abc . PathEntryFinder ] ,
2433- path_item ,
2434- packageName ,
2439+ importer : object ,
2440+ path_item : "StrPath" ,
2441+ packageName : str ,
24352442 module : types .ModuleType ,
24362443):
24372444 """Compute an ns-package subpath for a filesystem or zipfile importer"""
@@ -2454,7 +2461,7 @@ def file_ns_handler(
24542461
24552462
24562463def null_ns_handler (
2457- importer : Optional [ importlib . abc . PathEntryFinder ] ,
2464+ importer : object ,
24582465 path_item : Optional [str ],
24592466 packageName : Optional [str ],
24602467 module : Optional [_ModuleLike ],
@@ -3321,7 +3328,7 @@ def _always_object(classes):
33213328 return classes
33223329
33233330
3324- def _find_adapter (registry : Mapping [type , _AdapterType ], ob : object ):
3331+ def _find_adapter (registry : Mapping [type , _AdapterT ], ob : object ) -> _AdapterT :
33253332 """Return an adapter factory for `ob` from `registry`"""
33263333 types = _always_object (inspect .getmro (getattr (ob , '__class__' , type (ob ))))
33273334 for t in types :
0 commit comments