Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Made pkg_resoursces.NullProvider's has_metadata and metadata_isdir methods return actual booleans like all other Providers #4254

Merged
merged 2 commits into from Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions newsfragments/4254.bugfix.rst
@@ -0,0 +1 @@
Made ``pkg_resoursces.NullProvider``'s ``has_metadata`` and ``metadata_isdir`` methods return actual booleans like all other Providers. -- by :user:`Avasam`
26 changes: 13 additions & 13 deletions pkg_resources/__init__.py
Expand Up @@ -531,7 +531,7 @@ def get_entry_info(dist, group, name):


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

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

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

def metadata_listdir(self, name):
Expand Down Expand Up @@ -1489,9 +1489,9 @@ def has_resource(self, resource_name):
def _get_metadata_path(self, name):
return self._fn(self.egg_info, name)

def has_metadata(self, name):
def has_metadata(self, name) -> bool:
if not self.egg_info:
return self.egg_info
return False

path = self._get_metadata_path(name)
return self._has(path)
Expand All @@ -1515,8 +1515,8 @@ def get_metadata_lines(self, name):
def resource_isdir(self, resource_name):
return self._isdir(self._fn(self.module_path, resource_name))

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

def resource_listdir(self, resource_name):
return self._listdir(self._fn(self.module_path, resource_name))
Expand Down Expand Up @@ -1555,12 +1555,12 @@ def run_script(self, script_name, namespace):
script_code = compile(script_text, script_filename, 'exec')
exec(script_code, namespace, namespace)

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

def _isdir(self, path):
def _isdir(self, path) -> bool:
raise NotImplementedError(
"Can't perform this operation for unregistered loader type"
)
Expand Down Expand Up @@ -1695,10 +1695,10 @@ def _set_egg(self, path):
class DefaultProvider(EggProvider):
"""Provides access to package resources in the filesystem"""

def _has(self, path):
def _has(self, path) -> bool:
return os.path.exists(path)

def _isdir(self, path):
def _isdir(self, path) -> bool:
return os.path.isdir(path)

def _listdir(self, path):
Expand Down Expand Up @@ -1940,11 +1940,11 @@ def _index(self):
self._dirindex = ind
return ind

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

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

def _listdir(self, fspath):
Expand Down Expand Up @@ -1978,7 +1978,7 @@ def __init__(self, path):
def _get_metadata_path(self, name):
return self.path

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

def get_metadata(self, name):
Expand Down
2 changes: 1 addition & 1 deletion pkg_resources/tests/test_resources.py
Expand Up @@ -35,7 +35,7 @@ class Metadata(pkg_resources.EmptyProvider):
def __init__(self, *pairs):
self.metadata = dict(pairs)

def has_metadata(self, name):
def has_metadata(self, name) -> bool:
return name in self.metadata

def get_metadata(self, name):
Expand Down