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

No easy way to get the distribution which provided a importlib.metadata.EntryPoint #86548

Closed
s0undt3ch mannequin opened this issue Nov 17, 2020 · 8 comments
Closed

No easy way to get the distribution which provided a importlib.metadata.EntryPoint #86548

s0undt3ch mannequin opened this issue Nov 17, 2020 · 8 comments
Assignees
Labels
3.10 only security fixes stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@s0undt3ch
Copy link
Mannequin

s0undt3ch mannequin commented Nov 17, 2020

BPO 42382
Nosy @jaraco, @s0undt3ch
PRs
  • bpo-42382: Make sure each EntryPoint carries it's Distribution information #23334
  • bpo-42382: In importlib.metadata, EntryPoint objects now expose dist #23758
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = 'https://github.com/jaraco'
    closed_at = <Date 2021-03-07.22:43:02.795>
    created_at = <Date 2020-11-17.06:56:21.443>
    labels = ['type-feature', 'library', '3.10']
    title = 'No easy way to get the distribution which provided a importlib.metadata.EntryPoint'
    updated_at = <Date 2021-03-07.22:47:18.618>
    user = 'https://github.com/s0undt3ch'

    bugs.python.org fields:

    activity = <Date 2021-03-07.22:47:18.618>
    actor = 'jaraco'
    assignee = 'jaraco'
    closed = True
    closed_date = <Date 2021-03-07.22:43:02.795>
    closer = 'jaraco'
    components = ['Library (Lib)']
    creation = <Date 2020-11-17.06:56:21.443>
    creator = 's0undt3ch'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 42382
    keywords = ['patch']
    message_count = 8.0
    messages = ['381211', '381235', '381239', '381243', '382605', '382610', '382611', '384126']
    nosy_count = 2.0
    nosy_names = ['jaraco', 's0undt3ch']
    pr_nums = ['23334', '23758']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue42382'
    versions = ['Python 3.10']

    @s0undt3ch
    Copy link
    Mannequin Author

    s0undt3ch mannequin commented Nov 17, 2020

    With pkg_resources an EntryPoint has a dist attribute which allows you to get the distribution that provided that specific entry-point, however, with importlib.metafata and importlib_metadata that's not an east task.

    USE_IMPORTLIB_METADATA_STDLIB = USE_IMPORTLIB_METADATA = False
    try:
        # Py3.8+
        import importlib.metadata
    
        USE_IMPORTLIB_METADATA_STDLIB = True
    except ImportError:
        # < Py3.8 backport package
        import importlib_metadata
    
        USE_IMPORTLIB_METADATA = True
    
    
    def get_distribution_from_entry_point(entry_point):
        loaded_entry_point = entry_point.load()
        if isinstance(loaded_entry_point, types.ModuleType):
            module_path = loaded_entry_point.__file__
        else:
            module_path = sys.modules[loaded_entry_point.__module__].__file__
        if USE_IMPORTLIB_METADATA_STDLIB:
            distributions = importlib.metadata.distributions
        else:
            distributions = importlib_metadata.distributions
    
        for distribution in distributions():
            try:
                relative = pathlib.Path(module_path).relative_to(
                    distribution.locate_file("")
                )
            except ValueError:
                pass
            else:
                if relative in distribution.files:
                    return distribution

    The above solution has the additional drawback that you're iterating the distributions list, once per EntryPoint, which, was already iterated to get the entry-points listing.

    I propose we attach the Distribution instance to each of the found EntryPoint to avoid this low performance workaround.

    I don't have an issue with providing a pull-request, but should that pull request be done against importlib_metadata or importlib.metadata?

    @s0undt3ch s0undt3ch mannequin added stdlib Python modules in the Lib dir type-feature A feature request or enhancement labels Nov 17, 2020
    @jaraco
    Copy link
    Member

    jaraco commented Nov 17, 2020

    Pedro - thanks for the detailed report. Pull requests against importlib_metadata are easier to accept because they can be tested more easily, released more rapidly, and there's a straightforward way to port them to CPython. Regardless, I see you've proposed a change to CPython, so I can work with that.

    @s0undt3ch
    Copy link
    Mannequin Author

    s0undt3ch mannequin commented Nov 17, 2020

    Guess I jumped too fast :)

    Will the changes in CPythom be included in importlib_metadata?

    @jaraco
    Copy link
    Member

    jaraco commented Nov 17, 2020

    Yes - I keep both in sync.

    @jaraco
    Copy link
    Member

    jaraco commented Dec 6, 2020

    I've ported the initial patch over to the backport and am exploring options in python/importlib_metadata#266.

    @jaraco
    Copy link
    Member

    jaraco commented Dec 6, 2020

    In discussion, I realized that I don't yet understand what use-cases drive this demand? What code is it that requires resolving a distribution from an entry point?

    @s0undt3ch
    Copy link
    Mannequin Author

    s0undt3ch mannequin commented Dec 6, 2020

    Our software uses a plug-in based approach.
    Plugins are able to add/modify internal behavior, and, as part of bug submission process we have a CLI flag which provides information about the core app as well as any intervening plugins.

    This is where we need to "map" an entry point to it's distribution, so we know the name and version of it to display on this report.

    @jaraco
    Copy link
    Member

    jaraco commented Dec 31, 2020

    New changeset dfdca85 by Jason R. Coombs in branch 'master':
    bpo-42382: In importlib.metadata, EntryPoint objects now expose dist (bpo-23758)
    dfdca85

    @jaraco jaraco closed this as completed Mar 7, 2021
    @jaraco jaraco closed this as completed Mar 7, 2021
    @jaraco jaraco added 3.10 only security fixes labels Mar 7, 2021
    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.10 only security fixes stdlib Python modules in the Lib dir type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    1 participant