Skip to content

Commit

Permalink
Simplified pkg_resources._declare_state (#4346)
Browse files Browse the repository at this point in the history
  • Loading branch information
abravalheri committed May 9, 2024
2 parents fdd50e4 + 44bf554 commit 804ccd2
Showing 1 changed file with 13 additions and 12 deletions.
25 changes: 13 additions & 12 deletions pkg_resources/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import time
import re
import types
from typing import Any, Callable, Dict, Iterable, List, Protocol, Optional
from typing import Callable, Dict, Iterable, List, Protocol, Optional, TypeVar
import zipfile
import zipimport
import warnings
Expand Down Expand Up @@ -103,6 +103,8 @@
stacklevel=2,
)

T = TypeVar("T")


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

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


_state_vars: Dict[str, Any] = {}
_state_vars: Dict[str, str] = {}


def _declare_state(vartype: str, **kw: object) -> None:
_state_vars.update(dict.fromkeys(kw, vartype))
def _declare_state(vartype: str, varname: str, initial_value: T) -> T:
_state_vars[varname] = vartype
return initial_value


def __getstate__():
Expand Down Expand Up @@ -2023,8 +2026,7 @@ def __init__(self, importer):

_distribution_finders: Dict[
type, Callable[[object, str, bool], Iterable["Distribution"]]
] = {}
_declare_state('dict', _distribution_finders=_distribution_finders)
] = _declare_state('dict', '_distribution_finders', {})


def register_finder(importer_type, distribution_finder):
Expand Down Expand Up @@ -2199,10 +2201,10 @@ def resolve_egg_link(path):

_namespace_handlers: Dict[
type, Callable[[object, str, str, types.ModuleType], Optional[str]]
] = {}
_declare_state('dict', _namespace_handlers=_namespace_handlers)
_namespace_packages: Dict[Optional[str], List[str]] = {}
_declare_state('dict', _namespace_packages=_namespace_packages)
] = _declare_state('dict', '_namespace_handlers', {})
_namespace_packages: Dict[Optional[str], List[str]] = _declare_state(
'dict', '_namespace_packages', {}
)


def register_namespace_handler(importer_type, namespace_handler):
Expand Down Expand Up @@ -3301,8 +3303,7 @@ def _initialize_master_working_set():
Invocation by other packages is unsupported and done
at their own risk.
"""
working_set = WorkingSet._build_master()
_declare_state('object', working_set=working_set)
working_set = _declare_state('object', 'working_set', WorkingSet._build_master())

require = working_set.require
iter_entry_points = working_set.iter_entry_points
Expand Down

0 comments on commit 804ccd2

Please sign in to comment.