Skip to content

Assignment via import #12965

Open
Open
@serhiy-storchaka

Description

@serhiy-storchaka

In the following example MyPy complain about local import and does not recognize it as assignment:

from typing import Callable, Optional

def f(callable: Optional[Callable[[float], float]]) -> None:
    if callable is None:
        from math import sqrt as callable  # line 5

    callable(1.5)  # line 7

Output:

t3.py:5: error: Name "callable" already defined on line 3
t3.py:7: error: "None" not callable
Found 2 errors in 1 file (checked 1 source file)

It is not recognized as assignment even for global import:

from typing import Callable, Optional

callable: Optional[Callable[[float], float]] = None
if callable is None:
    from math import sqrt as callable

callable(1.5)  # line 7

Output:

t32.py:7: error: "None" not callable
Found 1 error in 1 file (checked 1 source file)

mypy 0.950 (compiled: yes)
Python 3.9.12

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrongtopic-type-narrowingConditional type narrowing / binder

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions