diff --git a/mypy/nodes.py b/mypy/nodes.py index 4fa3e1b16b413..72a23d4f07894 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 c787b34bf26b2..3b8e668c75468 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: ...