Skip to content

Prefer importlib.metadata over pkg_resources for version detection#425

Merged
richardkiss merged 10 commits intomainfrom
copilot/update-version-detection
Jan 6, 2026
Merged

Prefer importlib.metadata over pkg_resources for version detection#425
richardkiss merged 10 commits intomainfrom
copilot/update-version-detection

Conversation

Copy link
Contributor

Copilot AI commented Dec 22, 2025

pkg_resources is deprecated and triggers UserWarning on newer setuptools. This updates version detection to use the modern importlib.metadata API first, falling back to pkg_resources for Python < 3.8.

Changes

pycoin/__init__.py:

  • Try importlib.metadata first (Python 3.8+)
  • Fallback to pkg_resources if unavailable
  • Use specific exceptions (PackageNotFoundError, DistributionNotFound) instead of broad Exception catches

tests/version_test.py:

  • Add test coverage for __version__ attribute validation

Before/After

# Before: pkg_resources first (deprecated)
try:
    from pkg_resources import get_distribution
    version = get_distribution(__name__).version
except ImportError:
    from importlib import metadata
    version = metadata.version("pycoin")

# After: importlib.metadata first (modern)
try:
    from importlib.metadata import version as get_version, PackageNotFoundError
    version = get_version("pycoin")
except ImportError:
    from pkg_resources import get_distribution, DistributionNotFound
    version = get_distribution("pycoin").version

Eliminates deprecation warnings while maintaining backward compatibility.

Original prompt

Summary

Update the version detection in pycoin/__init__.py to use importlib.metadata as the primary import path, with pkg_resources as a fallback for backward compatibility.

Context

The current implementation in pycoin/__init__.py prefers pkg_resources when available, even though:

  1. pkg_resources is deprecated and scheduled for removal in setuptools
  2. Using pkg_resources triggers a UserWarning on newer setuptools versions
  3. importlib.metadata is the modern, recommended approach for version detection

Changes Required

Reverse the import order in pycoin/__init__.py to:

  1. Try importlib.metadata first (modern approach)
  2. Fall back to pkg_resources if available (backward compatibility)
  3. Use "unknown" as a final fallback

This ensures forward compatibility and eliminates deprecation warnings while maintaining backward compatibility for environments that haven't updated setuptools yet.

Related Issue

Addresses issue #424: "When will pkg_resources be fully removed in favor of importlib.metadata?"

This pull request was created from Copilot chat.


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

Copilot AI and others added 9 commits December 22, 2025 15:31
Co-authored-by: richardkiss <5336+richardkiss@users.noreply.github.com>
Co-authored-by: richardkiss <5336+richardkiss@users.noreply.github.com>
Co-authored-by: richardkiss <5336+richardkiss@users.noreply.github.com>
Co-authored-by: richardkiss <5336+richardkiss@users.noreply.github.com>
Co-authored-by: richardkiss <5336+richardkiss@users.noreply.github.com>
Co-authored-by: richardkiss <5336+richardkiss@users.noreply.github.com>
Co-authored-by: richardkiss <5336+richardkiss@users.noreply.github.com>
Co-authored-by: richardkiss <5336+richardkiss@users.noreply.github.com>
Co-authored-by: richardkiss <5336+richardkiss@users.noreply.github.com>
Copilot AI changed the title [WIP] Update version detection to use importlib.metadata Prefer importlib.metadata over pkg_resources for version detection Dec 22, 2025
Copilot AI requested a review from richardkiss December 22, 2025 15:50
Copy link
Owner

@richardkiss richardkiss left a comment

Choose a reason for hiding this comment

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

Looks good

@richardkiss richardkiss marked this pull request as ready for review January 6, 2026 01:42
@richardkiss richardkiss merged commit bd5ea9e into main Jan 6, 2026
7 of 9 checks passed
@richardkiss richardkiss deleted the copilot/update-version-detection branch January 6, 2026 01:42
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.

2 participants