Skip to content

Migrate package resource/metadata access to stdlib importlib APIs#359

Merged
alexlib merged 2 commits intomasterfrom
copilot/replace-pkg-resources-with-importlib
Mar 10, 2026
Merged

Migrate package resource/metadata access to stdlib importlib APIs#359
alexlib merged 2 commits intomasterfrom
copilot/replace-pkg-resources-with-importlib

Conversation

Copy link
Contributor

Copilot AI commented Mar 10, 2026

This PR replaces legacy/backport package resource and metadata access with stdlib importlib.resources and importlib.metadata. It removes importlib_resources as a runtime packaging dependency and aligns codepaths with supported Python versions.

  • Resource access migration (importlib_resourcesimportlib.resources)

    • Updated Python modules, tutorials, and tests that load bundled data via files(...) to use stdlib imports.
    • No behavioral change intended in path resolution semantics; only import source changed.
  • Package metadata access via stdlib

    • Added openpiv.__version__ sourced from importlib.metadata.version("OpenPIV") in openpiv/__init__.py.
    • Standardizes version lookup on installed distribution metadata rather than setuptools-era patterns.
  • Dependency and packaging cleanup

    • Removed importlib_resources from:
      • pyproject.toml
      • setup.py
      • recipe/meta.yaml
    • Regenerated poetry.lock to reflect dependency graph changes.
  • Focused metadata coverage

    • Added a targeted test asserting openpiv.__version__ matches distribution metadata.
from importlib.resources import files
from importlib.metadata import version

data_dir = files("openpiv.data")
pkg_version = version("OpenPIV")

💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Co-authored-by: alexlib <747110+alexlib@users.noreply.github.com>
Copilot AI changed the title [WIP] Replace pkg_resources with importlib.resources and importlib.metadata Migrate package resource/metadata access to stdlib importlib APIs Mar 10, 2026
@alexlib alexlib marked this pull request as ready for review March 10, 2026 22:45
Copilot AI review requested due to automatic review settings March 10, 2026 22:45
@alexlib alexlib merged commit f26f75c into master Mar 10, 2026
3 checks passed
@alexlib alexlib deleted the copilot/replace-pkg-resources-with-importlib branch March 10, 2026 22:46
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 files with from importlib.resources import files across core modules, tutorials, and tests.
  • Added openpiv.__version__ via importlib.metadata.version("OpenPIV") plus a matching metadata test.
  • Removed importlib_resources from packaging/dependency definitions and updated poetry.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.

Comment on lines +1 to +4
from importlib.metadata import version


__version__ = version("OpenPIV")
Copy link

Copilot AI Mar 10, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

__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.

Suggested change
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"

Copilot uses AI. Check for mistakes.
Comment on lines 27 to 30
- scipy
- natsort
- tqdm
- importlib_resources
- arm_pyart
Copy link

Copilot AI Mar 10, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
'tqdm>=4.66.0',
'importlib_resources>=5.12.0'
'tqdm>=4.66.0'
],
Copy link

Copilot AI Mar 10, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
],
],
python_requires=">=3.10",

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants