Skip to content

Assignment via import #12965

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

Open
serhiy-storchaka opened this issue Jun 10, 2022 · 1 comment
Open

Assignment via import #12965

serhiy-storchaka opened this issue Jun 10, 2022 · 1 comment
Labels
bug mypy got something wrong topic-type-narrowing Conditional type narrowing / binder

Comments

@serhiy-storchaka
Copy link
Member

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

@serhiy-storchaka serhiy-storchaka added the bug mypy got something wrong label Jun 10, 2022
@AlexWaygood AlexWaygood added the topic-type-narrowing Conditional type narrowing / binder label Jun 10, 2022
hauntsaninja added a commit that referenced this issue Nov 3, 2022
This changes our importing logic to be more consistent and to treat
import statements more like assignments.

Fixes #13803, fixes #13914, fixes half of #12965, probably fixes #12574

The primary motivation for this is when typing modules as protocols, as
in #13803. But it turns out we already allowed redefinition with "from"
imports, so this also seems like a nice consistency win.

We move shared logic from visit_import_all and visit_import_from (via
process_imported_symbol) into add_imported_symbol. We then reuse it in
visit_import.

To simplify stuff, we inline the code from add_module_symbol into
visit_import. Then we copy over logic from add_symbol, because MypyFile
is not a SymbolTableNode, but this isn't the worst thing ever.

Finally, we now need to check non-from import statements like
assignments, which was a thing we weren't doing earlier.
@hauntsaninja
Copy link
Collaborator

To clarify the state here, I fixed semantic analysis to not complain about the redefinition + get checker to type check it like an assignment. This gets rid of one of the errors here. However, we still need to fix the binder to do type narrowing here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-type-narrowing Conditional type narrowing / binder
Projects
None yet
Development

No branches or pull requests

3 participants