Skip to content

Commit 85a68ec

Browse files
committed
pkg_resources type the declared global variables
1 parent bac21fd commit 85a68ec

File tree

2 files changed

+31
-22
lines changed

2 files changed

+31
-22
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

+30-22
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 List, Protocol
30+
from typing import TYPE_CHECKING, List, Protocol
3131
import zipfile
3232
import zipimport
3333
import warnings
@@ -82,18 +82,6 @@
8282

8383
# declare some globals that will be defined later to
8484
# satisfy the linters.
85-
require = None
86-
working_set = None
87-
add_activation_listener = None
88-
cleanup_resources = None
89-
resource_stream = None
90-
set_extraction_path = None
91-
resource_isdir = None
92-
resource_string = None
93-
iter_entry_points = None
94-
resource_listdir = None
95-
resource_filename = None
96-
resource_exists = None
9785
_distribution_finders = None
9886
_namespace_handlers = None
9987
_namespace_packages = None
@@ -3261,6 +3249,15 @@ def _mkstemp(*args, **kw):
32613249
warnings.filterwarnings("ignore", category=PEP440Warning, append=True)
32623250

32633251

3252+
class PkgResourcesDeprecationWarning(Warning):
3253+
"""
3254+
Base class for warning about deprecations in ``pkg_resources``
3255+
3256+
This class is not derived from ``DeprecationWarning``, and as such is
3257+
visible by default.
3258+
"""
3259+
3260+
32643261
# from jaraco.functools 1.3
32653262
def _call_aside(f, *args, **kwargs):
32663263
f(*args, **kwargs)
@@ -3279,15 +3276,6 @@ def _initialize(g=globals()):
32793276
)
32803277

32813278

3282-
class PkgResourcesDeprecationWarning(Warning):
3283-
"""
3284-
Base class for warning about deprecations in ``pkg_resources``
3285-
3286-
This class is not derived from ``DeprecationWarning``, and as such is
3287-
visible by default.
3288-
"""
3289-
3290-
32913279
@_call_aside
32923280
def _initialize_master_working_set():
32933281
"""
@@ -3323,3 +3311,23 @@ def _initialize_master_working_set():
33233311
# match order
33243312
list(map(working_set.add_entry, sys.path))
33253313
globals().update(locals())
3314+
3315+
3316+
if TYPE_CHECKING:
3317+
# All of these are set by the @_call_aside methods above
3318+
__resource_manager = ResourceManager() # Won't exist at runtime
3319+
resource_exists = __resource_manager.resource_exists
3320+
resource_isdir = __resource_manager.resource_isdir
3321+
resource_filename = __resource_manager.resource_filename
3322+
resource_stream = __resource_manager.resource_stream
3323+
resource_string = __resource_manager.resource_string
3324+
resource_listdir = __resource_manager.resource_listdir
3325+
set_extraction_path = __resource_manager.set_extraction_path
3326+
cleanup_resources = __resource_manager.cleanup_resources
3327+
3328+
working_set = WorkingSet()
3329+
require = working_set.require
3330+
iter_entry_points = working_set.iter_entry_points
3331+
add_activation_listener = working_set.subscribe
3332+
run_script = working_set.run_script
3333+
run_main = run_script

0 commit comments

Comments
 (0)