27
27
import time
28
28
import re
29
29
import types
30
- from typing import Any , Callable , Dict , Iterable , List , Protocol , Optional
30
+ from typing import Callable , Dict , Iterable , List , Protocol , Optional , TypeVar
31
31
import zipfile
32
32
import zipimport
33
33
import warnings
103
103
stacklevel = 2 ,
104
104
)
105
105
106
+ T = TypeVar ("T" )
107
+
106
108
107
109
_PEP440_FALLBACK = re .compile (r"^v?(?P<safe>(?:[0-9]+!)?[0-9]+(?:\.[0-9]+)*)" , re .I )
108
110
@@ -117,11 +119,12 @@ class PEP440Warning(RuntimeWarning):
117
119
parse_version = packaging .version .Version
118
120
119
121
120
- _state_vars : Dict [str , Any ] = {}
122
+ _state_vars : Dict [str , str ] = {}
121
123
122
124
123
- def _declare_state (vartype : str , ** kw : object ) -> None :
124
- _state_vars .update (dict .fromkeys (kw , vartype ))
125
+ def _declare_state (vartype : str , varname : str , initial_value : T ) -> T :
126
+ _state_vars [varname ] = vartype
127
+ return initial_value
125
128
126
129
127
130
def __getstate__ ():
@@ -2023,8 +2026,7 @@ def __init__(self, importer):
2023
2026
2024
2027
_distribution_finders : Dict [
2025
2028
type , Callable [[object , str , bool ], Iterable ["Distribution" ]]
2026
- ] = {}
2027
- _declare_state ('dict' , _distribution_finders = _distribution_finders )
2029
+ ] = _declare_state ('dict' , '_distribution_finders' , {})
2028
2030
2029
2031
2030
2032
def register_finder (importer_type , distribution_finder ):
@@ -2199,10 +2201,10 @@ def resolve_egg_link(path):
2199
2201
2200
2202
_namespace_handlers : Dict [
2201
2203
type , Callable [[object , str , str , types .ModuleType ], Optional [str ]]
2202
- ] = {}
2203
- _declare_state ( 'dict' , _namespace_handlers = _namespace_handlers )
2204
- _namespace_packages : Dict [ Optional [ str ], List [ str ]] = {}
2205
- _declare_state ( 'dict' , _namespace_packages = _namespace_packages )
2204
+ ] = _declare_state ( 'dict' , '_namespace_handlers' , {})
2205
+ _namespace_packages : Dict [ Optional [ str ], List [ str ]] = _declare_state (
2206
+ 'dict' , '_namespace_packages' , {}
2207
+ )
2206
2208
2207
2209
2208
2210
def register_namespace_handler (importer_type , namespace_handler ):
@@ -3301,8 +3303,7 @@ def _initialize_master_working_set():
3301
3303
Invocation by other packages is unsupported and done
3302
3304
at their own risk.
3303
3305
"""
3304
- working_set = WorkingSet ._build_master ()
3305
- _declare_state ('object' , working_set = working_set )
3306
+ working_set = _declare_state ('object' , 'working_set' , WorkingSet ._build_master ())
3306
3307
3307
3308
require = working_set .require
3308
3309
iter_entry_points = working_set .iter_entry_points
0 commit comments