From 1827bfb1385df3ac575a0721241e4953235ddbbe Mon Sep 17 00:00:00 2001 From: Avasam Date: Tue, 27 Feb 2024 18:23:13 -0500 Subject: [PATCH 1/6] pkg_resources: Fix type of pre-declared variables --- pkg_resources/__init__.py | 82 +++++++++++++++++++-------------------- 1 file changed, 39 insertions(+), 43 deletions(-) diff --git a/pkg_resources/__init__.py b/pkg_resources/__init__.py index 10c6a9cd06..afdf68ac04 100644 --- a/pkg_resources/__init__.py +++ b/pkg_resources/__init__.py @@ -27,7 +27,8 @@ import time import re import types -from typing import Protocol +from collections.abc import Callable, Iterator +from typing import TYPE_CHECKING, Any, Dict, List, Optional, Protocol import zipfile import zipimport import warnings @@ -80,26 +81,6 @@ __import__('pkg_resources.extern.packaging.markers') __import__('pkg_resources.extern.packaging.utils') -# declare some globals that will be defined later to -# satisfy the linters. -require = None -working_set = None -add_activation_listener = None -resources_stream = None -cleanup_resources = None -resource_dir = 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 -_distribution_finders = None -_namespace_handlers = None -_namespace_packages = None - warnings.warn( "pkg_resources is deprecated as an API. " @@ -491,19 +472,6 @@ def compatible_platforms(provided, required): return False -def run_script(dist_spec, script_name): - """Locate distribution `dist_spec` and run its `script_name` script""" - ns = sys._getframe(1).f_globals - name = ns['__name__'] - ns.clear() - ns['__name__'] = name - require(dist_spec)[0].run_script(script_name, ns) - - -# backward compatibility -run_main = run_script - - def get_distribution(dist): """Return a current distribution object for a Requirement or string""" if isinstance(dist, str): @@ -2040,6 +2008,9 @@ def __init__(self, importer): self._setup_prefix() +_distribution_finders: Dict[ + type, Callable[[Any, str, bool], Iterator["Distribution"]] +] = {} _declare_state('dict', _distribution_finders={}) @@ -2214,7 +2185,11 @@ def resolve_egg_link(path): register_finder(importlib.machinery.FileFinder, find_on_path) +_namespace_handlers: Dict[ + type, Callable[[Any, str, str, types.ModuleType], Optional[str]] +] = {} _declare_state('dict', _namespace_handlers={}) +_namespace_packages: Dict[Optional[str], List[str]] = {} _declare_state('dict', _namespace_packages={}) @@ -3273,6 +3248,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) @@ -3291,15 +3275,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(): """ @@ -3335,3 +3310,24 @@ def _initialize_master_working_set(): # match order list(map(working_set.add_entry, sys.path)) globals().update(locals()) + + +# All of these are set by the @_call_aside methods above +# Declaring the variables to satisfy checkers so they know these exist +if TYPE_CHECKING: + __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 From fbcca3b8de5263ba9bf6cbdaee04e34b13b520db Mon Sep 17 00:00:00 2001 From: Avasam Date: Tue, 27 Feb 2024 18:44:25 -0500 Subject: [PATCH 2/6] Include imports --- pkg_resources/__init__.py | 62 ++++++++++++++++++++++++--------------- 1 file changed, 39 insertions(+), 23 deletions(-) diff --git a/pkg_resources/__init__.py b/pkg_resources/__init__.py index afdf68ac04..25f1f96150 100644 --- a/pkg_resources/__init__.py +++ b/pkg_resources/__init__.py @@ -27,8 +27,8 @@ import time import re import types -from collections.abc import Callable, Iterator from typing import TYPE_CHECKING, Any, Dict, List, Optional, Protocol +from collections.abc import Callable, Iterator import zipfile import zipimport import warnings @@ -66,20 +66,44 @@ from os import open as os_open from os.path import isdir, split -from pkg_resources.extern.jaraco.text import ( - yield_lines, - drop_comment, - join_continuation, -) - -from pkg_resources.extern import platformdirs -from pkg_resources.extern import packaging +if not TYPE_CHECKING: + from pkg_resources.extern.jaraco.text import ( + yield_lines, + drop_comment, + join_continuation, + ) -__import__('pkg_resources.extern.packaging.version') -__import__('pkg_resources.extern.packaging.specifiers') -__import__('pkg_resources.extern.packaging.requirements') -__import__('pkg_resources.extern.packaging.markers') -__import__('pkg_resources.extern.packaging.utils') + from pkg_resources.extern import platformdirs + from pkg_resources.extern import packaging + + __import__('pkg_resources.extern.packaging.version') + __import__('pkg_resources.extern.packaging.specifiers') + __import__('pkg_resources.extern.packaging.requirements') + __import__('pkg_resources.extern.packaging.markers') + __import__('pkg_resources.extern.packaging.utils') +else: + # Replicates the imports above in a way that can be understood by type-checkers + from jaraco.text import ( + yield_lines, + drop_comment, + join_continuation, + ) + import platformdirs + import packaging + import packaging.version + import packaging.specifiers + import packaging.requirements + import packaging.markers + import packaging.utils + + # Declare some globals that will be defined by calls to _declare_state so checkers know they exist and their type + _distribution_finders: Dict[ + type, Callable[[Any, str, bool], Iterator["Distribution"]] + ] = {} + _namespace_handlers: Dict[ + type, Callable[[Any, str, str, types.ModuleType], Optional[str]] + ] = {} + _namespace_packages: Dict[Optional[str], List[str]] = {} warnings.warn( @@ -2008,9 +2032,6 @@ def __init__(self, importer): self._setup_prefix() -_distribution_finders: Dict[ - type, Callable[[Any, str, bool], Iterator["Distribution"]] -] = {} _declare_state('dict', _distribution_finders={}) @@ -2185,11 +2206,7 @@ def resolve_egg_link(path): register_finder(importlib.machinery.FileFinder, find_on_path) -_namespace_handlers: Dict[ - type, Callable[[Any, str, str, types.ModuleType], Optional[str]] -] = {} _declare_state('dict', _namespace_handlers={}) -_namespace_packages: Dict[Optional[str], List[str]] = {} _declare_state('dict', _namespace_packages={}) @@ -3312,9 +3329,8 @@ def _initialize_master_working_set(): globals().update(locals()) -# All of these are set by the @_call_aside methods above -# Declaring the variables to satisfy checkers so they know these exist 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 From cebb4102f36e97b19039d4b2ce810db70edca0b8 Mon Sep 17 00:00:00 2001 From: Avasam Date: Wed, 6 Mar 2024 12:40:48 -0500 Subject: [PATCH 3/6] Reduce changes --- pkg_resources/__init__.py | 62 +++++++++++++++++++++++---------------- 1 file changed, 36 insertions(+), 26 deletions(-) diff --git a/pkg_resources/__init__.py b/pkg_resources/__init__.py index 4d14fbdf89..d465907169 100644 --- a/pkg_resources/__init__.py +++ b/pkg_resources/__init__.py @@ -27,8 +27,7 @@ import time import re import types -from typing import TYPE_CHECKING, Any, Dict, List, Optional, Protocol -from collections.abc import Callable, Iterator +from typing import TYPE_CHECKING, Callable, Dict, Iterable, List, Optional, Protocol import zipfile import zipimport import warnings @@ -66,7 +65,21 @@ from os import open as os_open from os.path import isdir, split -if not TYPE_CHECKING: +if TYPE_CHECKING: + # Replicates the imports below in a way that can be understood by type-checkers + from jaraco.text import ( + yield_lines, + drop_comment, + join_continuation, + ) + import platformdirs + import packaging + import packaging.version + import packaging.specifiers + import packaging.requirements + import packaging.markers + import packaging.utils +else: from pkg_resources.extern.jaraco.text import ( yield_lines, drop_comment, @@ -81,29 +94,6 @@ __import__('pkg_resources.extern.packaging.requirements') __import__('pkg_resources.extern.packaging.markers') __import__('pkg_resources.extern.packaging.utils') -else: - # Replicates the imports above in a way that can be understood by type-checkers - from jaraco.text import ( - yield_lines, - drop_comment, - join_continuation, - ) - import platformdirs - import packaging - import packaging.version - import packaging.specifiers - import packaging.requirements - import packaging.markers - import packaging.utils - - # Declare some globals that will be defined by calls to _declare_state so checkers know they exist and their type - _distribution_finders: Dict[ - type, Callable[[Any, str, bool], Iterator["Distribution"]] - ] = {} - _namespace_handlers: Dict[ - type, Callable[[Any, str, str, types.ModuleType], Optional[str]] - ] = {} - _namespace_packages: Dict[Optional[str], List[str]] = {} warnings.warn( @@ -496,6 +486,19 @@ def compatible_platforms(provided, required): return False +def run_script(dist_spec, script_name): + """Locate distribution `dist_spec` and run its `script_name` script""" + ns = sys._getframe(1).f_globals + name = ns['__name__'] + ns.clear() + ns['__name__'] = name + require(dist_spec)[0].run_script(script_name, ns) + + +# backward compatibility +run_main = run_script + + def get_distribution(dist): """Return a current distribution object for a Requirement or string""" if isinstance(dist, str): @@ -2033,6 +2036,9 @@ def __init__(self, importer): self._setup_prefix() +_distribution_finders: Dict[ + type, Callable[[object, str, bool], Iterable["Distribution"]] +] = {} _declare_state('dict', _distribution_finders={}) @@ -2207,7 +2213,11 @@ def resolve_egg_link(path): register_finder(importlib.machinery.FileFinder, find_on_path) +_namespace_handlers: Dict[ + type, Callable[[object, str, str, types.ModuleType], Optional[str]] +] = {} _declare_state('dict', _namespace_handlers={}) +_namespace_packages: Dict[Optional[str], List[str]] = {} _declare_state('dict', _namespace_packages={}) From e19f4a9033cf424959a7c745065d4a3c2306ca93 Mon Sep 17 00:00:00 2001 From: Avasam Date: Wed, 6 Mar 2024 12:53:56 -0500 Subject: [PATCH 4/6] elipsis issue in .py files --- pkg_resources/__init__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg_resources/__init__.py b/pkg_resources/__init__.py index d465907169..6c14668eba 100644 --- a/pkg_resources/__init__.py +++ b/pkg_resources/__init__.py @@ -3344,7 +3344,7 @@ def _initialize_master_working_set(): if TYPE_CHECKING: # All of these are set by the @_call_aside methods above - __resource_manager: ResourceManager = ... # Won't exist at runtime + __resource_manager: ResourceManager = ResourceManager() # Won't exist at runtime resource_exists = __resource_manager.resource_exists resource_isdir = __resource_manager.resource_isdir resource_filename = __resource_manager.resource_filename @@ -3354,7 +3354,7 @@ def _initialize_master_working_set(): set_extraction_path = __resource_manager.set_extraction_path cleanup_resources = __resource_manager.cleanup_resources - working_set: WorkingSet = ... + working_set: WorkingSet = WorkingSet() require = working_set.require iter_entry_points = working_set.iter_entry_points add_activation_listener = working_set.subscribe From b68edb492228b636d4b26855dc333afab1894d38 Mon Sep 17 00:00:00 2001 From: Avasam Date: Fri, 8 Mar 2024 01:14:23 -0500 Subject: [PATCH 5/6] Revert extracted changes --- pkg_resources/__init__.py | 65 +++++++++++++++++---------------------- 1 file changed, 28 insertions(+), 37 deletions(-) diff --git a/pkg_resources/__init__.py b/pkg_resources/__init__.py index 1d94a88788..1a900bed42 100644 --- a/pkg_resources/__init__.py +++ b/pkg_resources/__init__.py @@ -27,7 +27,7 @@ import time import re import types -from typing import TYPE_CHECKING, Callable, Dict, Iterable, List, Optional, Protocol +from typing import TYPE_CHECKING, List, Protocol import zipfile import zipimport import warnings @@ -95,6 +95,24 @@ __import__('pkg_resources.extern.packaging.markers') __import__('pkg_resources.extern.packaging.utils') +# 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 +_distribution_finders = None +_namespace_handlers = None +_namespace_packages = None + warnings.warn( "pkg_resources is deprecated as an API. " @@ -2023,9 +2041,6 @@ def __init__(self, importer): self._setup_prefix() -_distribution_finders: Dict[ - type, Callable[[object, str, bool], Iterable["Distribution"]] -] = {} _declare_state('dict', _distribution_finders={}) @@ -2200,11 +2215,7 @@ def resolve_egg_link(path): register_finder(importlib.machinery.FileFinder, find_on_path) -_namespace_handlers: Dict[ - type, Callable[[object, str, str, types.ModuleType], Optional[str]] -] = {} _declare_state('dict', _namespace_handlers={}) -_namespace_packages: Dict[Optional[str], List[str]] = {} _declare_state('dict', _namespace_packages={}) @@ -3265,15 +3276,6 @@ 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) @@ -3292,6 +3294,15 @@ 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(): """ @@ -3327,23 +3338,3 @@ def _initialize_master_working_set(): # match order list(map(working_set.add_entry, sys.path)) globals().update(locals()) - - -if TYPE_CHECKING: - # All of these are set by the @_call_aside methods above - __resource_manager: ResourceManager = 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 = 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 From 5b19c270346546438e34588489efa6dcf88afbd2 Mon Sep 17 00:00:00 2001 From: Avasam Date: Thu, 9 May 2024 11:56:40 -0400 Subject: [PATCH 6/6] Deduplicated typing import --- pkg_resources/__init__.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/pkg_resources/__init__.py b/pkg_resources/__init__.py index d99047ebe0..0b47d7d4c8 100644 --- a/pkg_resources/__init__.py +++ b/pkg_resources/__init__.py @@ -27,8 +27,16 @@ import time import re import types -from typing import TYPE_CHECKING, List, Protocol -from typing import Callable, Dict, Iterable, Optional, TypeVar +from typing import ( + TYPE_CHECKING, + List, + Protocol, + Callable, + Dict, + Iterable, + Optional, + TypeVar, +) import zipfile import zipimport import warnings