Migrate package resource/metadata access to stdlib importlib APIs#359
Migrate package resource/metadata access to stdlib importlib APIs#359
importlib APIs#359Conversation
Co-authored-by: alexlib <747110+alexlib@users.noreply.github.com>
importlib APIs
There was a problem hiding this comment.
Pull request overview
This PR migrates OpenPIV’s package resource and metadata access from the importlib_resources backport to the Python stdlib importlib.resources / importlib.metadata, and removes importlib_resources as a runtime dependency.
Changes:
- Replaced
from importlib_resources import fileswithfrom importlib.resources import filesacross core modules, tutorials, and tests. - Added
openpiv.__version__viaimportlib.metadata.version("OpenPIV")plus a matching metadata test. - Removed
importlib_resourcesfrom packaging/dependency definitions and updatedpoetry.lock.
Reviewed changes
Copilot reviewed 17 out of 18 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| setup.py | Drops importlib_resources from install_requires. |
| recipe/meta.yaml | Drops importlib_resources from conda run requirements. |
| pyproject.toml | Drops importlib_resources from Poetry dependencies. |
| poetry.lock | Regenerated lockfile reflecting dependency removal and Poetry version. |
| openpiv/init.py | Introduces __version__ sourced from distribution metadata. |
| openpiv/piv.py | Switches resource API import to stdlib. |
| openpiv/settings.py | Switches resource API import to stdlib. |
| openpiv/tutorials/tutorial1.py | Switches resource API import to stdlib. |
| openpiv/tutorials/tutorial2.py | Switches resource API import to stdlib. |
| openpiv/tutorials/windef_tutorial.py | Switches resource API import to stdlib. |
| openpiv/tutorials/masking_tutorial.py | Switches resource API import to stdlib. |
| openpiv/test/test_process.py | Switches resource API import to stdlib. |
| openpiv/test/test_piv.py | Switches resource API import to stdlib. |
| openpiv/test/test_validation.py | Switches resource API import to stdlib. |
| openpiv/test/test_windef.py | Switches resource API import to stdlib. |
| openpiv/test/test_windef_coverage.py | Switches resource API import to stdlib. |
| openpiv/test/test_windef_detailed.py | Switches resource API import to stdlib. |
| openpiv/test/test_package_metadata.py | Adds test asserting openpiv.__version__ matches distribution metadata. |
| from importlib.metadata import version | ||
|
|
||
|
|
||
| __version__ = version("OpenPIV") |
There was a problem hiding this comment.
__version__ = version("OpenPIV") runs at import time and will raise importlib.metadata.PackageNotFoundError when OpenPIV is imported from a source checkout (or other contexts where distribution metadata isn’t installed). That would break import openpiv as well as tutorials/tests that import openpiv indirectly. Consider wrapping the metadata lookup in a try/except and providing a reasonable fallback (e.g., a hardcoded dev version or reading from a local version constant) so the package remains importable without installed metadata.
| from importlib.metadata import version | |
| __version__ = version("OpenPIV") | |
| from importlib.metadata import PackageNotFoundError, version | |
| try: | |
| __version__ = version("OpenPIV") | |
| except PackageNotFoundError: | |
| # Fallback when distribution metadata is not available (e.g., source checkout) | |
| __version__ = "0.25.3" |
| - scipy | ||
| - natsort | ||
| - tqdm | ||
| - importlib_resources | ||
| - arm_pyart |
There was a problem hiding this comment.
After removing importlib_resources, the codebase now relies on stdlib importlib.resources.files, which is not available on Python 3.7/3.8. This recipe still advertises python >=3.7 (in both build/run requirements), so conda builds or installs on those versions would fail at runtime. Please bump the recipe’s Python minimum to match the project’s supported versions (>=3.10 per pyproject), or alternatively keep a conditional importlib_resources dependency for older Pythons.
| 'tqdm>=4.66.0', | ||
| 'importlib_resources>=5.12.0' | ||
| 'tqdm>=4.66.0' | ||
| ], |
There was a problem hiding this comment.
With importlib_resources removed from install_requires, installing via setup.py on older Python versions (e.g., 3.7/3.8) will now succeed but fail at import time because stdlib importlib.resources.files isn’t available there. setup.py currently doesn’t set python_requires, so pip won’t be prevented from installing on unsupported versions. Please add an appropriate python_requires constraint (e.g., >=3.10 to match pyproject) or reintroduce a conditional backport dependency for older Pythons.
| ], | |
| ], | |
| python_requires=">=3.10", |
This PR replaces legacy/backport package resource and metadata access with stdlib
importlib.resourcesandimportlib.metadata. It removesimportlib_resourcesas a runtime packaging dependency and aligns codepaths with supported Python versions.Resource access migration (
importlib_resources→importlib.resources)files(...)to use stdlib imports.Package metadata access via stdlib
openpiv.__version__sourced fromimportlib.metadata.version("OpenPIV")inopenpiv/__init__.py.Dependency and packaging cleanup
importlib_resourcesfrom:pyproject.tomlsetup.pyrecipe/meta.yamlpoetry.lockto reflect dependency graph changes.Focused metadata coverage
openpiv.__version__matches distribution metadata.💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.