Skip to content

Commit

Permalink
pkg_resources: Clarify some methods return bytes, not str (#4243)
Browse files Browse the repository at this point in the history
  • Loading branch information
abravalheri authored Mar 6, 2024
2 parents b4b622e + 15f7ef7 commit 0435c72
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
1 change: 1 addition & 0 deletions newsfragments/4243.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Clarify some `pkg_resources` methods return `bytes`, not `str`. Also return an empty `bytes` in ``EmptyProvider._get`` -- by :user:`Avasam`
18 changes: 9 additions & 9 deletions pkg_resources/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -566,8 +566,8 @@ def get_resource_stream(self, manager, resource_name):
`manager` must be an ``IResourceManager``"""

def get_resource_string(self, manager, resource_name):
"""Return a string containing the contents of `resource_name`
def get_resource_string(self, manager, resource_name) -> bytes:
"""Return the contents of `resource_name` as :obj:`bytes`
`manager` must be an ``IResourceManager``"""

Expand Down Expand Up @@ -1203,8 +1203,8 @@ def resource_stream(self, package_or_requirement, resource_name):
self, resource_name
)

def resource_string(self, package_or_requirement, resource_name):
"""Return specified resource as a string"""
def resource_string(self, package_or_requirement, resource_name) -> bytes:
"""Return specified resource as :obj:`bytes`"""
return get_provider(package_or_requirement).get_resource_string(
self, resource_name
)
Expand Down Expand Up @@ -1480,7 +1480,7 @@ def get_resource_filename(self, manager, resource_name):
def get_resource_stream(self, manager, resource_name):
return io.BytesIO(self.get_resource_string(manager, resource_name))

def get_resource_string(self, manager, resource_name):
def get_resource_string(self, manager, resource_name) -> bytes:
return self._get(self._fn(self.module_path, resource_name))

def has_resource(self, resource_name):
Expand Down Expand Up @@ -1650,7 +1650,7 @@ def _validate_resource_path(path):
DeprecationWarning,
)

def _get(self, path):
def _get(self, path) -> bytes:
if hasattr(self.loader, 'get_data'):
return self.loader.get_data(path)
raise NotImplementedError(
Expand Down Expand Up @@ -1707,7 +1707,7 @@ def _listdir(self, path):
def get_resource_stream(self, manager, resource_name):
return open(self._fn(self.module_path, resource_name), 'rb')

def _get(self, path):
def _get(self, path) -> bytes:
with open(path, 'rb') as stream:
return stream.read()

Expand All @@ -1732,8 +1732,8 @@ class EmptyProvider(NullProvider):

_isdir = _has = lambda self, path: False

def _get(self, path):
return ''
def _get(self, path) -> bytes:
return b''

def _listdir(self, path):
return []
Expand Down

0 comments on commit 0435c72

Please sign in to comment.