Skip to content

Commit 56bd481

Browse files
committed
Made pkg_resoursces.NullProvider's has_metadata and metadata_isdir methods return actual booleans like all other Providers
1 parent dbc6471 commit 56bd481

File tree

3 files changed

+15
-14
lines changed

3 files changed

+15
-14
lines changed

newsfragments/4254.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Made ``pkg_resoursces.NullProvider``'s ``has_metadata`` and ``metadata_isdir`` methods return actual booleans like all other Providers. -- by :user:`Avasam`

pkg_resources/__init__.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,7 @@ def get_entry_info(dist, group, name):
531531

532532

533533
class IMetadataProvider(Protocol):
534-
def has_metadata(self, name):
534+
def has_metadata(self, name) -> bool:
535535
"""Does the package's distribution contain the named metadata?"""
536536

537537
def get_metadata(self, name):
@@ -543,7 +543,7 @@ def get_metadata_lines(self, name):
543543
Leading and trailing whitespace is stripped from each line, and lines
544544
with ``#`` as the first non-blank character are omitted."""
545545

546-
def metadata_isdir(self, name):
546+
def metadata_isdir(self, name) -> bool:
547547
"""Is the named metadata a directory? (like ``os.path.isdir()``)"""
548548

549549
def metadata_listdir(self, name):
@@ -1488,9 +1488,9 @@ def has_resource(self, resource_name):
14881488
def _get_metadata_path(self, name):
14891489
return self._fn(self.egg_info, name)
14901490

1491-
def has_metadata(self, name):
1491+
def has_metadata(self, name) -> bool:
14921492
if not self.egg_info:
1493-
return self.egg_info
1493+
return False
14941494

14951495
path = self._get_metadata_path(name)
14961496
return self._has(path)
@@ -1514,8 +1514,8 @@ def get_metadata_lines(self, name):
15141514
def resource_isdir(self, resource_name):
15151515
return self._isdir(self._fn(self.module_path, resource_name))
15161516

1517-
def metadata_isdir(self, name):
1518-
return self.egg_info and self._isdir(self._fn(self.egg_info, name))
1517+
def metadata_isdir(self, name) -> bool:
1518+
return bool(self.egg_info and self._isdir(self._fn(self.egg_info, name)))
15191519

15201520
def resource_listdir(self, resource_name):
15211521
return self._listdir(self._fn(self.module_path, resource_name))
@@ -1554,12 +1554,12 @@ def run_script(self, script_name, namespace):
15541554
script_code = compile(script_text, script_filename, 'exec')
15551555
exec(script_code, namespace, namespace)
15561556

1557-
def _has(self, path):
1557+
def _has(self, path) -> bool:
15581558
raise NotImplementedError(
15591559
"Can't perform this operation for unregistered loader type"
15601560
)
15611561

1562-
def _isdir(self, path):
1562+
def _isdir(self, path) -> bool:
15631563
raise NotImplementedError(
15641564
"Can't perform this operation for unregistered loader type"
15651565
)
@@ -1694,10 +1694,10 @@ def _set_egg(self, path):
16941694
class DefaultProvider(EggProvider):
16951695
"""Provides access to package resources in the filesystem"""
16961696

1697-
def _has(self, path):
1697+
def _has(self, path) -> bool:
16981698
return os.path.exists(path)
16991699

1700-
def _isdir(self, path):
1700+
def _isdir(self, path) -> bool:
17011701
return os.path.isdir(path)
17021702

17031703
def _listdir(self, path):
@@ -1939,11 +1939,11 @@ def _index(self):
19391939
self._dirindex = ind
19401940
return ind
19411941

1942-
def _has(self, fspath):
1942+
def _has(self, fspath) -> bool:
19431943
zip_path = self._zipinfo_name(fspath)
19441944
return zip_path in self.zipinfo or zip_path in self._index()
19451945

1946-
def _isdir(self, fspath):
1946+
def _isdir(self, fspath) -> bool:
19471947
return self._zipinfo_name(fspath) in self._index()
19481948

19491949
def _listdir(self, fspath):
@@ -1977,7 +1977,7 @@ def __init__(self, path):
19771977
def _get_metadata_path(self, name):
19781978
return self.path
19791979

1980-
def has_metadata(self, name):
1980+
def has_metadata(self, name) -> bool:
19811981
return name == 'PKG-INFO' and os.path.isfile(self.path)
19821982

19831983
def get_metadata(self, name):

pkg_resources/tests/test_resources.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class Metadata(pkg_resources.EmptyProvider):
3535
def __init__(self, *pairs):
3636
self.metadata = dict(pairs)
3737

38-
def has_metadata(self, name):
38+
def has_metadata(self, name) -> bool:
3939
return name in self.metadata
4040

4141
def get_metadata(self, name):

0 commit comments

Comments
 (0)