gh-123987: Fix NotADirectoryError in NamespaceReader when sentinel present (#124018)

This commit is contained in:
Jason R. Coombs 2025-01-26 11:23:54 -05:00 committed by GitHub
parent fccbfc40b5
commit b543b32eff
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 93 additions and 18 deletions

View file

@ -60,6 +60,26 @@ class OpenZipTests(FilesTests, util.ZipSetup, unittest.TestCase):
class OpenNamespaceTests(FilesTests, util.DiskSetup, unittest.TestCase):
MODULE = 'namespacedata01'
def test_non_paths_in_dunder_path(self):
"""
Non-path items in a namespace package's ``__path__`` are ignored.
As reported in python/importlib_resources#311, some tools
like Setuptools, when creating editable packages, will inject
non-paths into a namespace package's ``__path__``, a
sentinel like
``__editable__.sample_namespace-1.0.finder.__path_hook__``
to cause the ``PathEntryFinder`` to be called when searching
for packages. In that case, resources should still be loadable.
"""
import namespacedata01
namespacedata01.__path__.append(
'__editable__.sample_namespace-1.0.finder.__path_hook__'
)
resources.files(namespacedata01)
class OpenNamespaceZipTests(FilesTests, util.ZipSetup, unittest.TestCase):
ZIP_MODULE = 'namespacedata01'
@ -86,7 +106,7 @@ class ModulesFiles:
"""
A module can have resources found adjacent to the module.
"""
import mod
import mod # type: ignore[import-not-found]
actual = resources.files(mod).joinpath('res.txt').read_text(encoding='utf-8')
assert actual == self.spec['res.txt']