Skip to content

Model implicit imports of submodules, if it occurs in a parent module __init__.py #133

@sharkdp

Description

@sharkdp

Consider the following case (playground):

main.py

import module

# Missing explicit import of submodule:
# import module.submodule

print(module.submodule.SubmoduleClass)

module/__init__.py

from module.submodule import SubmoduleClass

module/submodule.py

class SubmoduleClass: ...

This works at runtime due to a quirk in module resolution (the from module.submodule import SubmoduleClass import implicitly stores submodule as an attribute on module, thanks @AlexWaygood).

It is not considered good practice to rely on this, but people will probably write code that does so. Neither pyright nor mypy report any errors here. Should we attempt to model this behavior as well?

Metadata

Metadata

Assignees

Labels

importsModule resolution, site-packages discovery, import-related diagnosticsruntime semanticsAccurate modeling of how Python's semantics work at runtime

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions