Skip to content

Importlib: Loader issue #2793

@afourmy

Description

@afourmy

When loading a module dynamically with the following code:

from importlib.util import spec_from_file_location, module_from_spec
from pathlib import Path

file = Path.cwd() / etc...
spec = spec_from_file_location(str(file), str(file))
spec.loader.exec_module(module_from_spec(spec))

Mypy fails with the following errors:

test_loader.py:7: error: Item "_Loader" of "Optional[_Loader]" has no attribute "exec_module"
test_loader.py:7: error: Item "None" of "Optional[_Loader]" has no attribute "exec_module"

For the first error, from what I understand, the problem is that _Loader does not have exec_module while Loader does, and ModuleSpec.loader is set to Optional[_Loader].
Removing the underscore here

loader = ... # type: Optional[_Loader]
seems to work, but I'm not sure this is the right way to fix it.
The second error is due to Optional... it appears that None is a valid loader value (when the loader is not found).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions