Skip to content

GH-63062: Implement __subclasshook__() for Finders and Loaders in abc lib #102763

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

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Lib/importlib/_abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ def create_module(self, spec):
# We don't define exec_module() here since that would break
# hasattr checks we do to support backward compatibility.

@classmethod
def __subclasshook__(cls, C):
if cls is Loader:
if (any('exec_module' in B.__dict__ for B in C.__mro__) or
any('load_module' in B.__dict__ for B in C.__mro__)):
return True
return NotImplemented

def load_module(self, fullname):
"""Return the loaded module.

Expand Down
25 changes: 23 additions & 2 deletions Lib/importlib/abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,14 @@ class ResourceLoader(Loader):

"""

@classmethod
def __subclasshook__(cls, C):
if cls is ResourceLoader:
if (Loader.__subclasshook__(C) and
any('get_data' in B.__dict__ for B in C.__mro__)):
return True
return NotImplemented

@abc.abstractmethod
def get_data(self, path):
"""Abstract method which when implemented should return the bytes for
Expand All @@ -102,11 +110,24 @@ class InspectLoader(Loader):

"""

@classmethod
def __subclasshook__(cls, C):
if cls is InspectLoader:
if (Loader.__subclasshook__(C) and
any('is_package' in B.__dict__ for B in C.__mro__) and
any('get_code' in B.__dict__ for B in C.__mro__) and
any('get_source' in B.__dict__ for B in C.__mro__) and
any('source_to_code' in B.__dict__ for B in C.__mro__)):
return True
return NotImplemented

def is_package(self, fullname):
"""Optional method which when implemented should return whether the
module is a package. The fullname is a str. Returns a bool.
"""(abstract) Return whether the module is a package.

The fullname is a str.

Raises ImportError if the module cannot be found.

"""
raise ImportError

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__subclasshook__() implemented for the Finders and Loaders In the abc lib. Patched by Furkan Onder and Eric Snow.