Skip to content

Commit 544b332

Browse files
authored
pkg_resources type the declared global variables (#4267)
1 parent d8148cc commit 544b332

File tree

2 files changed

+40
-25
lines changed

2 files changed

+40
-25
lines changed

newsfragments/4267.feature.rst

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Typed the dynamically defined variables from `pkg_resources` -- by :user:`Avasam`

pkg_resources/__init__.py

+39-25
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,16 @@
2727
import time
2828
import re
2929
import types
30-
from typing import Callable, Dict, Iterable, List, Protocol, Optional, TypeVar
30+
from typing import (
31+
TYPE_CHECKING,
32+
List,
33+
Protocol,
34+
Callable,
35+
Dict,
36+
Iterable,
37+
Optional,
38+
TypeVar,
39+
)
3140
import zipfile
3241
import zipimport
3342
import warnings
@@ -76,21 +85,6 @@
7685
from pkg_resources.extern.packaging import version as _packaging_version
7786
from pkg_resources.extern.platformdirs import user_cache_dir as _user_cache_dir
7887

79-
# declare some globals that will be defined later to
80-
# satisfy the linters.
81-
require = None
82-
working_set = None
83-
add_activation_listener = None
84-
cleanup_resources = None
85-
resource_stream = None
86-
set_extraction_path = None
87-
resource_isdir = None
88-
resource_string = None
89-
iter_entry_points = None
90-
resource_listdir = None
91-
resource_filename = None
92-
resource_exists = None
93-
9488

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

32593253

3254+
class PkgResourcesDeprecationWarning(Warning):
3255+
"""
3256+
Base class for warning about deprecations in ``pkg_resources``
3257+
3258+
This class is not derived from ``DeprecationWarning``, and as such is
3259+
visible by default.
3260+
"""
3261+
3262+
32603263
# from jaraco.functools 1.3
32613264
def _call_aside(f, *args, **kwargs):
32623265
f(*args, **kwargs)
@@ -3275,15 +3278,6 @@ def _initialize(g=globals()):
32753278
)
32763279

32773280

3278-
class PkgResourcesDeprecationWarning(Warning):
3279-
"""
3280-
Base class for warning about deprecations in ``pkg_resources``
3281-
3282-
This class is not derived from ``DeprecationWarning``, and as such is
3283-
visible by default.
3284-
"""
3285-
3286-
32873281
@_call_aside
32883282
def _initialize_master_working_set():
32893283
"""
@@ -3320,6 +3314,26 @@ def _initialize_master_working_set():
33203314
globals().update(locals())
33213315

33223316

3317+
if TYPE_CHECKING:
3318+
# All of these are set by the @_call_aside methods above
3319+
__resource_manager = ResourceManager() # Won't exist at runtime
3320+
resource_exists = __resource_manager.resource_exists
3321+
resource_isdir = __resource_manager.resource_isdir
3322+
resource_filename = __resource_manager.resource_filename
3323+
resource_stream = __resource_manager.resource_stream
3324+
resource_string = __resource_manager.resource_string
3325+
resource_listdir = __resource_manager.resource_listdir
3326+
set_extraction_path = __resource_manager.set_extraction_path
3327+
cleanup_resources = __resource_manager.cleanup_resources
3328+
3329+
working_set = WorkingSet()
3330+
require = working_set.require
3331+
iter_entry_points = working_set.iter_entry_points
3332+
add_activation_listener = working_set.subscribe
3333+
run_script = working_set.run_script
3334+
run_main = run_script
3335+
3336+
33233337
# ---- Ported from ``setuptools`` to avoid introducing an import inter-dependency ----
33243338
LOCALE_ENCODING = "locale" if sys.version_info >= (3, 10) else None
33253339

0 commit comments

Comments
 (0)