From 4da416069f18781a3043959267b9b1929609a480 Mon Sep 17 00:00:00 2001 From: Ivan Levkivskyi Date: Mon, 20 Mar 2023 09:39:26 +0000 Subject: [PATCH] Fix crash on class-level import in protocol definition --- mypy/nodes.py | 2 +- test-data/unit/check-protocols.test | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/mypy/nodes.py b/mypy/nodes.py index 4fa3e1b16b41..72a23d4f0789 100644 --- a/mypy/nodes.py +++ b/mypy/nodes.py @@ -3083,7 +3083,7 @@ def protocol_members(self) -> list[str]: for base in self.mro[:-1]: # we skip "object" since everyone implements it if base.is_protocol: for name, node in base.names.items(): - if isinstance(node.node, (TypeAlias, TypeVarExpr)): + if isinstance(node.node, (TypeAlias, TypeVarExpr, MypyFile)): # These are auxiliary definitions (and type aliases are prohibited). continue members.add(name) diff --git a/test-data/unit/check-protocols.test b/test-data/unit/check-protocols.test index c787b34bf26b..3b8e668c7546 100644 --- a/test-data/unit/check-protocols.test +++ b/test-data/unit/check-protocols.test @@ -3998,3 +3998,27 @@ TF = TypeVar("TF", bound=Foo) def outer(cls: Type[TF]) -> TF: reveal_type(test(cls)) # N: Revealed type is "TF`-1" return cls() + +[case testProtocolImportNotMember] +import m +import lib + +class Bad: + x: int +class Good: + x: lib.C + +x: m.P = Bad() # E: Incompatible types in assignment (expression has type "Bad", variable has type "P") \ + # N: Following member(s) of "Bad" have conflicts: \ + # N: x: expected "C", got "int" +x = Good() + +[file m.py] +from typing import Protocol + +class P(Protocol): + import lib + x: lib.C + +[file lib.py] +class C: ...