Skip to content

importlib_metadata 3.6 and Python 3.12 compat fix#2

Merged
amotzkau merged 5 commits into
ldmud:masterfrom
cpu:cpu-importlib-3.12-fix
Feb 10, 2025
Merged

importlib_metadata 3.6 and Python 3.12 compat fix#2
amotzkau merged 5 commits into
ldmud:masterfrom
cpu:cpu-importlib-3.12-fix

Conversation

@cpu

@cpu cpu commented Jan 4, 2025

Copy link
Copy Markdown
Contributor

👋 After setting up ldmud-efuns in a fresh venv w/ Python 3.12+ I noticed the following error at startup and no efuns/types being registered:

Traceback (most recent call last):
  File "/home/daniel/Code/C/ldmud/python/startup.py", line 3, in <module>
    startup()
  File "/home/daniel/Code/C/ldmud/.venv/lib/python3.12/site-packages/ldmudefuns/startup.py", line 33, in startup
    for entry_point in eps.get(ep_name,()):
                       ^^^^^^^
AttributeError: 'EntryPoints' object has no attribute 'get'

The root cause is a breaking change described in the importlib.metadata docs:

Changed in version 3.12: The “selectable” entry points were introduced in importlib_metadata 3.6 and Python 3.10. Prior to those changes, entry_points accepted no parameters and always returned a dictionary of entry points, keyed by group. With importlib_metadata 5.0 and Python 3.12, entry_points always returns an EntryPoints object.

This branch works around that change so the package continues to work with Python 3.12+ and importlib_metadata 5.0+.

I couldn't help but tidy up some duplication commit-by-commit before implementing the fix. If you'd prefer a more minimal diff I'd be happy to rework the branch.

cpu added 5 commits February 10, 2025 13:27
Reduce a bit of duplication by lifting out a `get_config()`
helper function to a new `internal` package.
Removes some duplication between `startup.py` and `reload.py` with
respect to processing the config for `ep_types`.
This centralizes the handling of importing the correct `importlib`
metadata, and the logic for finding the entry points for a specific
entrypoint group name.
Quoting the upstream docs:
> Changed in version 3.12: The “selectable” entry points were introduced
> in importlib_metadata 3.6 and Python 3.10. Prior to those changes,
> entry_points accepted no parameters and always returned a dictionary of
> entry points, keyed by group. With importlib_metadata 5.0 and Python
> 3.12, entry_points always returns an EntryPoints object.

Rather than try to gate the updated logic on both the `importlib`
version and the Python version we instead update `get_entry_points()` to
check whether it has a `dict` or not after calling `entry_points()` and
act accordingly.
@cpu cpu force-pushed the cpu-importlib-3.12-fix branch from a3cfbfb to f117bf9 Compare February 10, 2025 18:34
@cpu

cpu commented Feb 10, 2025

Copy link
Copy Markdown
Contributor Author

Reworked to use internal.py instead of __init__.py, and to avoid _ prefixing the helper names based on OOB feedback.

@amotzkau amotzkau merged commit 4bb1f15 into ldmud:master Feb 10, 2025
@cpu cpu deleted the cpu-importlib-3.12-fix branch February 10, 2025 18:54
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