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

Prefer importlib.metadata over pkg_resources if available #2081

Merged
merged 12 commits into from
May 22, 2023

Conversation

sentrivana
Copy link
Contributor

Let's not use pkg_resources and avoid a DeprecationWarning if importlib.metadata is available.

Fixes #2048

from importlib.metadata import distributions, version

for dist in distributions():
yield dist.metadata["Name"], version(dist.metadata["Name"])
Copy link
Contributor Author

Choose a reason for hiding this comment

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

There's also dist.name (it's just an alias of dist.metadata["Name"]), but it hasn't always been there. dist.metadata["Name"] should work with all py versions.

@sentrivana sentrivana marked this pull request as ready for review May 5, 2023 12:58
Copy link
Member

@antonpirker antonpirker left a comment

Choose a reason for hiding this comment

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

If distributions() yields the same stuff as pkg_resources.working_set this looks good and can be merged.

@sentrivana
Copy link
Contributor Author

If distributions() yields the same stuff as pkg_resources.working_set this looks good and can be merged.

@antonpirker It does with one difference: distributions() keeps the capitalization, working_set makes everything lowercase, e.g.:

# distributions()
MarkupSafe 2.1.2
matplotlib-inline 0.1.6
mock 5.0.2
packaging 23.1
parso 0.8.3
pexpect 4.8.0
pickleshare 0.7.5
pip 23.1.2
platformdirs 3.5.0
pluggy 0.13.1
prompt-toolkit 3.0.38
ptyprocess 0.7.0
pure-eval 0.2.2
py 1.11.0
Pygments 2.15.1
# working_set
markupsafe 2.1.2
matplotlib-inline 0.1.6
mock 5.0.2
packaging 23.1
parso 0.8.3
pexpect 4.8.0
pickleshare 0.7.5
pip 23.1.2
platformdirs 3.5.0
pluggy 0.13.1
prompt-toolkit 3.0.38
ptyprocess 0.7.0
pure-eval 0.2.2
py 1.11.0
pygments 2.15.1

Do you see this creating issues anywhere? I grepped for where we actually use the modules and it seems like we attach them to the event and check them here but elsewhere we seem to be using sys.modules instead.

@antonpirker
Copy link
Member

@sentrivana could you please add a test for testing _generate_installed_modules?

@sentrivana
Copy link
Contributor Author

@antonpirker Changed the PR a bit and added a test, could you please skim over to see if it still looks ok to you?

@@ -18,15 +18,30 @@
_installed_modules = None


def _normalize_module_name(name):
# type: (str) -> str
return name.lower().replace("-", "_")
Copy link
Member

Choose a reason for hiding this comment

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

Why do we need to replace the "-" with a "_"?
Wouldn't we then report a package name "sentry_sdk" that is different than the one the user installs "sentry-sdk"?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There's a weird mismatch between importlib and pkg_resources where they report the package typing-extensions differently; importlib reports typing_extensions, while pkg_resources reports it as typing-extensions. This is the only package that is different (if we normalize to lowercase, which we're doing). I'll remove the .replace() and change the test to ignore the one package.

Copy link
Member

Choose a reason for hiding this comment

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

I guess if we just try to keep as much the same as it was reported with pkg_resources we are good to go.

@sentrivana sentrivana merged commit 8c24d33 into master May 22, 2023
244 checks passed
@sentrivana sentrivana deleted the ivana/remove-pkg-resources branch May 22, 2023 14:29
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.

pkg_resources is deprecated as an API
3 participants