Skip to content

Commit

Permalink
pkg_resources type the declared global variables (#4267)
Browse files Browse the repository at this point in the history
  • Loading branch information
Avasam authored May 13, 2024
1 parent d8148cc commit 544b332
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 25 deletions.
1 change: 1 addition & 0 deletions newsfragments/4267.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Typed the dynamically defined variables from `pkg_resources` -- by :user:`Avasam`
64 changes: 39 additions & 25 deletions pkg_resources/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,16 @@
import time
import re
import types
from typing import Callable, Dict, Iterable, List, Protocol, Optional, TypeVar
from typing import (
TYPE_CHECKING,
List,
Protocol,
Callable,
Dict,
Iterable,
Optional,
TypeVar,
)
import zipfile
import zipimport
import warnings
Expand Down Expand Up @@ -76,21 +85,6 @@
from pkg_resources.extern.packaging import version as _packaging_version
from pkg_resources.extern.platformdirs import user_cache_dir as _user_cache_dir

# declare some globals that will be defined later to
# satisfy the linters.
require = None
working_set = None
add_activation_listener = None
cleanup_resources = None
resource_stream = None
set_extraction_path = None
resource_isdir = None
resource_string = None
iter_entry_points = None
resource_listdir = None
resource_filename = None
resource_exists = None


warnings.warn(
"pkg_resources is deprecated as an API. "
Expand Down Expand Up @@ -3257,6 +3251,15 @@ def _mkstemp(*args, **kw):
warnings.filterwarnings("ignore", category=PEP440Warning, append=True)


class PkgResourcesDeprecationWarning(Warning):
"""
Base class for warning about deprecations in ``pkg_resources``
This class is not derived from ``DeprecationWarning``, and as such is
visible by default.
"""


# from jaraco.functools 1.3
def _call_aside(f, *args, **kwargs):
f(*args, **kwargs)
Expand All @@ -3275,15 +3278,6 @@ def _initialize(g=globals()):
)


class PkgResourcesDeprecationWarning(Warning):
"""
Base class for warning about deprecations in ``pkg_resources``
This class is not derived from ``DeprecationWarning``, and as such is
visible by default.
"""


@_call_aside
def _initialize_master_working_set():
"""
Expand Down Expand Up @@ -3320,6 +3314,26 @@ def _initialize_master_working_set():
globals().update(locals())


if TYPE_CHECKING:
# All of these are set by the @_call_aside methods above
__resource_manager = ResourceManager() # Won't exist at runtime
resource_exists = __resource_manager.resource_exists
resource_isdir = __resource_manager.resource_isdir
resource_filename = __resource_manager.resource_filename
resource_stream = __resource_manager.resource_stream
resource_string = __resource_manager.resource_string
resource_listdir = __resource_manager.resource_listdir
set_extraction_path = __resource_manager.set_extraction_path
cleanup_resources = __resource_manager.cleanup_resources

working_set = WorkingSet()
require = working_set.require
iter_entry_points = working_set.iter_entry_points
add_activation_listener = working_set.subscribe
run_script = working_set.run_script
run_main = run_script


# ---- Ported from ``setuptools`` to avoid introducing an import inter-dependency ----
LOCALE_ENCODING = "locale" if sys.version_info >= (3, 10) else None

Expand Down

0 comments on commit 544b332

Please sign in to comment.