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.
Copy file name to clipboardExpand all lines: test-data/unit/check-classes.test
+1-2
Original file line number
Diff line number
Diff line change
@@ -7414,8 +7414,7 @@ class Foo:
7414
7414
def meth1(self, a: str) -> str: ... # E: Name "meth1" already defined on line 5
7415
7415
7416
7416
def meth2(self, a: str) -> str: ...
7417
-
from mod1 import meth2 # E: Unsupported class scoped import \
7418
-
# E: Name "meth2" already defined on line 8
7417
+
from mod1 import meth2 # E: Incompatible import of "meth2" (imported name has type "Callable[[int], int]", local name has type "Callable[[Foo, str], str]")
7419
7418
7420
7419
class Bar:
7421
7420
from mod1 import foo # E: Unsupported class scoped import
0 commit comments