Skip to content

Commit

Permalink
Added conformance tests for the "Protocols" section of the type speci…
Browse files Browse the repository at this point in the history
…fication.
  • Loading branch information
erictraut committed Jan 2, 2024
1 parent 2e7ae17 commit 65a4364
Show file tree
Hide file tree
Showing 63 changed files with 2,242 additions and 9 deletions.
23 changes: 23 additions & 0 deletions conformance/results/mypy/protocols_class_objects.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
conformant = "Pass"
output = """
protocols_class_objects.py:29: error: Only concrete class can be given where "type[Proto]" is expected [type-abstract]
protocols_class_objects.py:34: error: Can only assign concrete classes to a variable of type "type[Proto]" [type-abstract]
protocols_class_objects.py:58: error: Incompatible types in assignment (expression has type "type[ConcreteA]", variable has type "ProtoA1") [assignment]
protocols_class_objects.py:58: note: Following member(s) of "ConcreteA" have conflicts:
protocols_class_objects.py:58: note: Expected:
protocols_class_objects.py:58: note: def method1(x: int) -> int
protocols_class_objects.py:58: note: Got:
protocols_class_objects.py:58: note: def method1(self: ConcreteA, x: int) -> int
protocols_class_objects.py:74: error: Incompatible types in assignment (expression has type "type[ConcreteB]", variable has type "ProtoB1") [assignment]
protocols_class_objects.py:74: note: Following member(s) of "ConcreteB" have conflicts:
protocols_class_objects.py:74: note: prop1: expected "int", got "Callable[[ConcreteB], int]"
protocols_class_objects.py:74: note: Only class variables allowed for class object access on protocols, prop1 is an instance variable of "ConcreteB"
protocols_class_objects.py:101: error: Incompatible types in assignment (expression has type "type[ConcreteC1]", variable has type "ProtoC1") [assignment]
protocols_class_objects.py:101: note: ClassVar protocol member ProtoC1.attr1 can never be matched by a class object
protocols_class_objects.py:103: error: Incompatible types in assignment (expression has type "type[ConcreteC2]", variable has type "ProtoC1") [assignment]
protocols_class_objects.py:103: note: Only class variables allowed for class object access on protocols, attr1 is an instance variable of "ConcreteC2"
protocols_class_objects.py:103: note: ClassVar protocol member ProtoC1.attr1 can never be matched by a class object
protocols_class_objects.py:104: error: Incompatible types in assignment (expression has type "type[ConcreteC2]", variable has type "ProtoC2") [assignment]
protocols_class_objects.py:104: note: Only class variables allowed for class object access on protocols, attr1 is an instance variable of "ConcreteC2"
protocols_class_objects.py:105: error: Incompatible types in assignment (expression has type "type[ConcreteC3]", variable has type "ProtoC1") [assignment]
"""
56 changes: 56 additions & 0 deletions conformance/results/mypy/protocols_definition.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
conformant = "Partial"
notes = """
Rejects implicit class variable when matching protocol with explicit ClassVar.
Does not detect protocol mismatch if concrete method is missing annotations.
Does not detect protocol mismatch if concrete method's parameters are position-only.
Does not detect protocol mismatch if concrete method is a classmethod.
"""
output = """
protocols_definition.py:30: error: List item 0 has incompatible type "int"; expected "SupportsClose" [list-item]
protocols_definition.py:62: error: Protocol members cannot be defined via assignment to self [misc]
protocols_definition.py:62: error: "Template" has no attribute "temp" [attr-defined]
protocols_definition.py:109: error: Incompatible types in assignment (expression has type "Concrete2_Good2", variable has type "Template2") [assignment]
protocols_definition.py:109: note: Protocol member Template2.val1 expected class variable, got instance variable
protocols_definition.py:110: error: Incompatible types in assignment (expression has type "Concrete2_Bad1", variable has type "Template2") [assignment]
protocols_definition.py:111: error: Incompatible types in assignment (expression has type "Concrete2_Bad2", variable has type "Template2") [assignment]
protocols_definition.py:111: note: Following member(s) of "Concrete2_Bad2" have conflicts:
protocols_definition.py:111: note: val1: expected "Sequence[int]", got "Sequence[float]"
protocols_definition.py:111: note: Protocol member Template2.val1 expected class variable, got instance variable
protocols_definition.py:112: error: Incompatible types in assignment (expression has type "Concrete2_Bad3", variable has type "Template2") [assignment]
protocols_definition.py:112: note: Following member(s) of "Concrete2_Bad3" have conflicts:
protocols_definition.py:112: note: val1: expected "Sequence[int]", got "list[int]"
protocols_definition.py:112: note: Protocol member Template2.val1 expected class variable, got instance variable
protocols_definition.py:151: error: Incompatible types in assignment (expression has type "Concrete3_Bad1", variable has type "Template3") [assignment]
protocols_definition.py:152: error: Incompatible types in assignment (expression has type "Concrete3_Bad2", variable has type "Template3") [assignment]
protocols_definition.py:152: note: Protocol member Template3.val1 expected instance variable, got class variable
protocols_definition.py:153: error: Incompatible types in assignment (expression has type "Concrete3_Bad3", variable has type "Template3") [assignment]
protocols_definition.py:153: note: Protocol member Template3.val1 expected settable variable, got read-only attribute
protocols_definition.py:154: error: Incompatible types in assignment (expression has type "Concrete3_Bad4", variable has type "Template3") [assignment]
protocols_definition.py:154: note: Following member(s) of "Concrete3_Bad4" have conflicts:
protocols_definition.py:154: note: val1: expected "Sequence[int]", got "Sequence[float]"
protocols_definition.py:155: error: Incompatible types in assignment (expression has type "Concrete3_Bad5", variable has type "Template3") [assignment]
protocols_definition.py:155: note: Following member(s) of "Concrete3_Bad5" have conflicts:
protocols_definition.py:155: note: val1: expected "Sequence[int]", got "list[int]"
protocols_definition.py:213: error: Incompatible types in assignment (expression has type "Concrete4_Bad1", variable has type "Template4") [assignment]
protocols_definition.py:213: note: Following member(s) of "Concrete4_Bad1" have conflicts:
protocols_definition.py:213: note: val1: expected "Sequence[float]", got "Callable[[], Sequence[int]]"
protocols_definition.py:214: error: Incompatible types in assignment (expression has type "Concrete4_Bad2", variable has type "Template4") [assignment]
protocols_definition.py:268: error: Incompatible types in assignment (expression has type "Concrete5_Bad2", variable has type "Template5") [assignment]
protocols_definition.py:268: note: Following member(s) of "Concrete5_Bad2" have conflicts:
protocols_definition.py:268: note: Expected:
protocols_definition.py:268: note: def method1(self, a: int, b: int) -> float
protocols_definition.py:268: note: Got:
protocols_definition.py:268: note: def method1(self, *, a: int, b: int) -> float
protocols_definition.py:271: error: Incompatible types in assignment (expression has type "Concrete5_Bad5", variable has type "Template5") [assignment]
protocols_definition.py:271: note: Following member(s) of "Concrete5_Bad5" have conflicts:
protocols_definition.py:271: note: Expected:
protocols_definition.py:271: note: def method1(self, a: int, b: int) -> float
protocols_definition.py:271: note: Got:
protocols_definition.py:271: note: def method1(self: Any, a: int, b: int) -> float
protocols_definition.py:321: error: Incompatible types in assignment (expression has type "Concrete6_Bad1", variable has type "Template6") [assignment]
protocols_definition.py:321: note: Protocol member Template6.val1 expected settable variable, got read-only attribute
protocols_definition.py:322: error: Incompatible types in assignment (expression has type "Concrete6_Bad2", variable has type "Template6") [assignment]
protocols_definition.py:322: note: Protocol member Template6.val1 expected settable variable, got read-only attribute
protocols_definition.py:323: error: Incompatible types in assignment (expression has type "Concrete6_Bad3", variable has type "Template6") [assignment]
protocols_definition.py:323: note: Protocol member Template6.val1 expected settable variable, got read-only attribute
"""
13 changes: 13 additions & 0 deletions conformance/results/mypy/protocols_explicit.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
conformant = "Pass"
notes = """
Does not report unimplemented attributes for class that explicitly derives from protocol until is instantiated.
"""
output = """
protocols_explicit.py:27: error: Call to abstract method "draw" of "PColor" with trivial body via super() is unsafe [safe-super]
protocols_explicit.py:56: error: Incompatible types in assignment (expression has type "tuple[int, int, str]", base class "RGB" defined the type as "tuple[int, int, int]") [assignment]
protocols_explicit.py:63: error: Cannot instantiate abstract class "Point" with abstract attributes "intensity", "other" and "transparency" [abstract]
protocols_explicit.py:92: error: Cannot instantiate abstract class "Concrete1" with abstract attributes "cm1" and "im1" [abstract]
protocols_explicit.py:114: error: Cannot instantiate abstract class "Concrete3" with abstract attributes "cm10", "cm11" and "im1" [abstract]
protocols_explicit.py:140: error: Cannot instantiate abstract class "Concrete5" with abstract attribute "method1" [abstract]
protocols_explicit.py:171: error: Cannot instantiate abstract class "Concrete7A" with abstract attribute "method1" [abstract]
"""
41 changes: 41 additions & 0 deletions conformance/results/mypy/protocols_generic.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
conformant = "Partial"
notes = """
Fails protocol matching when method-scoped TypeVar is used in protocol.
"""
output = """
protocols_generic.py:40: error: Incompatible types in assignment (expression has type "Concrete1", variable has type "Proto1[int, str]") [assignment]
protocols_generic.py:40: note: Following member(s) of "Concrete1" have conflicts:
protocols_generic.py:40: note: Expected:
protocols_generic.py:40: note: def __iter__(self) -> Iterator[str]
protocols_generic.py:40: note: Got:
protocols_generic.py:40: note: def __iter__(self) -> Iterator[int]
protocols_generic.py:40: note: Expected:
protocols_generic.py:40: note: def method1(self, x: int) -> int
protocols_generic.py:40: note: Got:
protocols_generic.py:40: note: def method1(self, x: str) -> str
protocols_generic.py:44: error: Only single Generic[...] or Protocol[...] can be in bases [misc]
protocols_generic.py:44: error: Duplicate type variables in Generic[...] or Protocol[...] [misc]
protocols_generic.py:56: error: Incompatible types in assignment (expression has type "Box[float]", variable has type "Box[int]") [assignment]
protocols_generic.py:66: error: Incompatible types in assignment (expression has type "Sender[int]", variable has type "Sender[float]") [assignment]
protocols_generic.py:74: error: Incompatible types in assignment (expression has type "AttrProto[int]", variable has type "AttrProto[float]") [assignment]
protocols_generic.py:75: error: Incompatible types in assignment (expression has type "AttrProto[float]", variable has type "AttrProto[int]") [assignment]
protocols_generic.py:145: error: Incompatible types in assignment (expression has type "ConcreteHasProperty2", variable has type "HasPropertyProto") [assignment]
protocols_generic.py:145: note: Following member(s) of "ConcreteHasProperty2" have conflicts:
protocols_generic.py:145: note: Expected:
protocols_generic.py:145: note: def [T] m(self, item: T, callback: Callable[[T], str]) -> str
protocols_generic.py:145: note: Got:
protocols_generic.py:145: note: def m(self, item: int, callback: Callable[[int], str]) -> str
protocols_generic.py:146: error: Incompatible types in assignment (expression has type "ConcreteHasProperty3", variable has type "HasPropertyProto") [assignment]
protocols_generic.py:146: note: Following member(s) of "ConcreteHasProperty3" have conflicts:
protocols_generic.py:146: note: f: expected "ConcreteHasProperty3", got "int"
protocols_generic.py:146: note: Expected:
protocols_generic.py:146: note: def [T] m(self, item: T, callback: Callable[[T], str]) -> str
protocols_generic.py:146: note: Got:
protocols_generic.py:146: note: def m(self, item: int, callback: Callable[[int], str]) -> str
protocols_generic.py:147: error: Incompatible types in assignment (expression has type "ConcreteHasProperty4", variable has type "HasPropertyProto") [assignment]
protocols_generic.py:147: note: Following member(s) of "ConcreteHasProperty4" have conflicts:
protocols_generic.py:147: note: Expected:
protocols_generic.py:147: note: def [T] m(self, item: T, callback: Callable[[T], str]) -> str
protocols_generic.py:147: note: Got:
protocols_generic.py:147: note: def m(self, item: str, callback: Callable[[int], str]) -> str
"""
12 changes: 12 additions & 0 deletions conformance/results/mypy/protocols_merging.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
conformant = "Pass"
output = """
protocols_merging.py:52: error: Incompatible types in assignment (expression has type "SCConcrete2", variable has type "SizedAndClosable1") [assignment]
protocols_merging.py:52: note: "SCConcrete2" is missing following "SizedAndClosable1" protocol member:
protocols_merging.py:52: note: __len__
protocols_merging.py:53: error: Incompatible types in assignment (expression has type "SCConcrete2", variable has type "SizedAndClosable2") [assignment]
protocols_merging.py:53: note: "SCConcrete2" is missing following "SizedAndClosable2" protocol member:
protocols_merging.py:53: note: __len__
protocols_merging.py:54: error: Incompatible types in assignment (expression has type "SCConcrete2", variable has type "SizedAndClosable3") [assignment]
protocols_merging.py:68: error: All bases of a protocol must be protocols [misc]
protocols_merging.py:83: error: Cannot instantiate abstract class "SizedAndClosable4" with abstract attribute "close" [abstract]
"""
13 changes: 13 additions & 0 deletions conformance/results/mypy/protocols_modules.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
conformant = "Pass"
output = """
protocols_modules.py:26: error: Incompatible types in assignment (expression has type Module, variable has type "Options2") [assignment]
protocols_modules.py:26: note: Following member(s) of Module "_protocols_modules1" have conflicts:
protocols_modules.py:26: note: timeout: expected "str", got "int"
protocols_modules.py:48: error: Incompatible types in assignment (expression has type Module, variable has type "Reporter2") [assignment]
protocols_modules.py:48: note: Following member(s) of Module "_protocols_modules2" have conflicts:
protocols_modules.py:48: note: Expected:
protocols_modules.py:48: note: def on_error(x: int) -> int
protocols_modules.py:48: note: Got:
protocols_modules.py:48: note: def on_error(x: int) -> None
protocols_modules.py:49: error: Incompatible types in assignment (expression has type Module, variable has type "Reporter3") [assignment]
"""
3 changes: 3 additions & 0 deletions conformance/results/mypy/protocols_recursive.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
conformant = "Pass"
output = """
"""
9 changes: 9 additions & 0 deletions conformance/results/mypy/protocols_runtime_checkable.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
conformant = "Partial"
notes = """
Does not report unsafe overlap for runtime_checkable protocol.
"""
output = """
protocols_runtime_checkable.py:23: error: Only @runtime_checkable protocols can be used with instance and class checks [misc]
protocols_runtime_checkable.py:55: error: Only protocols that don't have non-method members can be used with issubclass() [misc]
protocols_runtime_checkable.py:55: note: Protocol "DataProtocol" has non-method member(s): name
"""
3 changes: 3 additions & 0 deletions conformance/results/mypy/protocols_self.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
conformant = "Pass"
output = """
"""
12 changes: 12 additions & 0 deletions conformance/results/mypy/protocols_subtyping.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
conformant = "Pass"
output = """
protocols_subtyping.py:16: error: Cannot instantiate protocol class "Proto1" [misc]
protocols_subtyping.py:38: error: Incompatible types in assignment (expression has type "Proto2", variable has type "Concrete2") [assignment]
protocols_subtyping.py:55: error: Incompatible types in assignment (expression has type "Proto2", variable has type "Proto3") [assignment]
protocols_subtyping.py:55: note: "Proto2" is missing following "Proto3" protocol member:
protocols_subtyping.py:55: note: method2
protocols_subtyping.py:79: error: Incompatible types in assignment (expression has type "Proto5[int]", variable has type "Proto4[int, float]") [assignment]
protocols_subtyping.py:80: error: Incompatible types in assignment (expression has type "Proto4[int, int]", variable has type "Proto5[float]") [assignment]
protocols_subtyping.py:102: error: Incompatible types in assignment (expression has type "Proto6[float, float]", variable has type "Proto7[int, float]") [assignment]
protocols_subtyping.py:103: error: Incompatible types in assignment (expression has type "Proto6[float, float]", variable has type "Proto7[float, object]") [assignment]
"""
12 changes: 12 additions & 0 deletions conformance/results/mypy/protocols_variance.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
conformant = "Pass"
output = """
protocols_variance.py:21: error: Invariant type variable "T1" used in protocol where covariant one is expected [misc]
protocols_variance.py:40: error: Invariant type variable "T3" used in protocol where contravariant one is expected [misc]
protocols_variance.py:56: error: Invariant type variable "T1" used in protocol where contravariant one is expected [misc]
protocols_variance.py:61: error: Covariant type variable "T1_co" used in protocol where contravariant one is expected [misc]
protocols_variance.py:62: error: Cannot use a covariant type variable as a parameter [misc]
protocols_variance.py:66: error: Invariant type variable "T1" used in protocol where covariant one is expected [misc]
protocols_variance.py:71: error: Contravariant type variable "T1_contra" used in protocol where covariant one is expected [misc]
protocols_variance.py:72: error: Cannot use a contravariant type variable as return type [misc]
protocols_variance.py:104: error: Invariant type variable "T1" used in protocol where covariant one is expected [misc]
"""
2 changes: 1 addition & 1 deletion conformance/results/mypy/version.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
version = "mypy 1.8.0"
test_duration = 0.5275311470031738
test_duration = 0.3901679515838623
12 changes: 12 additions & 0 deletions conformance/results/pyre/protocols_class_objects.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
conformant = "Partial"
notes = """
Does not reject protocol class assigned to type[Proto].
Incorrectly reports some class objects as incompatible with a protocol.
Fails to report some class objects as incompatible with a protocol.
"""
output = """
protocols_class_objects.py:26:11 Invalid class instantiation [45]: Cannot instantiate abstract class `Proto` with abstract method `meth`.
protocols_class_objects.py:58:0 Incompatible variable type [9]: pa1 is declared to have type `ProtoA1` but is used as type `Type[ConcreteA]`.
protocols_class_objects.py:59:0 Incompatible variable type [9]: pa2 is declared to have type `ProtoA2` but is used as type `Type[ConcreteA]`.
protocols_class_objects.py:93:0 Uninitialized attribute [13]: Attribute `attr1` is declared in class `CMeta` to have type `int` but is never initialized.
"""
Loading

0 comments on commit 65a4364

Please sign in to comment.