Skip to content

Replace deprected pk_reources with importlib.metadata#94

Open
kalteslicht wants to merge 1 commit intostianaske:masterfrom
kalteslicht:master
Open

Replace deprected pk_reources with importlib.metadata#94
kalteslicht wants to merge 1 commit intostianaske:masterfrom
kalteslicht:master

Conversation

@kalteslicht
Copy link

I use a fork of the homeassistant-vorwerk custom integration in Home Assistant which uses pybotvac as a dependency and noticed this warning in the logs:

/usr/local/lib/python3.13/site-packages/pybotvac/version.py:1: UserWarning: pkg_resources is deprecated as an API. See https://setuptools.pypa.io/en/latest/pkg_resources.html. The pkg_resources package is slated for removal as early as 2025-11-30. Refrain from using this package or pin to Setuptools<81. import pkg_resources

These changes should fix the problem.

Comment on lines +1 to +22
from typing import TYPE_CHECKING

if TYPE_CHECKING:
# During type checking, we assume the standard library interface is available
from importlib.metadata import PackageNotFoundError, version
else:
# At runtime, use stdlib importlib.metadata if available, otherwise fall back
try:
from importlib.metadata import PackageNotFoundError, version
except ImportError:
try:
from importlib_metadata import PackageNotFoundError, version # backport
except ImportError:
# Final fallback: provide minimal stubs
class PackageNotFoundError(Exception):
"""Fallback exception used when metadata backends are unavailable."""
pass

def version(_dist_name: str) -> str:
"""Fallback version() that always raises PackageNotFoundError."""
raise PackageNotFoundError(f"Package '{_dist_name}' not found")

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think the fallback is necessary

Suggested change
from typing import TYPE_CHECKING
if TYPE_CHECKING:
# During type checking, we assume the standard library interface is available
from importlib.metadata import PackageNotFoundError, version
else:
# At runtime, use stdlib importlib.metadata if available, otherwise fall back
try:
from importlib.metadata import PackageNotFoundError, version
except ImportError:
try:
from importlib_metadata import PackageNotFoundError, version # backport
except ImportError:
# Final fallback: provide minimal stubs
class PackageNotFoundError(Exception):
"""Fallback exception used when metadata backends are unavailable."""
pass
def version(_dist_name: str) -> str:
"""Fallback version() that always raises PackageNotFoundError."""
raise PackageNotFoundError(f"Package '{_dist_name}' not found")
from importlib.metadata import PackageNotFoundError, version

@epenet
Copy link
Contributor

epenet commented Feb 9, 2026

@stianaske have you seen this PR?

setuptools 82.0.0 has been released, and pkg_resources is now fully removed

See home-assistant/core#162611
And https://github.com/pypa/setuptools/blob/main/NEWS.rst

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