Skip to content

Commit

Permalink
Added some tests for callables.
Browse files Browse the repository at this point in the history
  • Loading branch information
erictraut committed Dec 27, 2023
1 parent 3f4eb6d commit c797202
Show file tree
Hide file tree
Showing 15 changed files with 613 additions and 8 deletions.
16 changes: 16 additions & 0 deletions conformance/results/mypy/callables_annotation.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
conformant = "Pass"
output = """
callables_annotation.py:13: error: Too few arguments [call-arg]
callables_annotation.py:14: error: Argument 2 has incompatible type "int"; expected "str" [arg-type]
callables_annotation.py:15: error: Too many arguments [call-arg]
callables_annotation.py:16: error: Unexpected keyword argument "a" [call-arg]
callables_annotation.py:16: error: Unexpected keyword argument "b" [call-arg]
callables_annotation.py:22: error: Too many arguments [call-arg]
callables_annotation.py:39: error: Please use "Callable[[<parameters>], <return type>]" or "Callable" [misc]
callables_annotation.py:40: error: The first argument to Callable must be a list of types, parameter specification, or "..." [valid-type]
callables_annotation.py:40: note: See https://mypy.readthedocs.io/en/stable/kinds_of_types.html#callable-types-and-lambdas
callables_annotation.py:41: error: Bracketed expression "[...]" is not valid as a type [valid-type]
callables_annotation.py:41: note: Did you mean "List[...]"?
callables_annotation.py:42: error: Please use "Callable[[<parameters>], <return type>]" or "Callable" [misc]
callables_annotation.py:43: error: Unexpected "..." [misc]
"""
34 changes: 34 additions & 0 deletions conformance/results/mypy/callables_protocol.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
conformant = "Pass"
output = """
callables_protocol.py:35: error: Incompatible types in assignment (expression has type "Callable[[VarArg(bytes), NamedArg(int | None, 'max_items')], list[bytes]]", variable has type "Proto1") [assignment]
callables_protocol.py:35: note: "Proto1.__call__" has type "Callable[[VarArg(bytes), DefaultNamedArg(int | None, 'max_len')], list[bytes]]"
callables_protocol.py:36: error: Incompatible types in assignment (expression has type "Callable[[VarArg(bytes)], list[bytes]]", variable has type "Proto1") [assignment]
callables_protocol.py:36: note: "Proto1.__call__" has type "Callable[[VarArg(bytes), DefaultNamedArg(int | None, 'max_len')], list[bytes]]"
callables_protocol.py:37: error: Incompatible types in assignment (expression has type "Callable[[VarArg(bytes), NamedArg(str | None, 'max_len')], list[bytes]]", variable has type "Proto1") [assignment]
callables_protocol.py:37: note: "Proto1.__call__" has type "Callable[[VarArg(bytes), DefaultNamedArg(int | None, 'max_len')], list[bytes]]"
callables_protocol.py:67: error: Incompatible types in assignment (expression has type "Callable[[VarArg(bytes)], Any]", variable has type "Proto2") [assignment]
callables_protocol.py:67: note: "Proto2.__call__" has type "Callable[[VarArg(bytes), KwArg(str)], None]"
callables_protocol.py:68: error: Incompatible types in assignment (expression has type "Callable[[VarArg(str), KwArg(str)], Any]", variable has type "Proto2") [assignment]
callables_protocol.py:68: note: "Proto2.__call__" has type "Callable[[VarArg(bytes), KwArg(str)], None]"
callables_protocol.py:69: error: Incompatible types in assignment (expression has type "Callable[[VarArg(bytes), KwArg(bytes)], Any]", variable has type "Proto2") [assignment]
callables_protocol.py:69: note: "Proto2.__call__" has type "Callable[[VarArg(bytes), KwArg(str)], None]"
callables_protocol.py:70: error: Incompatible types in assignment (expression has type "Callable[[KwArg(str)], Any]", variable has type "Proto2") [assignment]
callables_protocol.py:70: note: "Proto2.__call__" has type "Callable[[VarArg(bytes), KwArg(str)], None]"
callables_protocol.py:97: error: Incompatible types in assignment (expression has type "Callable[[int], None]", variable has type "Proto4") [assignment]
callables_protocol.py:97: note: "function" is missing following "Proto4" protocol member:
callables_protocol.py:97: note: other_attribute
callables_protocol.py:121: error: Incompatible types in assignment (expression has type "Callable[[VarArg(bytes), DefaultNamedArg(int | None, 'max_len')], list[bytes]]", variable has type "NotProto6") [assignment]
callables_protocol.py:169: error: Incompatible types in assignment (expression has type "Callable[[int], Any]", variable has type "Proto8") [assignment]
callables_protocol.py:169: note: "Proto8.__call__" has type overloaded function
callables_protocol.py:186: error: Incompatible types in assignment (expression has type "str", variable has type "int") [assignment]
callables_protocol.py:187: error: "Proto9[P, R]" has no attribute "xxx" [attr-defined]
callables_protocol.py:197: error: "Proto9[[int], str]" has no attribute "other_attribute2"; maybe "other_attribute"? [attr-defined]
callables_protocol.py:238: error: Incompatible types in assignment (expression has type "Callable[[int, str], Any]", variable has type "Proto11") [assignment]
callables_protocol.py:238: note: "Proto11.__call__" has type "Callable[[int, Arg(str, 'y')], Any]"
callables_protocol.py:260: error: Incompatible types in assignment (expression has type "Callable[[VarArg(Any), NamedArg(Any, 'kwarg0')], None]", variable has type "Proto12") [assignment]
callables_protocol.py:260: note: "Proto12.__call__" has type "Callable[[VarArg(Any), NamedArg(Any, 'kwarg0'), NamedArg(Any, 'kwarg1')], None]"
callables_protocol.py:284: error: Incompatible types in assignment (expression has type "Callable[[str], str]", variable has type "Proto13_Default") [assignment]
callables_protocol.py:284: note: "Proto13_Default.__call__" has type "Callable[[DefaultArg(str, 'path')], str]"
callables_protocol.py:311: error: Incompatible types in assignment (expression has type "Callable[[NamedArg(str, 'path')], str]", variable has type "Proto14_Default") [assignment]
callables_protocol.py:311: note: "Proto14_Default.__call__" has type "Callable[[DefaultNamedArg(str, 'path')], str]"
"""
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.36733102798461914
test_duration = 0.33256983757019043
17 changes: 17 additions & 0 deletions conformance/results/pyre/callables_annotation.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
conformant = "Partial"
notes = """
Does not evaluate correct type for `*args: int` parameter.
Does not reject illegal form `Callable[[...], int]`.
"""
output = """
callables_annotation.py:13:4 Missing argument [20]: PositionalOnly call expects argument in position 1.
callables_annotation.py:14:10 Incompatible parameter type [6]: In anonymous call, for 2nd positional argument, expected `str` but got `int`.
callables_annotation.py:15:4 Too many arguments [19]: PositionalOnly call expects 2 positional arguments, 3 were provided.
callables_annotation.py:16:4 Unexpected keyword [28]: Unexpected keyword argument `a` to anonymous call.
callables_annotation.py:22:4 Too many arguments [19]: PositionalOnly call expects 0 positional arguments, 1 was provided.
callables_annotation.py:35:28 Incompatible parameter type [6]: In call `typing.GenericMeta.__getitem__`, for 1st positional argument, expected `Type[Variable[_T_co](covariant)]` but got `Tuple[Type[int], typing.Any]`.
callables_annotation.py:39:4 Invalid type [31]: Expression `typing.Callable[int]` is not a valid type.
callables_annotation.py:40:4 Invalid type [31]: Expression `typing.Callable[(int, int)]` is not a valid type.
callables_annotation.py:41:4 Invalid type [31]: Expression `typing.Callable[([], [int])]` is not a valid type.
callables_annotation.py:42:4 Invalid type [31]: Expression `typing.Callable[(int, int, int)]` is not a valid type.
"""
26 changes: 26 additions & 0 deletions conformance/results/pyre/callables_protocol.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
conformant = "Partial"
notes = """
Does not correctly handle callback protocol that declares attributes in all functions.
Does not report type incompatibility for callback protocol with positional-only parameters.
Incorrectly reports type compatibility error with callback that has *args and **kwargs.
Does not report type incompatibility for callback missing a default argument for positional parameter.
Does not report type incompatibility for callback missing a default argument for keyword parameter.
"""
output = """
callables_protocol.py:35:0 Incompatible variable type [9]: cb1 is declared to have type `Proto1` but is used as type `typing.Callable(cb1_bad1)[[Variable(bytes), KeywordOnly(max_items, Optional[int])], List[bytes]]`.
callables_protocol.py:36:0 Incompatible variable type [9]: cb1 is declared to have type `Proto1` but is used as type `typing.Callable(cb1_bad2)[[Variable(bytes)], List[bytes]]`.
callables_protocol.py:37:0 Incompatible variable type [9]: cb1 is declared to have type `Proto1` but is used as type `typing.Callable(cb1_bad3)[[Variable(bytes), KeywordOnly(max_len, Optional[str])], List[bytes]]`.
callables_protocol.py:67:0 Incompatible variable type [9]: cb2 is declared to have type `Proto2` but is used as type `typing.Callable(cb2_bad1)[[Variable(bytes)], typing.Any]`.
callables_protocol.py:68:0 Incompatible variable type [9]: cb2 is declared to have type `Proto2` but is used as type `typing.Callable(cb2_bad2)[[Variable(str), Keywords(str)], typing.Any]`.
callables_protocol.py:69:0 Incompatible variable type [9]: cb2 is declared to have type `Proto2` but is used as type `typing.Callable(cb2_bad3)[[Variable(bytes), Keywords(bytes)], typing.Any]`.
callables_protocol.py:70:0 Incompatible variable type [9]: cb2 is declared to have type `Proto2` but is used as type `typing.Callable(cb2_bad4)[[Keywords(str)], typing.Any]`.
callables_protocol.py:97:0 Incompatible variable type [9]: var4 is declared to have type `Proto4` but is used as type `typing.Callable(cb4_bad1)[[Named(x, int)], None]`.
callables_protocol.py:121:0 Incompatible variable type [9]: cb6 is declared to have type `NotProto6` but is used as type `typing.Callable(cb6_bad1)[[Variable(bytes), KeywordOnly(max_len, Optional[int], default)], List[bytes]]`.
callables_protocol.py:169:0 Incompatible variable type [9]: cb8 is declared to have type `Proto8` but is used as type `typing.Callable(cb8_bad1)[[Named(x, int)], typing.Any]`.
callables_protocol.py:186:4 Incompatible attribute type [8]: Attribute `other_attribute` declared in class `Proto9` has type `int` but is used as type `str`.
callables_protocol.py:187:4 Undefined attribute [16]: `Proto9` has no attribute `xxx`.
callables_protocol.py:197:6 Undefined attribute [16]: `Proto9` has no attribute `other_attribute2`.
callables_protocol.py:216:0 Incompatible variable type [9]: cb10 is declared to have type `Proto10` but is used as type `typing.Callable(cb10_good)[[], None]`.
callables_protocol.py:259:0 Incompatible variable type [9]: cb12 is declared to have type `Proto12` but is used as type `typing.Callable(cb12_good2)[[Variable(typing.Any), Keywords(typing.Any)], None]`.
callables_protocol.py:260:0 Incompatible variable type [9]: cb12 is declared to have type `Proto12` but is used as type `typing.Callable(cb12_bad1)[[Variable(typing.Any), KeywordOnly(kwarg0, typing.Any)], None]`.
"""
2 changes: 1 addition & 1 deletion conformance/results/pyre/version.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
version = "pyre 0.9.19"
test_duration = 1.3579108715057373
test_duration = 1.4521031379699707
16 changes: 16 additions & 0 deletions conformance/results/pyright/callables_annotation.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
conformant = "Pass"
output = """
callables_annotation.py:13:5 - error: Expected 1 more positional argument (reportGeneralTypeIssues)
callables_annotation.py:14:11 - error: Argument of type "Literal[2]" cannot be assigned to parameter of type "str"
  "Literal[2]" is incompatible with "str" (reportGeneralTypeIssues)
callables_annotation.py:15:15 - error: Expected 2 positional arguments (reportGeneralTypeIssues)
callables_annotation.py:16:10 - error: Expected 2 more positional arguments (reportGeneralTypeIssues)
callables_annotation.py:22:8 - error: Expected 0 positional arguments (reportGeneralTypeIssues)
callables_annotation.py:39:14 - error: Expected parameter type list or "..."
callables_annotation.py:39:5 - error: Expected return type as second type argument for "Callable" (reportGeneralTypeIssues)
callables_annotation.py:40:14 - error: Expected parameter type list or "..."
callables_annotation.py:41:18 - error: List expression not allowed for this type argument
callables_annotation.py:42:14 - error: Expected parameter type list or "..."
callables_annotation.py:42:24 - error: Expected only two type arguments to "Callable"
callables_annotation.py:43:15 - error: "..." is not allowed in this context
"""
59 changes: 59 additions & 0 deletions conformance/results/pyright/callables_protocol.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
conformant = "Partial"
notes = """
Does not report type incompatibility for callback protocol with positional-only parameters.
"""
output = """
callables_protocol.py:35:7 - error: Expression of type "(*vals: bytes, max_items: int | None) -> list[bytes]" cannot be assigned to declared type "Proto1"
  Type "(*vals: bytes, max_items: int | None) -> list[bytes]" cannot be assigned to type "(*vals: bytes, max_len: int | None = None) -> list[bytes]"
    Keyword parameter "max_items" is missing in destination
    Keyword parameter "max_len" is missing in source (reportGeneralTypeIssues)
callables_protocol.py:36:7 - error: Expression of type "(*vals: bytes) -> list[bytes]" cannot be assigned to declared type "Proto1"
  Type "(*vals: bytes) -> list[bytes]" cannot be assigned to type "(*vals: bytes, max_len: int | None = None) -> list[bytes]"
    Keyword parameter "max_len" is missing in source (reportGeneralTypeIssues)
callables_protocol.py:37:7 - error: Expression of type "(*vals: bytes, max_len: str | None) -> list[bytes]" cannot be assigned to declared type "Proto1"
  Type "(*vals: bytes, max_len: str | None) -> list[bytes]" cannot be assigned to type "(*vals: bytes, max_len: int | None = None) -> list[bytes]"
    Keyword parameter "max_len" of type "int | None" cannot be assigned to type "str | None"
    Parameter "max_len" is missing default argument (reportGeneralTypeIssues)
callables_protocol.py:67:7 - error: Expression of type "(*a: bytes) -> None" cannot be assigned to declared type "Proto2"
  Type "(*a: bytes) -> None" cannot be assigned to type "(*vals: bytes, **kwargs: str) -> None"
    Parameter "**kwargs" has no corresponding parameter (reportGeneralTypeIssues)
callables_protocol.py:68:7 - error: Expression of type "(*a: str, **b: str) -> None" cannot be assigned to declared type "Proto2"
  Type "(*a: str, **b: str) -> None" cannot be assigned to type "(*vals: bytes, **kwargs: str) -> None"
    Parameter 1: type "*tuple[bytes, ...]" cannot be assigned to type "*tuple[str, ...]"
      "*tuple[bytes, ...]" is incompatible with "*tuple[str, ...]"
        Tuple entry 1 is incorrect type
          "bytes" is incompatible with "str" (reportGeneralTypeIssues)
callables_protocol.py:69:7 - error: Expression of type "(*a: bytes, **b: bytes) -> None" cannot be assigned to declared type "Proto2"
  Type "(*a: bytes, **b: bytes) -> None" cannot be assigned to type "(*vals: bytes, **kwargs: str) -> None"
    Parameter 2: type "str" cannot be assigned to type "bytes"
      "str" is incompatible with "bytes" (reportGeneralTypeIssues)
callables_protocol.py:70:7 - error: Expression of type "(**b: str) -> None" cannot be assigned to declared type "Proto2"
  Type "(**b: str) -> None" cannot be assigned to type "(*vals: bytes, **kwargs: str) -> None"
    Parameter "*vals" has no corresponding parameter (reportGeneralTypeIssues)
callables_protocol.py:97:16 - error: Expression of type "(x: int) -> None" cannot be assigned to declared type "Proto4"
  "function" is incompatible with protocol "Proto4"
    "other_attribute" is not present
    "__call__" is not present (reportGeneralTypeIssues)
callables_protocol.py:121:18 - error: Expression of type "(*vals: bytes, max_len: int | None = None) -> list[bytes]" cannot be assigned to declared type "NotProto6"
  "function" is incompatible with "NotProto6" (reportGeneralTypeIssues)
callables_protocol.py:169:7 - error: Expression of type "(x: int) -> Any" cannot be assigned to declared type "Proto8"
  One or more overloads of "__call__" is not assignable
    Type "(x: int) -> Any" cannot be assigned to type "(x: str) -> str"
      Parameter 1: type "str" cannot be assigned to type "int"
        "str" is incompatible with "int" (reportGeneralTypeIssues)
callables_protocol.py:186:33 - error: Cannot assign member "other_attribute" for type "Proto9[P@decorator1, R@decorator1]"
  "Literal['str']" is incompatible with "int" (reportGeneralTypeIssues)
callables_protocol.py:187:15 - error: Cannot assign member "xxx" for type "Proto9[P@decorator1, R@decorator1]"
  Member "xxx" is unknown (reportGeneralTypeIssues)
callables_protocol.py:197:16 - error: Cannot access member "other_attribute2" for type "Proto9[(x: int), str]"
  Member "other_attribute2" is unknown (reportGeneralTypeIssues)
callables_protocol.py:260:8 - error: Expression of type "(*args: Any, kwarg0: Any) -> None" cannot be assigned to declared type "Proto12"
  Type "(*args: Any, kwarg0: Any) -> None" cannot be assigned to type "(*args: Any, kwarg0: Any, kwarg1: Any) -> None"
    Keyword parameter "kwarg1" is missing in source (reportGeneralTypeIssues)
callables_protocol.py:284:27 - error: Expression of type "(path: str) -> str" cannot be assigned to declared type "Proto13_Default"
  Type "(path: str) -> str" cannot be assigned to type "(path: str = ...) -> str"
    Parameter "path" is missing default argument (reportGeneralTypeIssues)
callables_protocol.py:311:27 - error: Expression of type "(*, path: str) -> str" cannot be assigned to declared type "Proto14_Default"
  Type "(*, path: str) -> str" cannot be assigned to type "(*, path: str = ...) -> str"
    Parameter "path" is missing default argument (reportGeneralTypeIssues)
"""
2 changes: 1 addition & 1 deletion conformance/results/pyright/version.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
version = "pyright 1.1.343"
test_duration = 0.872654914855957
test_duration = 0.8812670707702637
32 changes: 32 additions & 0 deletions conformance/results/pytype/callables_annotation.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
conformant = "Pass"
output = """
File "callables_annotation.py", line 13, in func1: Function <callable> expects 2 arg(s), got 1 [wrong-arg-count]
Expected: (_, _)
Actually passed: (_)
File "callables_annotation.py", line 14, in func1: Function <callable> was called with the wrong arguments [wrong-arg-types]
Expected: (_, _1: str)
Actually passed: (_, _1: int)
File "callables_annotation.py", line 15, in func1: Function <callable> expects 2 arg(s), got 3 [wrong-arg-count]
Expected: (_, _)
Actually passed: (_, _, _)
File "callables_annotation.py", line 16, in func1: Invalid keyword arguments (a, b) to function <callable> [wrong-keyword-args]
Expected: (_, _)
Actually passed: (a, b)
File "callables_annotation.py", line 22, in func2: Function <callable> expects 0 arg(s), got 1 [wrong-arg-count]
Expected: ()
Actually passed: (_)
File "callables_annotation.py", line 39, in <module>: Invalid type annotation 'int' [invalid-annotation]
First argument to Callable must be a list of argument types or ellipsis.
File "callables_annotation.py", line 39, in <module>: Invalid type annotation 'Callable[Any]' [invalid-annotation]
Callable[_ARGS, _RET] expected 2 parameters, got 1
File "callables_annotation.py", line 40, in <module>: Invalid type annotation 'int' [invalid-annotation]
First argument to Callable must be a list of argument types or ellipsis.
File "callables_annotation.py", line 41, in <module>: Invalid type annotation '[int]' [invalid-annotation]
Not a type
File "callables_annotation.py", line 42, in <module>: Invalid type annotation 'int' [invalid-annotation]
First argument to Callable must be a list of argument types or ellipsis.
File "callables_annotation.py", line 42, in <module>: Invalid type annotation 'Callable[Any, int, int]' [invalid-annotation]
Callable[_ARGS, _RET] expected 2 parameters, got 3
File "callables_annotation.py", line 43, in <module>: Invalid type annotation 'Ellipsis' [invalid-annotation]
Not allowed at index 0 in list
"""
Loading

0 comments on commit c797202

Please sign in to comment.