Skip to content

Commit

Permalink
Made pkg_resoursces.NullProvider's has_metadata and `metadata_isd…
Browse files Browse the repository at this point in the history
…ir` methods return actual booleans like all other Providers
  • Loading branch information
Avasam committed Mar 4, 2024
1 parent dbc6471 commit 752fe1d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
26 changes: 13 additions & 13 deletions pkg_resources/__init__.py
Original file line number Diff line number Diff line change
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 @@ -1488,9 +1488,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 @@ -1514,8 +1514,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 @@ -1554,12 +1554,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 @@ -1694,10 +1694,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 @@ -1939,11 +1939,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 @@ -1977,7 +1977,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
Original file line number Diff line number Diff line change
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

0 comments on commit 752fe1d

Please sign in to comment.