Skip to content

Use importlib.metadata instead of deprecated pkg_resources - #500

Merged
jaraco merged 2 commits into
cherrypy:mainfrom
wylfen:importlib
Jun 22, 2022
Merged

jaraco merged 2 commits into
cherrypy:mainfrom
wylfen:importlib

Conversation

@wylfen

@wylfen wylfen commented Mar 3, 2022 •

Copy link
Copy Markdown

pkg_resources is deprecated upstream and does not handle modern .dist-info directories correctly. Specifically it does not honor the new replacement rules. As a result, it can't find some of cheroot's dependencies on modern Gentoo systems and ultimately fails to load with a DistributionNotFound error.

pkg_resources is only used to get the cheroot version and can easily be replaced with importlib.metadata, the intended replacement for this specific use-case. For python versions before 3.8, attempt to use the importlib_metadata backport instead.

❓ What kind of change does this PR introduce?

  • 🐞 bug fix
  • 🐣 feature
  • 📋 docs update
  • 📋 tests/coverage improvement
  • 📋 refactoring
  • 💥 other

📋 Contribution checklist:

I do not know how to add tests for this specific failure case, so I omitted them.

  • I wrote descriptive pull request text above
  • I think the code is well written
  • I wrote [good commit messages]
  • I have [squashed related commits together][related squash] after
    the changes have been approved
  • Unit tests for the changes exist
  • Integration tests for the changes exist (if applicable)
  • I used the same coding conventions as the rest of the project
  • The new code doesn't generate linter offenses
  • Documentation reflects the changes
  • The PR relates to only one subject with a clear title
    and description in grammatically correct, complete sentences

This change is Reviewable

pkg_resources is deprecated upstream and does not handle modern
.dist-info directories correctly. Specifically it does not honor the new
replacement rules as defined in [1]. As a result, it can't find some of
cheroot's dependencies on modern Gentoo systems and ultimately fails to
load with a DistributionNotFound error [2].

pkg_resources is only used to get the cheroot version and can easily be
replaced with importlib.metadata [3], the intended replacement for this
specific use-case. For python versions before 3.8, attempt to use the
importlib_metadata backport instead.

[1] https://packaging.python.org/en/latest/specifications/binary-distribution-format/#escaping-and-unicode
[2] https://bugs.gentoo.org/834522
[3] https://docs.python.org/3/library/importlib.metadata.html

Thanks-to: Michał Górny <mgorny@gentoo.org>
Signed-off-by: Wolfgang Müller <wolf@oriole.systems>
@wylfen

wylfen commented Mar 3, 2022

Copy link
Copy Markdown
Author

Let me know what to do about the nested try block, flake8 didn't trigger on my local run of pre-commit.

@codecov

codecov Bot commented Mar 3, 2022 •

Copy link
Copy Markdown

Codecov Report

Merging #500 (fa19e29) into main (b2c619f) will decrease coverage by 1.22%.
The diff coverage is 0.00%.

@@            Coverage Diff             @@
##             main     #500      +/-   ##
==========================================
- Coverage   80.56%   79.33%   -1.23%     
==========================================
  Files          28       28              
  Lines        4409     4409              
==========================================
- Hits         3552     3498      -54     
- Misses        857      911      +54     

@jaraco jaraco left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I've started recommending against supplying a __version__ at all and instead directing consumers to query for importlib.metadata.version("cheroot") if they want to know the version. However, for compatibility, it's probably better to go with this approach for now.

@jaraco
jaraco merged commit b7be66d into cherrypy:main Jun 22, 2022
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