Skip to content

Commit 0435c72

Browse files
authored
pkg_resources: Clarify some methods return bytes, not str (#4243)
2 parents b4b622e + 15f7ef7 commit 0435c72

File tree

2 files changed

+10
-9
lines changed

2 files changed

+10
-9
lines changed

newsfragments/4243.bugfix.rst

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Clarify some `pkg_resources` methods return `bytes`, not `str`. Also return an empty `bytes` in ``EmptyProvider._get`` -- by :user:`Avasam`

pkg_resources/__init__.py

+9-9
Original file line numberDiff line numberDiff line change
@@ -566,8 +566,8 @@ def get_resource_stream(self, manager, resource_name):
566566
567567
`manager` must be an ``IResourceManager``"""
568568

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

@@ -1203,8 +1203,8 @@ def resource_stream(self, package_or_requirement, resource_name):
12031203
self, resource_name
12041204
)
12051205

1206-
def resource_string(self, package_or_requirement, resource_name):
1207-
"""Return specified resource as a string"""
1206+
def resource_string(self, package_or_requirement, resource_name) -> bytes:
1207+
"""Return specified resource as :obj:`bytes`"""
12081208
return get_provider(package_or_requirement).get_resource_string(
12091209
self, resource_name
12101210
)
@@ -1480,7 +1480,7 @@ def get_resource_filename(self, manager, resource_name):
14801480
def get_resource_stream(self, manager, resource_name):
14811481
return io.BytesIO(self.get_resource_string(manager, resource_name))
14821482

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

14861486
def has_resource(self, resource_name):
@@ -1650,7 +1650,7 @@ def _validate_resource_path(path):
16501650
DeprecationWarning,
16511651
)
16521652

1653-
def _get(self, path):
1653+
def _get(self, path) -> bytes:
16541654
if hasattr(self.loader, 'get_data'):
16551655
return self.loader.get_data(path)
16561656
raise NotImplementedError(
@@ -1707,7 +1707,7 @@ def _listdir(self, path):
17071707
def get_resource_stream(self, manager, resource_name):
17081708
return open(self._fn(self.module_path, resource_name), 'rb')
17091709

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

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

17331733
_isdir = _has = lambda self, path: False
17341734

1735-
def _get(self, path):
1736-
return ''
1735+
def _get(self, path) -> bytes:
1736+
return b''
17371737

17381738
def _listdir(self, path):
17391739
return []

0 commit comments

Comments
 (0)