109
109
)
110
110
111
111
112
- T = TypeVar ("T " )
112
+ _T = TypeVar ("_T " )
113
113
# Type aliases
114
114
_NestedStr = Union [str , Iterable [Union [str , Iterable ["_NestedStr" ]]]]
115
115
_InstallerType = Callable [["Requirement" ], Optional ["Distribution" ]]
118
118
_MetadataType = Optional ["IResourceProvider" ]
119
119
# Any object works, but let's indicate we expect something like a module (optionally has __loader__ or __file__)
120
120
_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
+ )
122
127
123
128
124
129
# Use _typeshed.importlib.LoaderProtocol once available https://github.com/python/typeshed/pull/11890
@@ -142,7 +147,7 @@ class PEP440Warning(RuntimeWarning):
142
147
_state_vars : Dict [str , str ] = {}
143
148
144
149
145
- def _declare_state (vartype : str , varname : str , initial_value : T ) -> T :
150
+ def _declare_state (vartype : str , varname : str , initial_value : _T ) -> _T :
146
151
_state_vars [varname ] = vartype
147
152
return initial_value
148
153
@@ -377,7 +382,7 @@ class UnknownExtra(ResolutionError):
377
382
"""Distribution doesn't have an "extra feature" of the given name"""
378
383
379
384
380
- _provider_factories : Dict [Type [_ModuleLike ], _AdapterType ] = {}
385
+ _provider_factories : Dict [Type [_ModuleLike ], _ProviderFactoryType ] = {}
381
386
382
387
PY_MAJOR = '{}.{}' .format (* sys .version_info )
383
388
EGG_DIST = 3
@@ -388,7 +393,7 @@ class UnknownExtra(ResolutionError):
388
393
389
394
390
395
def register_loader_type (
391
- loader_type : Type [_ModuleLike ], provider_factory : _AdapterType
396
+ loader_type : Type [_ModuleLike ], provider_factory : _ProviderFactoryType
392
397
):
393
398
"""Register `provider_factory` to make providers for `loader_type`
394
399
@@ -2097,12 +2102,12 @@ def __init__(self, importer: zipimport.zipimporter):
2097
2102
self ._setup_prefix ()
2098
2103
2099
2104
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
+ )
2103
2108
2104
2109
2105
- def register_finder (importer_type : type , distribution_finder : _AdapterType ):
2110
+ def register_finder (importer_type : Type [ _T ] , distribution_finder : _DistFinderType [ _T ] ):
2106
2111
"""Register `distribution_finder` to find distributions in sys.path items
2107
2112
2108
2113
`importer_type` is the type or class of a PEP 302 "Importer" (sys.path item
@@ -2276,15 +2281,17 @@ def resolve_egg_link(path):
2276
2281
2277
2282
register_finder (importlib .machinery .FileFinder , find_on_path )
2278
2283
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
+ )
2282
2287
_namespace_packages : Dict [Optional [str ], List [str ]] = _declare_state (
2283
2288
'dict' , '_namespace_packages' , {}
2284
2289
)
2285
2290
2286
2291
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
+ ):
2288
2295
"""Register `namespace_handler` to declare namespace packages
2289
2296
2290
2297
`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):
2429
2436
2430
2437
2431
2438
def file_ns_handler (
2432
- importer : Optional [ importlib . abc . PathEntryFinder ] ,
2433
- path_item ,
2434
- packageName ,
2439
+ importer : object ,
2440
+ path_item : "StrPath" ,
2441
+ packageName : str ,
2435
2442
module : types .ModuleType ,
2436
2443
):
2437
2444
"""Compute an ns-package subpath for a filesystem or zipfile importer"""
@@ -2454,7 +2461,7 @@ def file_ns_handler(
2454
2461
2455
2462
2456
2463
def null_ns_handler (
2457
- importer : Optional [ importlib . abc . PathEntryFinder ] ,
2464
+ importer : object ,
2458
2465
path_item : Optional [str ],
2459
2466
packageName : Optional [str ],
2460
2467
module : Optional [_ModuleLike ],
@@ -3321,7 +3328,7 @@ def _always_object(classes):
3321
3328
return classes
3322
3329
3323
3330
3324
- def _find_adapter (registry : Mapping [type , _AdapterType ], ob : object ):
3331
+ def _find_adapter (registry : Mapping [type , _AdapterT ], ob : object ) -> _AdapterT :
3325
3332
"""Return an adapter factory for `ob` from `registry`"""
3326
3333
types = _always_object (inspect .getmro (getattr (ob , '__class__' , type (ob ))))
3327
3334
for t in types :
0 commit comments