Skip to content

Commit 804ccd2

Browse files
authored
Simplified pkg_resources._declare_state (#4346)
2 parents fdd50e4 + 44bf554 commit 804ccd2

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

pkg_resources/__init__.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
import time
2828
import re
2929
import types
30-
from typing import Any, Callable, Dict, Iterable, List, Protocol, Optional
30+
from typing import Callable, Dict, Iterable, List, Protocol, Optional, TypeVar
3131
import zipfile
3232
import zipimport
3333
import warnings
@@ -103,6 +103,8 @@
103103
stacklevel=2,
104104
)
105105

106+
T = TypeVar("T")
107+
106108

107109
_PEP440_FALLBACK = re.compile(r"^v?(?P<safe>(?:[0-9]+!)?[0-9]+(?:\.[0-9]+)*)", re.I)
108110

@@ -117,11 +119,12 @@ class PEP440Warning(RuntimeWarning):
117119
parse_version = packaging.version.Version
118120

119121

120-
_state_vars: Dict[str, Any] = {}
122+
_state_vars: Dict[str, str] = {}
121123

122124

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
125128

126129

127130
def __getstate__():
@@ -2023,8 +2026,7 @@ def __init__(self, importer):
20232026

20242027
_distribution_finders: Dict[
20252028
type, Callable[[object, str, bool], Iterable["Distribution"]]
2026-
] = {}
2027-
_declare_state('dict', _distribution_finders=_distribution_finders)
2029+
] = _declare_state('dict', '_distribution_finders', {})
20282030

20292031

20302032
def register_finder(importer_type, distribution_finder):
@@ -2199,10 +2201,10 @@ def resolve_egg_link(path):
21992201

22002202
_namespace_handlers: Dict[
22012203
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+
)
22062208

22072209

22082210
def register_namespace_handler(importer_type, namespace_handler):
@@ -3301,8 +3303,7 @@ def _initialize_master_working_set():
33013303
Invocation by other packages is unsupported and done
33023304
at their own risk.
33033305
"""
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())
33063307

33073308
require = working_set.require
33083309
iter_entry_points = working_set.iter_entry_points

0 commit comments

Comments
 (0)