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

replace pkg_resources with importlib.metadata #1445

Merged
merged 3 commits into from Oct 18, 2021

Conversation

Panaetius
Copy link
Contributor

Fixes #952

Proposed Changes

  • replaces pkg_resources.iter_entry_points with importlib.metdata.entry_points
  • Conditionally installs importlib_metadata backport depending on python version

One thing to note, the if hasattr(all_entry_points, "select"): check is slightly awkward. In python 3.8 and 3.9, entry_points() returns a plain dict, but in python 3.10 and the backport (on 3.7) it returns a SelectableGroups objects that inherits from dict. Using select() on that object is slightly faster (~55ms) than using it through the dict interface. Since it inherits from dict, isinstance(x, dict) wouldn't work, and since SelectableGroups is not available in the 3.8/3.9 version, isinstance(x, SelectableGroups) wouldn't work either. hence the check via hasattr. This makes it perform equally fast across python versions.

@Panaetius
Copy link
Contributor Author

I'm not sure why it fails on python 3.7 with

error: Module "importlib.metadata" has no attribute "entry_points"  [attr-defined]

on 3.7 it should be

ModuleNotFoundError: No module named 'importlib.metadata'

which is what I get when testing locally.

@nicholascar
Copy link
Member

Hmmm... I'll try and run locally tomorrow

@aucampia
Copy link
Member

I think the best option is to add this to compat and use sys.version_info so mypy can ignore it. I will try make a PR tomorrow morning to fix this.

Don't hesitate pinging me for mypy errors.

This will eliminate these errors:

```
+ mypy --show-error-context --show-error-codes rdflib
rdflib/plugin.py:114: error: Module "importlib.metadata" has no attribute "entry_points"  [attr-defined]
rdflib/plugins/sparql/__init__.py:44: error: Module "importlib.metadata" has no attribute "entry_points"  [attr-defined]
Found 2 errors in 2 files (checked 110 source files)
```
@aucampia
Copy link
Member

@Panaetius I created a PR against your fork to fix this: Panaetius#1

@aucampia
Copy link
Member

Use version conditional imports to accomodate mypy
@Panaetius
Copy link
Contributor Author

I merged your PR, thanks for looking into this!

@nicholascar
Copy link
Member

Thanks to both of you @Panaetius and @aucampia!

@nicholascar nicholascar merged commit d9e86c9 into RDFLib:master Oct 18, 2021
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.

plugin discovery causes very bad startup time
3 participants