-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Use protocols instead of importlib.abc.Loader/MetaPathFinder/PathEntryFinder
#11890
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
Changes from 4 commits
c0324ae
ae8ee9d
a25120b
dde456e
a189e29
6cddd52
45b7a5c
7075e5e
eecac02
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# Implicit protocols used in importlib. | ||
# We intentionally omit deprecated and optional methods. | ||
|
||
from collections.abc import Sequence | ||
from importlib.machinery import ModuleSpec | ||
from types import ModuleType | ||
from typing import Protocol | ||
|
||
__all__ = ["LoaderProtocol", "MetaPathFinderProtocol", "PathEntryFinderProtocol"] | ||
|
||
class LoaderProtocol(Protocol): | ||
def load_module(self, fullname: str, /) -> ModuleType: ... | ||
|
||
class MetaPathFinderProtocol(Protocol): | ||
def find_spec(self, fullname: str, path: Sequence[str] | None, target: ModuleType | None = ..., /) -> ModuleSpec | None: ... | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not sure about making it positional only arguments... but I copied the definitions from Maybe it is better just to remove the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Protocols should usually have positional-only parameters, because otherwise implementations need to use the exact same parameter names. Use positional-or-keyword parameters only if consumers of the protocol pass keyword arguments. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, I see, that makes a lot of sense! Thank you very much for the clarification. In accordance to that, I also introduced 6cddd52 to make the arguments in |
||
|
||
class PathEntryFinderProtocol(Protocol): | ||
def find_spec(self, fullname: str, target: ModuleType | None = ...) -> ModuleSpec | None: ... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should be able to replace
types._LoaderProtocol
with this as well.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Replacing that in
types.pyi
would create an import loop betweentypes.pyi
and_typeshed/importlib.pyi
. Is that OK to do for type stubs?I can do the replacement in the
pkg_resources
stub without problems.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Loops are not a problem in stubs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the clarification, I introduced this change in 7075e5e.