Skip to content

Better types for modules #3500

@ilevkivskyi

Description

@ilevkivskyi

In situations with dynamically/conditionally imported modules:

if random() > 0.5:
    import mod1
else:
    import mod2 as mod1

mypy can infer only types.ModuleType for the mod1 node. Another (quite widespread) example is something like:

try:
    import fastmath
except ImportError:
    import math as fastmath

In such case all symbol table information for math and fastmath is "lost". We could support (at least internally) some special type that will hold symbol table information and will be allowed to appear in unions (or support structural joins). For example, in the above case type of fastmath would be Union[Module['/path/to/fastmath'], Module['math']]. Then mypy will be able to infer fastmath.sin as Callable[[float], float].

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions