Skip to content

Commit 6dc694e

Browse files
authored
pkg_resources: Runtime fixes to make typing annotations work (pypa#4262)
2 parents dcfc833 + af23e4c commit 6dc694e

File tree

2 files changed

+18
-16
lines changed

2 files changed

+18
-16
lines changed

newsfragments/4262.feature.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Improved `AttributeError` error message if ``pkg_resources.EntryPoint.require`` is called without extras or distribution
2+
Gracefully "do nothing" when trying to activate a ``pkg_resources.Distribution`` with a `None` location, rather than raising a `TypeError`
3+
-- by :user:`Avasam`

pkg_resources/__init__.py

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -920,10 +920,10 @@ def find_plugins(self, plugin_env, full_env=None, installer=None, fallback=True)
920920
# success, no need to try any more versions of this project
921921
break
922922

923-
distributions = list(distributions)
924-
distributions.sort()
923+
sorted_distributions = list(distributions)
924+
sorted_distributions.sort()
925925

926-
return distributions, error_info
926+
return sorted_distributions, error_info
927927

928928
def require(self, *requirements):
929929
"""Ensure that distributions matching `requirements` are activated
@@ -1635,7 +1635,7 @@ def _validate_resource_path(path):
16351635
)
16361636

16371637
def _get(self, path) -> bytes:
1638-
if hasattr(self.loader, 'get_data'):
1638+
if hasattr(self.loader, 'get_data') and self.loader:
16391639
return self.loader.get_data(path)
16401640
raise NotImplementedError(
16411641
"Can't perform this operation for loaders without 'get_data()'"
@@ -2490,8 +2490,9 @@ def resolve(self):
24902490
raise ImportError(str(exc)) from exc
24912491

24922492
def require(self, env=None, installer=None):
2493-
if self.extras and not self.dist:
2494-
raise UnknownExtra("Can't require() without a distribution", self)
2493+
if not self.dist:
2494+
error_cls = UnknownExtra if self.extras else AttributeError
2495+
raise error_cls("Can't require() without a distribution", self)
24952496

24962497
# Get the requirements for this entry point with all its extras and
24972498
# then resolve them. We have to pass `extras` along when resolving so
@@ -2557,11 +2558,11 @@ def parse_group(cls, group, lines, dist=None):
25572558
def parse_map(cls, data, dist=None):
25582559
"""Parse a map of entry point groups"""
25592560
if isinstance(data, dict):
2560-
data = data.items()
2561+
_data = data.items()
25612562
else:
2562-
data = split_sections(data)
2563+
_data = split_sections(data)
25632564
maps = {}
2564-
for group, lines in data:
2565+
for group, lines in _data:
25652566
if group is None:
25662567
if not lines:
25672568
continue
@@ -2823,7 +2824,7 @@ def activate(self, path=None, replace=False):
28232824
if path is None:
28242825
path = sys.path
28252826
self.insert_on(path, replace=replace)
2826-
if path is sys.path:
2827+
if path is sys.path and self.location is not None:
28272828
fixup_namespace_packages(self.location)
28282829
for pkg in self._get_metadata('namespace_packages.txt'):
28292830
if pkg in sys.modules:
@@ -2891,15 +2892,13 @@ def load_entry_point(self, group, name):
28912892

28922893
def get_entry_map(self, group=None):
28932894
"""Return the entry point map for `group`, or the full entry map"""
2894-
try:
2895-
ep_map = self._ep_map
2896-
except AttributeError:
2897-
ep_map = self._ep_map = EntryPoint.parse_map(
2895+
if not hasattr(self, "_ep_map"):
2896+
self._ep_map = EntryPoint.parse_map(
28982897
self._get_metadata('entry_points.txt'), self
28992898
)
29002899
if group is not None:
2901-
return ep_map.get(group, {})
2902-
return ep_map
2900+
return self._ep_map.get(group, {})
2901+
return self._ep_map
29032902

29042903
def get_entry_info(self, group, name):
29052904
"""Return the EntryPoint object for `group`+`name`, or ``None``"""

0 commit comments

Comments
 (0)