-
-
Notifications
You must be signed in to change notification settings - Fork 48
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
Support non-package modules. #258
Conversation
jaraco
commented
Jul 22, 2022
- Add test capturing current behavior and missed expectation. Ref Unable to load resources for modules #203.
- Remove restriction that a 'package' cannot be a module. Allows resolution of resources from adjacent modules, even those not found in a package. Fixes Unable to load resources for modules #203.
- Update changelog. Ref Unable to load resources for modules #203.
@warsaw I'd appreciate your review of this change, especially since you were involved so much in the discussion on #60. The behavior change is entirely in the second commit ecffc07. Note how it simplifies the code, aligns with |
See jaraco/openpack@82a5d0b for an illustration of how this change simplifies and harmonizes resource loading. |
_path.build(spec, self.site_dir) | ||
import mod | ||
|
||
actual = resources.files(mod).joinpath('res.txt').read_text() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is surprising to me. I would expect, a pathlib.Path('...', 'mod.py')
to be returned, requiring resources.files(mod).parent.joinpath('res.txt')
. res.txt
is not a resource of mod
, but rather of the parent module, which might not even exist if mod
is top level.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for sharing your surprise. Surely this would need some improved documentation to illustrate how files()
now accepts an Anchor
(instead of Package
), where the anchor indicates:
- the container (importer) from where resources can be discovered,
- if a module is indicated, that's equivalent to indicating the container of that module,
- if a top-level module is indicated, that will anchor on resources reachable at the top level by that same importer.
This is the behavior indicated by pkg_resources:
Note that if a module name is used, then the resource name is relative to the package immediately containing the named module.
However, I've also observed that the pkg_resources implementation also allows for loading resources against unpackaged modules. And while that behavior may have been unintentional, it works because it's a natural consequence of relying on the importer/loader to resolve the resources from the same location as the indicated package/module. That is, because the importlib mechanism supports loading modules from outside packages, so too should resources be loadable from that location.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
res.txt
is not a resource ofmod
, but rather of the parent module, which might not even exist ifmod
is top level.
A parent module may not exist, but a loader for that module does exist and can load resources adjacent to that module:
importlib_resources feature/203-non-package-modules $ touch setup.py
importlib_resources feature/203-non-package-modules $ .tox/python/bin/python -iq
>>> import importlib_resources as res
>>> files = res.files('setup')
>>> files
PosixPath('/Users/jaraco/code/public/importlib_resources')
>>> files.joinpath('pyproject.toml').read_text()[:10]
'[build-sys'
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In 1f3c226, I've updated the documentation to honor an Anchor
type which need not be a package.
…tion of resources from adjacent modules, even those not found in a package. Fixes #203.
1923197
to
1f3c226
Compare
1f3c226
to
8c3edeb
Compare
2ff7b03
to
4e63613
Compare