You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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.
In the following example MyPy complain about local import and does not recognize it as assignment:
Output:
It is not recognized as assignment even for global import:
Output:
mypy 0.950 (compiled: yes)
Python 3.9.12
The text was updated successfully, but these errors were encountered: