You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Should importlib.util.spec_from_loader use a Protocol for the loader parameter ? #11882
After further consideration of the Python docs, we can see that the text references the glossary which describes loader as "an object that loads a module. It must define a method named load_module()" and points to PEP 302 which describes "The Importer Protocol"1.
We can see that in the standard library some perfectly valid loaders do not inherit from this abc:
These are all evidences that the definition in typeshed is incorrect and leads to false positives/negatives during type checking.
I think that in general the best assumption is that importlib.abc abstract classes should not be used for other signatures in typeshed. Instead corresponding protocols should be defined.
The likely reason is that the PEPs related to the importlib where designed in a time when a formal definition of Protocol was not available for Python and abc was used a conceptual replacement... Some of them serve as informational purposes (specially the generic ones like Loader, PathEntryFinder, MetaPathFinder) and others are just handy ways of sharing optional implementation details.
Footnotes
Note that even the word "protocol" is used in the standard... We can conclude that the abstract classes are provided as a documentation device and as means of sharing code among a few implementation of the standard library. ↩
Using a protocol sounds about right to me for the reasons @abravalheri gave, especially due to the glossary definition and the fact that this is such a simple protocol.
Activity
abravalheri commentedon May 10, 2024
Regarding 1:
After further consideration of the Python docs, we can see that the text references the glossary which describes
loaderas "an object that loads a module. It must define a method namedload_module()" and points to PEP 302 which describes "The Importer Protocol"1.We can see that in the standard library some perfectly valid loaders do not inherit from this
abc:We also can see that the type hints that
typeshedprovides for somepkgutilfunctions are incorrect:These are all evidences that the definition in
typeshedis incorrect and leads to false positives/negatives during type checking.I think that in general the best assumption is that
importlib.abcabstract classes should not be used for other signatures intypeshed. Instead corresponding protocols should be defined.The likely reason is that the PEPs related to the
importlibwhere designed in a time when a formal definition ofProtocolwas not available for Python andabcwas used a conceptual replacement... Some of them serve as informational purposes (specially the generic ones likeLoader,PathEntryFinder,MetaPathFinder) and others are just handy ways of sharing optional implementation details.Footnotes
Note that even the word "protocol" is used in the standard... We can conclude that the abstract classes are provided as a documentation device and as means of sharing code among a few implementation of the standard library. ↩
srittau commentedon May 10, 2024
Using a protocol sounds about right to me for the reasons @abravalheri gave, especially due to the glossary definition and the fact that this is such a simple protocol.