-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Should importlib.util.spec_from_loader
use a Protocol
for the loader
parameter ?
#11882
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
Comments
Regarding 1: After further consideration of the Python docs, we can see that the text references the glossary which describes We can see that in the standard library some perfectly valid loaders do not inherit from this >>> from importlib.abc import Loader
>>> import math
>>> math.__loader__
<class '_frozen_importlib.BuiltinImporter'>
>>> isinstance(math.__loader__, Loader)
False
>>> import abc
>>> abc.__loader__
<class '_frozen_importlib.FrozenImporter'>
>>> isinstance(abc.__loader__, Loader)
False
>>> import importlib.util
>>> spec = importlib.util.spec_from_loader('math', math.__loader__)
>>> module = importlib.util.module_from_spec(spec)
>>> spec.loader.exec_module(module)
>>> module.sin(module.pi / 2)
1.0 We also can see that the type hints that >>> import pkgutil
>>> print(loader := pkgutil.get_loader('os'), f"{isinstance(loader, Loader)=}")
<class '_frozen_importlib.FrozenImporter'> isinstance(loader, Loader)=False
>>> print(loader := pkgutil.get_loader('stat'), f"{isinstance(loader, Loader)=}")
<class '_frozen_importlib.FrozenImporter'> isinstance(loader, Loader)=False
>>> print(loader := pkgutil.get_loader('math'), f"{isinstance(loader, Loader)=}")
<class '_frozen_importlib.BuiltinImporter'> isinstance(loader, Loader)=False
>>> print(loader := pkgutil.get_loader('abc'), f"{isinstance(loader, Loader)=}")
<class '_frozen_importlib.FrozenImporter'> isinstance(loader, Loader)=False
>>> print(loader := pkgutil.get_loader('builtins'), f"{isinstance(loader, Loader)=}")
<class '_frozen_importlib.BuiltinImporter'> isinstance(loader, Loader)=False
>>> print(loader := pkgutil.get_loader('site'), f"{isinstance(loader, Loader)=}")
<class '_frozen_importlib.FrozenImporter'> isinstance(loader, Loader)=False
>>> from importlib.abc import PathEntryFinder, MetaPathFinder
>>> print(importer := next(pkgutil.iter_importers('stat')), f"{isinstance(importer, (PathEntryFinder, MetaPathFi
nder))=}")
<class '_frozen_importlib.BuiltinImporter'> isinstance(importer, (PathEntryFinder, MetaPathFinder))=False
>>> print(importer := next(pkgutil.iter_importers('math')), f"{isinstance(importer, (PathEntryFinder, MetaPathFi
nder))=}")
<class '_frozen_importlib.BuiltinImporter'> isinstance(importer, (PathEntryFinder, MetaPathFinder))=False These are all evidences that the definition in I think that in general the best assumption is that The likely reason is that the PEPs related to the Footnotes
|
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. |
Essentially a question asked by @abravalheri at https://github.com/pypa/setuptools/pull/4246/files#r1593000435 .
importlib.util.spec_from_loader
use aProtocol
for theloader
parameter ?pkg_resources.extern.VendorImporter
andsetuptools.extern.VendorImporter
to subclassimportlib.abc.Loader
?type: ignore[arg-type]
the call toimportlib.util.spec_from_loader
?The text was updated successfully, but these errors were encountered: