Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove uses of pkg_resources and distutils #1096

Merged
merged 3 commits into from May 13, 2024

Conversation

diazona
Copy link
Contributor

@diazona diazona commented May 6, 2024

This PR replaces uses of deprecated APIs from pkg_resources and distutils with modern equivalents from importlib.*, packaging, and the standard library's shutil. The one exception is that I added a new custom reimplementation of pkg_resources.require() because there's no equivalent to that in importlib.* or packaging (or the standard library).

I wasn't sure if there's any tests that would make sense to add here, since this PR isn't changing any functionality, and the only way I know of to reproduce the corresponding bug (#1088) seems to be installing pyinfra itself using pipx in Python 3.12 (and even then, it depends on some other environmental factors I haven't figured out yet), so it would be difficult to come up with a test that exercises that. Happy to take any suggestions if there's a test that would make sense to add here.

I note that the GitHub Actions workflow currently doesn't test pyinfra on Python 3.12, which would probably be good to add, but I'd consider that out of scope for this PR.

This should resolve #1088.

@diazona diazona force-pushed the remove-uses-of-pkg-resources/1/dev branch from 00fe3d5 to 573c3e8 Compare May 12, 2024 02:16
@diazona
Copy link
Contributor Author

diazona commented May 12, 2024

whoops, I missed that the CI jobs failed the first time. Now updated.

@diazona diazona force-pushed the remove-uses-of-pkg-resources/1/dev branch 2 times, most recently from 39ac1f9 to 14a548e Compare May 12, 2024 08:59
Copy link
Member

@Fizzadar Fizzadar left a comment

Choose a reason for hiding this comment

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

Thank you for working on this @diazona! All looks good just the one import fix.

pyinfra/api/config.py Outdated Show resolved Hide resolved
pkg_resources is deprecated and, under some circumstances, causes import
errors in Python 3.12. This commit replaces the deprecated pkg_resources
functions that access distribution metadata with calls to corresponding
functions from importlib.metadata (or its backport importlib_metadata)
and packaging. This change will bring pyinfra more in line with modern
recommendations and should resolve the errors.

The change introduces a new dependency on importlib_metadata in Python
versions prior to 3.10. Even though importlib.metadata was available in
Python 3.8, its entry_points() function did not have the group keyword
argument until 3.10, hence this commit prefers the backport until that
Python version.

The only remaining use of pkg_resources is the require() function, which
will take more work to replace since there's no ready equivalent in any
of the importlib* or packaging modules.
@diazona diazona force-pushed the remove-uses-of-pkg-resources/1/dev branch from 38d6d22 to 56e42be Compare May 12, 2024 21:20
@diazona diazona requested a review from Fizzadar May 12, 2024 21:23
pkg_resources is deprecated and, under some circumstances, causes import
errors in Python 3.12. This commit replaces the one remaining function
from pkg_resources, the require() function, with a new implementation
that does the same thing. It was necessary to implement the function
anew because there's no equivalent for require() in the packaging or
importlib.* modules. There is an implementation in the hbutils package,
but I didn't think it was worth bringing in a new dependency for just
this one function.
  https://hansbug.github.io/hbutils/main/api_doc/system/python.html#check-reqs
See also
  pypa/packaging-problems#664
This new implementation is based most closely on the original
pkg_resources code.
This commit replaces distutils.spawn.find_executable() with
shutil.which() from the standard library. distutils is deprecated and
has been removed from Python 3.12+, causing import errors when pyinfra
is run on that version.
@diazona diazona force-pushed the remove-uses-of-pkg-resources/1/dev branch from 56e42be to 3985b7e Compare May 13, 2024 05:39
Copy link
Member

@Fizzadar Fizzadar left a comment

Choose a reason for hiding this comment

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

Thank you once again @diazona, this is fantastic!

@Fizzadar Fizzadar merged commit 80aca6e into pyinfra-dev:3.x May 13, 2024
24 checks passed
@diazona diazona deleted the remove-uses-of-pkg-resources/1/dev branch May 14, 2024 08:20
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.

Missing distutils with pipx on 3.12
2 participants