Skip to content

Do we need to define importlib.abc.PathEntryFinder.find_spec() #103999

Description

@warsaw
No description provided.

Activity

changed the title [-]`importlib.abs.PathEntryFinder` has this comment: "We don't define find_spec() here since that would break hasattr checks we do to support backward compatibility." Do we need to define `find_spec()` in this ABC?[/-] [+]Do we need to define importlib.abc.PathEntryFinder.find_spec()[/+] on Apr 29, 2023

warsaw commented on Apr 29, 2023

@warsaw
MemberAuthor

importlib.abc.PathEntryFinder has this comment: "We don't define find_spec() here since that would break hasattr checks we do to support backward compatibility." Do we need to define find_spec() in this ABC?

added
stdlibStandard Library Python modules in the Lib/ directory
on Nov 28, 2023

tungol commented on Dec 16, 2024

@tungol
Contributor

importlib.abc.MetaPathFinder has the same comment. Just in case someone looks at this issue later and is confused: PathEntryFinder doesn't have the comment anymore, it was removed in #98059, but it was there before that.

One problem: importlib.metadata.DistributionFinder inherits from MetaPathFinder. That's another abstract class, which in turn is a base class for the concrete importlib.metadata.MetadataPathFinder. MetadataPathFinder does not implement find_spec, and either it or DistributionFinder would need to if find_spec is added to MetaPathFinder as an abstract method. The documented failure state for find_spec is just to return None, so maybe that's fine. Or find_spec can be added to MetaPathFinder with a default implementation that just returns None.

All the documentation says these ABCs have these as abstract methods; it'd be good to have that be true. I think probably the right thing for MetaPathFinder is to have the abstract method, and then an implementation on DistributionFinder that just returns None.

One final wrinkle: All the implementations of MetaPathFinder in the standard library have find_spec as a classmethod. We don't document it as a classmethod on MetaPathFinder, and looking at the first page of github search results, I didn't see any third-party users who implemented find_spec as a classmethod. Should it be a classmethod on MetaPathFinder?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directory

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions