From ab63a422db84e7836f61c6eb1a0dadba4a9082ea Mon Sep 17 00:00:00 2001 From: theodoreando Date: Mon, 27 Oct 2025 10:52:31 -0500 Subject: [PATCH 1/4] use pretty_callable in format_type_inner and fix unit-tests for def-style Callables. --- mypy/messages.py | 16 ++++++++ test-data/unit/check-assert-type-fail.test | 2 +- test-data/unit/check-callable.test | 6 +-- test-data/unit/check-functions.test | 38 +++++++++---------- test-data/unit/check-functools.test | 16 ++++---- test-data/unit/check-incremental.test | 2 +- test-data/unit/check-inference.test | 14 +++---- .../unit/check-parameter-specification.test | 8 ++-- test-data/unit/check-protocols.test | 34 ++++++++--------- test-data/unit/check-python311.test | 2 +- test-data/unit/check-statements.test | 2 +- test-data/unit/check-typevar-tuple.test | 26 ++++++------- test-data/unit/check-varargs.test | 2 +- test-data/unit/pythoneval.test | 2 +- 14 files changed, 93 insertions(+), 77 deletions(-) diff --git a/mypy/messages.py b/mypy/messages.py index c6378c264757..094b51c91398 100644 --- a/mypy/messages.py +++ b/mypy/messages.py @@ -2558,13 +2558,17 @@ def format_type_inner( options: Options, fullnames: set[str] | None, module_names: bool = False, + use_pretty_callable: bool = True, ) -> str: """ Convert a type to a relatively short string suitable for error messages. Args: + typ: type to be formatted verbosity: a coarse grained control on the verbosity of the type fullnames: a set of names that should be printed in full + use_pretty_callable: use MessageBuilder.pretty_callable to format Callable + types. """ def format(typ: Type) -> str: @@ -2761,6 +2765,18 @@ def format_literal_value(typ: LiteralType) -> str: param_spec = func.param_spec() if param_spec is not None: return f"Callable[{format(param_spec)}, {return_type}]" + + # Use pretty format (def-style) for complex signatures with named, optional, or star args. + # Use compact Callable[[...], ...] only for signatures with all simple positional args. + has_complex_args = any( + not ( + (kind == ARG_POS and name is None) or (verbosity == 0 and kind.is_positional()) + ) + for name, kind in zip(func.arg_names, func.arg_kinds) + ) + if use_pretty_callable and has_complex_args: + return pretty_callable(func, options) + args = format_callable_args( func.arg_types, func.arg_kinds, func.arg_names, format, verbosity ) diff --git a/test-data/unit/check-assert-type-fail.test b/test-data/unit/check-assert-type-fail.test index 514650649641..98aae0ba6b32 100644 --- a/test-data/unit/check-assert-type-fail.test +++ b/test-data/unit/check-assert-type-fail.test @@ -30,7 +30,7 @@ def f(si: arr.array[int]): [case testAssertTypeFailCallableArgKind] from typing import assert_type, Callable def myfunc(arg: int) -> None: pass -assert_type(myfunc, Callable[[int], None]) # E: Expression is of type "Callable[[Arg(int, 'arg')], None]", not "Callable[[int], None]" +assert_type(myfunc, Callable[[int], None]) # E: Expression is of type "def myfunc(arg: int) -> None", not "Callable[[int], None]" [case testAssertTypeOverload] from typing import assert_type, overload diff --git a/test-data/unit/check-callable.test b/test-data/unit/check-callable.test index 23db0bf50a4e..0157ff3d2c53 100644 --- a/test-data/unit/check-callable.test +++ b/test-data/unit/check-callable.test @@ -654,13 +654,13 @@ class Call(Protocol): def f1() -> None: ... a1: Call = f1 # E: Incompatible types in assignment (expression has type "Callable[[], None]", variable has type "Call") \ - # N: "Call.__call__" has type "Callable[[Arg(int, 'x'), VarArg(Any), KwArg(Any)], None]" + # N: "Call.__call__" has type "def __call__(self, x: int, *args: Any, **kwargs: Any) -> None" def f2(x: str) -> None: ... a2: Call = f2 # E: Incompatible types in assignment (expression has type "Callable[[str], None]", variable has type "Call") \ - # N: "Call.__call__" has type "Callable[[Arg(int, 'x'), VarArg(Any), KwArg(Any)], None]" + # N: "Call.__call__" has type "def __call__(self, x: int, *args: Any, **kwargs: Any) -> None" def f3(y: int) -> None: ... a3: Call = f3 # E: Incompatible types in assignment (expression has type "Callable[[int], None]", variable has type "Call") \ - # N: "Call.__call__" has type "Callable[[Arg(int, 'x'), VarArg(Any), KwArg(Any)], None]" + # N: "Call.__call__" has type "def __call__(self, x: int, *args: Any, **kwargs: Any) -> None" def f4(x: int) -> None: ... a4: Call = f4 diff --git a/test-data/unit/check-functions.test b/test-data/unit/check-functions.test index 7fa34a398ea0..1882f235f7e3 100644 --- a/test-data/unit/check-functions.test +++ b/test-data/unit/check-functions.test @@ -107,30 +107,30 @@ if int(): [case testSubtypingFunctionsDoubleCorrespondence] def l(x) -> None: ... def r(__x, *, x) -> None: ... -r = l # E: Incompatible types in assignment (expression has type "Callable[[Any], None]", variable has type "Callable[[Any, NamedArg(Any, 'x')], None]") +r = l # E: Incompatible types in assignment (expression has type "Callable[[Any], None]", variable has type "def r(Any, /, *, x: Any) -> None") [case testSubtypingFunctionsDoubleCorrespondenceNamedOptional] def l(x) -> None: ... def r(__x, *, x = 1) -> None: ... -r = l # E: Incompatible types in assignment (expression has type "Callable[[Any], None]", variable has type "Callable[[Any, DefaultNamedArg(Any, 'x')], None]") +r = l # E: Incompatible types in assignment (expression has type "Callable[[Any], None]", variable has type "def r(Any, /, *, x: Any = ...) -> None") [case testSubtypingFunctionsDoubleCorrespondenceBothNamedOptional] def l(x = 1) -> None: ... def r(__x, *, x = 1) -> None: ... -r = l # E: Incompatible types in assignment (expression has type "Callable[[Any], None]", variable has type "Callable[[Any, DefaultNamedArg(Any, 'x')], None]") +r = l # E: Incompatible types in assignment (expression has type "Callable[[Any], None]", variable has type "def r(Any, /, *, x: Any = ...) -> None") [case testSubtypingFunctionsTrivialSuffixRequired] def l(__x) -> None: ... def r(x, *args, **kwargs) -> None: ... -r = l # E: Incompatible types in assignment (expression has type "Callable[[Any], None]", variable has type "Callable[[Arg(Any, 'x'), VarArg(Any), KwArg(Any)], None]") +r = l # E: Incompatible types in assignment (expression has type "Callable[[Any], None]", variable has type "def r(x: Any, *args: Any, **kwargs: Any) -> None") [builtins fixtures/dict.pyi] [case testSubtypingFunctionsTrivialSuffixOptional] def l(__x = 1) -> None: ... def r(x = 1, *args, **kwargs) -> None: ... -r = l # E: Incompatible types in assignment (expression has type "Callable[[DefaultArg(Any)], None]", variable has type "Callable[[DefaultArg(Any, 'x'), VarArg(Any), KwArg(Any)], None]") +r = l # E: Incompatible types in assignment (expression has type "def l(Any = ..., /) -> None", variable has type "def r(x: Any = ..., *args: Any, **kwargs: Any) -> None") [builtins fixtures/dict.pyi] [case testSubtypingFunctionsRequiredLeftArgNotPresent] @@ -170,13 +170,13 @@ if int(): if int(): ff_nonames = f_nonames # reset if int(): - ff = ff_nonames # E: Incompatible types in assignment (expression has type "Callable[[int, str], None]", variable has type "Callable[[Arg(int, 'a'), Arg(str, 'b')], None]") + ff = ff_nonames # E: Incompatible types in assignment (expression has type "Callable[[int, str], None]", variable has type "def f(a: int, b: str) -> None") if int(): ff = f # reset if int(): - gg = ff # E: Incompatible types in assignment (expression has type "Callable[[Arg(int, 'a'), Arg(str, 'b')], None]", variable has type "Callable[[Arg(int, 'a'), DefaultArg(str, 'b')], None]") + gg = ff # E: Incompatible types in assignment (expression has type "def f(a: int, b: str) -> None", variable has type "def g(a: int, b: str = ...) -> None") if int(): - gg = hh # E: Incompatible types in assignment (expression has type "Callable[[Arg(int, 'aa'), DefaultArg(str, 'b')], None]", variable has type "Callable[[Arg(int, 'a'), DefaultArg(str, 'b')], None]") + gg = hh # E: Incompatible types in assignment (expression has type "def h(aa: int, b: str = ...) -> None", variable has type "def g(a: int, b: str = ...) -> None") [case testSubtypingFunctionsArgsKwargs] from typing import Any, Callable @@ -245,7 +245,7 @@ gg = g if int(): ff = g if int(): - gg = f # E: Incompatible types in assignment (expression has type "Callable[[int, str], None]", variable has type "Callable[[Arg(int, 'a'), Arg(str, 'b')], None]") + gg = f # E: Incompatible types in assignment (expression has type "Callable[[int, str], None]", variable has type "def g(a: int, b: str) -> None") [case testLackOfNamesFastparse] def f(__a: int, __b: str) -> None: pass @@ -257,7 +257,7 @@ gg = g if int(): ff = g if int(): - gg = f # E: Incompatible types in assignment (expression has type "Callable[[int, str], None]", variable has type "Callable[[Arg(int, 'a'), Arg(str, 'b')], None]") + gg = f # E: Incompatible types in assignment (expression has type "Callable[[int, str], None]", variable has type "def g(a: int, b: str) -> None") [case testFunctionTypeCompatibilityWithOtherTypes] # flags: --no-strict-optional @@ -2016,12 +2016,12 @@ def isf_unnamed(__i: int, __s: str) -> str: int_str_fun = isf int_str_fun = isf_unnamed -int_named_str_fun = isf_unnamed # E: Incompatible types in assignment (expression has type "Callable[[int, str], str]", variable has type "Callable[[int, Arg(str, 's')], str]") +int_named_str_fun = isf_unnamed # E: Incompatible types in assignment (expression has type "Callable[[int, str], str]", variable has type "def (int, /, s: str) -> str") int_opt_str_fun = iosf int_str_fun = iosf -int_opt_str_fun = isf # E: Incompatible types in assignment (expression has type "Callable[[Arg(int, 'ii'), Arg(str, 'ss')], str]", variable has type "Callable[[int, DefaultArg(str)], str]") +int_opt_str_fun = isf # E: Incompatible types in assignment (expression has type "def isf(ii: int, ss: str) -> str", variable has type "def (int, str = ..., /) -> str") -int_named_str_fun = isf # E: Incompatible types in assignment (expression has type "Callable[[Arg(int, 'ii'), Arg(str, 'ss')], str]", variable has type "Callable[[int, Arg(str, 's')], str]") +int_named_str_fun = isf # E: Incompatible types in assignment (expression has type "def isf(ii: int, ss: str) -> str", variable has type "def (int, /, s: str) -> str") int_named_str_fun = iosf [builtins fixtures/dict.pyi] @@ -2076,7 +2076,7 @@ def g4(*, y: int) -> str: pass f(g1) f(g2) f(g3) -f(g4) # E: Argument 1 to "f" has incompatible type "Callable[[NamedArg(int, 'y')], str]"; expected "Callable[..., int]" +f(g4) # E: Argument 1 to "f" has incompatible type "def g4(*, y: int) -> str"; expected "Callable[..., int]" [case testCallableWithArbitraryArgsSubtypingWithGenericFunc] from typing import Callable, TypeVar @@ -2238,7 +2238,7 @@ def g(x, y): pass def h(x): pass def j(y) -> Any: pass f = h -f = j # E: Incompatible types in assignment (expression has type "Callable[[Arg(Any, 'y')], Any]", variable has type "Callable[[Arg(Any, 'x')], Any]") +f = j # E: Incompatible types in assignment (expression has type "def j(y: Any) -> Any", variable has type "def f(x: Any) -> Any") f = g # E: Incompatible types in assignment (expression has type "Callable[[Any, Any], Any]", variable has type "Callable[[Any], Any]") [case testRedefineFunction2] @@ -3531,7 +3531,7 @@ def decorator(f: Callable[P, None]) -> Callable[[Callable[P, A]], None]: def key(x: int) -> None: ... def fn_b(b: int) -> B: ... -decorator(key)(fn_b) # E: Argument 1 has incompatible type "Callable[[Arg(int, 'b')], B]"; expected "Callable[[Arg(int, 'x')], A]" +decorator(key)(fn_b) # E: Argument 1 has incompatible type "def fn_b(b: int) -> B"; expected "def (x: int) -> A" def decorator2(f: Callable[P, None]) -> Callable[ [Callable[P, Awaitable[None]]], @@ -3542,7 +3542,7 @@ def decorator2(f: Callable[P, None]) -> Callable[ def key2(x: int) -> None: ... -@decorator2(key2) # E: Argument 1 has incompatible type "Callable[[Arg(int, 'y')], Coroutine[Any, Any, None]]"; expected "Callable[[Arg(int, 'x')], Awaitable[None]]" +@decorator2(key2) # E: Argument 1 has incompatible type "def foo2(y: int) -> Coroutine[Any, Any, None]"; expected "def (x: int) -> Awaitable[None]" async def foo2(y: int) -> None: ... @@ -3552,7 +3552,7 @@ class Parent: class Child(Parent): method_without: Callable[[], "Child"] - method_with: Callable[[str], "Child"] # E: Incompatible types in assignment (expression has type "Callable[[str], Child]", base class "Parent" defined the type as "Callable[[Arg(str, 'param')], Parent]") + method_with: Callable[[str], "Child"] # E: Incompatible types in assignment (expression has type "Callable[[str], Child]", base class "Parent" defined the type as "def method_with(self, param: str) -> Parent") [builtins fixtures/tuple.pyi] [case testDistinctFormattingUnion] @@ -3562,7 +3562,7 @@ from mypy_extensions import Arg def f(x: Callable[[Arg(int, 'x')], None]) -> None: pass y: Callable[[Union[int, str]], None] -f(y) # E: Argument 1 to "f" has incompatible type "Callable[[Union[int, str]], None]"; expected "Callable[[Arg(int, 'x')], None]" +f(y) # E: Argument 1 to "f" has incompatible type "Callable[[Union[int, str]], None]"; expected "def (x: int) -> None" [builtins fixtures/tuple.pyi] [case testAbstractOverloadsWithoutImplementationAllowed] diff --git a/test-data/unit/check-functools.test b/test-data/unit/check-functools.test index fa2cacda275d..650928b1a5ed 100644 --- a/test-data/unit/check-functools.test +++ b/test-data/unit/check-functools.test @@ -162,7 +162,7 @@ def takes_callable_int(f: Callable[..., int]) -> None: ... def takes_callable_str(f: Callable[..., str]) -> None: ... takes_callable_int(p1) takes_callable_str(p1) # E: Argument 1 to "takes_callable_str" has incompatible type "partial[int]"; expected "Callable[..., str]" \ - # N: "partial[int].__call__" has type "Callable[[VarArg(Any), KwArg(Any)], int]" + # N: "partial[int].__call__" has type "def __call__(__self, *args: Any, **kwargs: Any) -> int" p2 = functools.partial(foo, 1) p2("a") # OK @@ -386,7 +386,7 @@ q: partial[bool] = partial(generic, resulting_type=str) # E: Argument "resultin pc: Callable[..., str] = partial(generic, resulting_type=str) qc: Callable[..., bool] = partial(generic, resulting_type=str) # E: Incompatible types in assignment (expression has type "partial[str]", variable has type "Callable[..., bool]") \ - # N: "partial[str].__call__" has type "Callable[[VarArg(Any), KwArg(Any)], str]" + # N: "partial[str].__call__" has type "def __call__(__self, *args: Any, **kwargs: Any) -> str" [builtins fixtures/tuple.pyi] [case testFunctoolsPartialNestedPartial] @@ -697,11 +697,11 @@ use_int_callable(partial(func_b, b="")) use_func_callable(partial(func_b, b="")) use_int_callable(partial(func_c, b="")) use_func_callable(partial(func_c, b="")) -use_int_callable(partial(func_fn, b="")) # E: Argument 1 to "use_int_callable" has incompatible type "partial[Callable[[VarArg(Any), KwArg(Any)], Any]]"; expected "Callable[[int], int]" \ - # N: "partial[Callable[[VarArg(Any), KwArg(Any)], Any]].__call__" has type "Callable[[VarArg(Any), KwArg(Any)], Callable[[VarArg(Any), KwArg(Any)], Any]]" +use_int_callable(partial(func_fn, b="")) # E: Argument 1 to "use_int_callable" has incompatible type "partial[def (*Any, **Any) -> Any]"; expected "Callable[[int], int]" \ + # N: "partial[def (*Any, **Any) -> Any].__call__" has type "def __call__(__self, *args: Any, **kwargs: Any) -> def (*Any, **Any) -> Any" use_func_callable(partial(func_fn, b="")) -use_int_callable(partial(func_fn_unpack, b="")) # E: Argument 1 to "use_int_callable" has incompatible type "partial[Callable[[VarArg(Any)], Any]]"; expected "Callable[[int], int]" \ - # N: "partial[Callable[[VarArg(Any)], Any]].__call__" has type "Callable[[VarArg(Any), KwArg(Any)], Callable[[VarArg(Any)], Any]]" +use_int_callable(partial(func_fn_unpack, b="")) # E: Argument 1 to "use_int_callable" has incompatible type "partial[def (*Any) -> Any]"; expected "Callable[[int], int]" \ + # N: "partial[def (*Any) -> Any].__call__" has type "def __call__(__self, *args: Any, **kwargs: Any) -> def (*Any) -> Any" use_func_callable(partial(func_fn_unpack, b="")) # But we should not erase typevars that aren't bound by function @@ -714,7 +714,7 @@ def outer_b(arg: Tb) -> None: reveal_type(partial(inner, b="")) # N: Revealed type is "functools.partial[Tb`-1]" use_int_callable(partial(inner, b="")) # E: Argument 1 to "use_int_callable" has incompatible type "partial[Tb]"; expected "Callable[[int], int]" \ - # N: "partial[Tb].__call__" has type "Callable[[VarArg(Any), KwArg(Any)], Tb]" + # N: "partial[Tb].__call__" has type "def __call__(__self, *args: Any, **kwargs: Any) -> Tb" def outer_c(arg: Tc) -> None: @@ -724,5 +724,5 @@ def outer_c(arg: Tc) -> None: reveal_type(partial(inner, b="")) # N: Revealed type is "functools.partial[builtins.int]" \ # N: Revealed type is "functools.partial[builtins.str]" use_int_callable(partial(inner, b="")) # E: Argument 1 to "use_int_callable" has incompatible type "partial[str]"; expected "Callable[[int], int]" \ - # N: "partial[str].__call__" has type "Callable[[VarArg(Any), KwArg(Any)], str]" + # N: "partial[str].__call__" has type "def __call__(__self, *args: Any, **kwargs: Any) -> str" [builtins fixtures/tuple.pyi] diff --git a/test-data/unit/check-incremental.test b/test-data/unit/check-incremental.test index 94f65a950062..5fbaa4f2c904 100644 --- a/test-data/unit/check-incremental.test +++ b/test-data/unit/check-incremental.test @@ -6950,7 +6950,7 @@ p3 = functools.partial(foo, b="a") [out] tmp/a.py:8: note: Revealed type is "functools.partial[builtins.int]" tmp/a.py:13: error: Argument 1 to "takes_callable_str" has incompatible type "partial[int]"; expected "Callable[..., str]" -tmp/a.py:13: note: "partial[int].__call__" has type "Callable[[VarArg(Any), KwArg(Any)], int]" +tmp/a.py:13: note: "partial[int].__call__" has type "def __call__(__self, *args: Any, **kwargs: Any) -> int" tmp/a.py:18: error: Argument 1 to "foo" has incompatible type "int"; expected "str" tmp/a.py:19: error: Too many arguments for "foo" tmp/a.py:19: error: Argument 1 to "foo" has incompatible type "int"; expected "str" diff --git a/test-data/unit/check-inference.test b/test-data/unit/check-inference.test index 24ea61f2c715..38ee883b7f05 100644 --- a/test-data/unit/check-inference.test +++ b/test-data/unit/check-inference.test @@ -1109,7 +1109,7 @@ def f(*, x: int) -> int: ... def g(*, y: int) -> int: ... def h(*, x: int) -> int: ... -list_1 = [f, g] # E: List item 0 has incompatible type "Callable[[NamedArg(int, 'x')], int]"; expected "Callable[[NamedArg(int, 'y')], int]" +list_1 = [f, g] # E: List item 0 has incompatible type "def f(*, x: int) -> int"; expected "def g(*, y: int) -> int" list_2 = [f, h] [builtins fixtures/list.pyi] @@ -1434,7 +1434,7 @@ from typing import Callable def f(a: Callable[..., None] = lambda *a, **k: None): pass -def g(a: Callable[..., None] = lambda *a, **k: 1): # E: Incompatible default for argument "a" (default has type "Callable[[VarArg(Any), KwArg(Any)], int]", argument has type "Callable[..., None]") +def g(a: Callable[..., None] = lambda *a, **k: 1): # E: Incompatible default for argument "a" (default has type "def (*a: Any, **k: Any) -> int", argument has type "Callable[..., None]") pass [builtins fixtures/dict.pyi] @@ -3704,7 +3704,7 @@ def f(x: Call[T]) -> Tuple[T, T]: ... def g(__x: str) -> None: pass reveal_type(f(g)) # N: Revealed type is "tuple[Never, Never]" \ # E: Argument 1 to "f" has incompatible type "Callable[[str], None]"; expected "Call[Never]" \ - # N: "Call[Never].__call__" has type "Callable[[NamedArg(Never, 'x')], None]" + # N: "Call[Never].__call__" has type "def __call__(self, *, x: Never) -> None" [builtins fixtures/list.pyi] [case testCallableInferenceAgainstCallableNamedVsPosOnly] @@ -3720,7 +3720,7 @@ def f(x: Call[T]) -> Tuple[T, T]: ... def g(*, x: str) -> None: pass reveal_type(f(g)) # N: Revealed type is "tuple[Never, Never]" \ - # E: Argument 1 to "f" has incompatible type "Callable[[NamedArg(str, 'x')], None]"; expected "Call[Never]" \ + # E: Argument 1 to "f" has incompatible type "def g(*, x: str) -> None"; expected "Call[Never]" \ # N: "Call[Never].__call__" has type "Callable[[Never], None]" [builtins fixtures/list.pyi] @@ -3737,7 +3737,7 @@ def f(x: Call[T]) -> Tuple[T, T]: ... def g(**x: str) -> None: pass reveal_type(f(g)) # N: Revealed type is "tuple[Never, Never]" \ - # E: Argument 1 to "f" has incompatible type "Callable[[KwArg(str)], None]"; expected "Call[Never]" \ + # E: Argument 1 to "f" has incompatible type "def g(**x: str) -> None"; expected "Call[Never]" \ # N: "Call[Never].__call__" has type "Callable[[Never], None]" [builtins fixtures/list.pyi] @@ -3754,8 +3754,8 @@ def f(x: Call[T]) -> Tuple[T, T]: ... def g(*args: str) -> None: pass reveal_type(f(g)) # N: Revealed type is "tuple[Never, Never]" \ - # E: Argument 1 to "f" has incompatible type "Callable[[VarArg(str)], None]"; expected "Call[Never]" \ - # N: "Call[Never].__call__" has type "Callable[[NamedArg(Never, 'x')], None]" + # E: Argument 1 to "f" has incompatible type "def g(*args: str) -> None"; expected "Call[Never]" \ + # N: "Call[Never].__call__" has type "def __call__(self, *, x: Never) -> None" [builtins fixtures/list.pyi] [case testInferenceAgainstTypeVarActualBound] diff --git a/test-data/unit/check-parameter-specification.test b/test-data/unit/check-parameter-specification.test index 2b4f92c7c819..bffd34782f51 100644 --- a/test-data/unit/check-parameter-specification.test +++ b/test-data/unit/check-parameter-specification.test @@ -443,7 +443,7 @@ reveal_type(register(lambda: f(1))) # N: Revealed type is "def ()" reveal_type(register(lambda x: f(x), x=1)) # N: Revealed type is "def (x: Literal[1]?)" register(lambda x: f(x)) # E: Cannot infer type of lambda \ # E: Argument 1 to "register" has incompatible type "Callable[[Any], None]"; expected "Callable[[], None]" -register(lambda x: f(x), y=1) # E: Argument 1 to "register" has incompatible type "Callable[[Arg(int, 'x')], None]"; expected "Callable[[Arg(int, 'y')], None]" +register(lambda x: f(x), y=1) # E: Argument 1 to "register" has incompatible type "def (x: int) -> None"; expected "def (y: int) -> None" reveal_type(register(lambda x: f(x), 1)) # N: Revealed type is "def (Literal[1]?)" reveal_type(register(lambda x, y: g(x, y), 1, "a")) # N: Revealed type is "def (Literal[1]?, Literal['a']?)" reveal_type(register(lambda x, y: g(x, y), 1, y="a")) # N: Revealed type is "def (Literal[1]?, y: Literal['a']?)" @@ -623,10 +623,10 @@ def expects_int_first(x: Callable[Concatenate[int, P], int]) -> None: ... # N: This is likely because "one" has named arguments: "x". Consider marking them positional-only def one(x: str) -> int: ... -@expects_int_first # E: Argument 1 to "expects_int_first" has incompatible type "Callable[[NamedArg(int, 'x')], int]"; expected "Callable[[int, NamedArg(int, 'x')], int]" +@expects_int_first # E: Argument 1 to "expects_int_first" has incompatible type "def two(*, x: int) -> int"; expected "def (int, /, *, x: int) -> int" def two(*, x: int) -> int: ... -@expects_int_first # E: Argument 1 to "expects_int_first" has incompatible type "Callable[[KwArg(int)], int]"; expected "Callable[[int, KwArg(int)], int]" +@expects_int_first # E: Argument 1 to "expects_int_first" has incompatible type "def three(**kwargs: int) -> int"; expected "def (int, /, **kwargs: int) -> int" def three(**kwargs: int) -> int: ... @expects_int_first # Accepted @@ -2154,7 +2154,7 @@ reveal_type(submit( # N: Revealed type is "__main__.Result" backend="asyncio", )) submit( - run, # E: Argument 1 to "submit" has incompatible type "Callable[[Callable[[], R], VarArg(object), DefaultNamedArg(str, 'backend')], R]"; expected "Callable[[Callable[[], Result], int], Result]" + run, # E: Argument 1 to "submit" has incompatible type "def [R] run(func: Callable[[], R], *args: object, backend: str = ...) -> R"; expected "Callable[[Callable[[], Result], int], Result]" run_portal, backend=int(), ) diff --git a/test-data/unit/check-protocols.test b/test-data/unit/check-protocols.test index ae6f60355512..06d6ad161f95 100644 --- a/test-data/unit/check-protocols.test +++ b/test-data/unit/check-protocols.test @@ -1894,7 +1894,7 @@ reveal_type(apply_gen(Add5())) # N: Revealed type is "builtins.int" def apply_str(f: Callable[[str], int], x: str) -> int: return f(x) apply_str(Add5(), 'a') # E: Argument 1 to "apply_str" has incompatible type "Add5"; expected "Callable[[str], int]" \ - # N: "Add5.__call__" has type "Callable[[Arg(int, 'x')], int]" + # N: "Add5.__call__" has type "def __call__(self, x: int) -> int" [builtins fixtures/isinstancelist.pyi] [case testMoreComplexCallableStructuralSubtyping] @@ -1910,10 +1910,10 @@ class Bad1: class Bad2: def __call__(self, y: int, *rest: str) -> int: pass call_soon(Good()) -call_soon(Bad1()) # E: Argument 1 to "call_soon" has incompatible type "Bad1"; expected "Callable[[int, VarArg(str)], int]" \ - # N: "Bad1.__call__" has type "Callable[[Arg(int, 'x'), VarArg(int)], int]" -call_soon(Bad2()) # E: Argument 1 to "call_soon" has incompatible type "Bad2"; expected "Callable[[int, VarArg(str)], int]" \ - # N: "Bad2.__call__" has type "Callable[[Arg(int, 'y'), VarArg(str)], int]" +call_soon(Bad1()) # E: Argument 1 to "call_soon" has incompatible type "Bad1"; expected "def (x: int, *str) -> int" \ + # N: "Bad1.__call__" has type "def __call__(self, x: int, *rest: int) -> int" +call_soon(Bad2()) # E: Argument 1 to "call_soon" has incompatible type "Bad2"; expected "def (x: int, *str) -> int" \ + # N: "Bad2.__call__" has type "def __call__(self, y: int, *rest: str) -> int" [builtins fixtures/isinstancelist.pyi] [case testStructuralSupportForPartial] @@ -2473,8 +2473,8 @@ def func(caller: Caller) -> None: pass func(call) -func(bad) # E: Argument 1 to "func" has incompatible type "Callable[[int, VarArg(str)], None]"; expected "Caller" \ - # N: "Caller.__call__" has type "Callable[[Arg(str, 'x'), VarArg(int)], None]" +func(bad) # E: Argument 1 to "func" has incompatible type "def bad(x: int, *args: str) -> None"; expected "Caller" \ + # N: "Caller.__call__" has type "def __call__(self, x: str, *args: int) -> None" [builtins fixtures/tuple.pyi] [out] @@ -2512,7 +2512,7 @@ def func(caller: Caller) -> None: func(call) func(bad) # E: Argument 1 to "func" has incompatible type "Callable[[int], int]"; expected "Caller" \ - # N: "Caller.__call__" has type "Callable[[Arg(T, 'x')], T]" + # N: "Caller.__call__" has type "def [T] __call__(self, x: T) -> T" [builtins fixtures/tuple.pyi] [out] @@ -2533,7 +2533,7 @@ def func(caller: Caller) -> None: func(call) func(bad) # E: Argument 1 to "func" has incompatible type "Callable[[T], tuple[T, T]]"; expected "Caller" \ - # N: "Caller.__call__" has type "Callable[[Arg(int, 'x')], int]" + # N: "Caller.__call__" has type "def __call__(self, x: int) -> int" [builtins fixtures/tuple.pyi] [out] @@ -2573,8 +2573,8 @@ class Caller(Protocol): def bad(x: int, *args: str) -> None: pass -cb: Caller = bad # E: Incompatible types in assignment (expression has type "Callable[[int, VarArg(str)], None]", variable has type "Caller") \ - # N: "Caller.__call__" has type "Callable[[Arg(str, 'x'), VarArg(int)], None]" +cb: Caller = bad # E: Incompatible types in assignment (expression has type "def bad(x: int, *args: str) -> None", variable has type "Caller") \ + # N: "Caller.__call__" has type "def __call__(self, x: str, *args: int) -> None" [builtins fixtures/tuple.pyi] [out] @@ -2601,7 +2601,7 @@ def anon(caller: CallerAnon) -> None: func(call) func(bad) # E: Argument 1 to "func" has incompatible type "Callable[[str], None]"; expected "Caller" \ - # N: "Caller.__call__" has type "Callable[[Arg(str, 'x')], None]" + # N: "Caller.__call__" has type "def __call__(self, x: str) -> None" anon(bad) [out] @@ -2625,7 +2625,7 @@ b: Bad func(a) func(b) # E: Argument 1 to "func" has incompatible type "Bad"; expected "One" \ - # N: "One.__call__" has type "Callable[[Arg(str, 'x')], None]" + # N: "One.__call__" has type "def __call__(self, x: str) -> None" [out] [case testJoinProtocolCallback] @@ -3597,7 +3597,7 @@ test(C) # E: Argument 1 to "test" has incompatible type "type[C]"; expected "P" # N: def __call__(x: int, y: int) -> Any \ # N: Got: \ # N: def __init__(x: int, y: str) -> C \ - # N: "P.__call__" has type "Callable[[Arg(int, 'x'), Arg(int, 'y')], Any]" + # N: "P.__call__" has type "def __call__(self, x: int, y: int) -> Any" [case testProtocolClassObjectPureCallback] from typing import Any, ClassVar, Protocol @@ -3619,7 +3619,7 @@ test(C) # E: Argument 1 to "test" has incompatible type "type[C]"; expected "P" # N: def __call__(x: int, y: int) -> Any \ # N: Got: \ # N: def __init__(x: int, y: str) -> C \ - # N: "P.__call__" has type "Callable[[Arg(int, 'x'), Arg(int, 'y')], Any]" + # N: "P.__call__" has type "def __call__(self, x: int, y: int) -> Any" [builtins fixtures/type.pyi] [case testProtocolClassObjectCallableError] @@ -3642,7 +3642,7 @@ p: P = C # E: Incompatible types in assignment (expression has type "type[C]", # N: def __call__(app: int) -> Callable[[str], None] \ # N: Got: \ # N: def __init__(app: str) -> C \ - # N: "P.__call__" has type "Callable[[Arg(int, 'app')], Callable[[str], None]]" + # N: "P.__call__" has type "def __call__(self, app: int) -> Callable[[str], None]" [builtins fixtures/type.pyi] @@ -3801,7 +3801,7 @@ def f_good(t: S) -> S: return t g: C = f_bad # E: Incompatible types in assignment (expression has type "Callable[[int], int]", variable has type "C") \ - # N: "C.__call__" has type "Callable[[Arg(T, 't')], T]" + # N: "C.__call__" has type "def [T] __call__(self, t: T) -> T" g = f_good # OK [case testModuleAsProtocolImplementation] diff --git a/test-data/unit/check-python311.test b/test-data/unit/check-python311.test index 09c8d6082365..c2a0bb09810a 100644 --- a/test-data/unit/check-python311.test +++ b/test-data/unit/check-python311.test @@ -157,7 +157,7 @@ Alias1 = Callable[[*Ts], int] # E: Variable "__main__.Ts" is not valid as a typ x1: Alias1[int] # E: Bad number of arguments for type alias, expected 0, given 1 reveal_type(x1) # N: Revealed type is "def (*Any) -> builtins.int" x1 = good -x1 = bad # E: Incompatible types in assignment (expression has type "Callable[[VarArg(int), NamedArg(int, 'y')], int]", variable has type "Callable[[VarArg(Any)], int]") +x1 = bad # E: Incompatible types in assignment (expression has type "def bad(*x: int, y: int) -> int", variable has type "def (*Any) -> int") Alias2 = Callable[[*T], int] # E: "T" cannot be unpacked (must be tuple or TypeVarTuple) x2: Alias2[int] diff --git a/test-data/unit/check-statements.test b/test-data/unit/check-statements.test index 9ab68b32472d..658bee76ef0d 100644 --- a/test-data/unit/check-statements.test +++ b/test-data/unit/check-statements.test @@ -2358,6 +2358,6 @@ describe(CAny()) describe(C()) describe(CNone()) describe(CWrong()) # E: Argument 1 to "describe" has incompatible type "CWrong"; expected "Callable[[], None]" \ - # N: "CWrong.__call__" has type "Callable[[Arg(int, 'x')], None]" + # N: "CWrong.__call__" has type "def __call__(self, x: int) -> None" describe(f) [builtins fixtures/isinstancelist.pyi] diff --git a/test-data/unit/check-typevar-tuple.test b/test-data/unit/check-typevar-tuple.test index 927a4f037a4a..cb5029ee4e6d 100644 --- a/test-data/unit/check-typevar-tuple.test +++ b/test-data/unit/check-typevar-tuple.test @@ -518,15 +518,15 @@ call(target=func, args=(0, 'foo')) call(target=func, args=('bar', 'foo')) # E: Argument "target" to "call" has incompatible type "Callable[[int, str], None]"; expected "Callable[[str, str], None]" call(target=func, args=(True, 'foo', 0)) # E: Argument "target" to "call" has incompatible type "Callable[[int, str], None]"; expected "Callable[[bool, str, int], None]" call(target=func, args=(0, 0, 'foo')) # E: Argument "target" to "call" has incompatible type "Callable[[int, str], None]"; expected "Callable[[int, int, str], None]" -call(target=func, args=vargs) # E: Argument "target" to "call" has incompatible type "Callable[[int, str], None]"; expected "Callable[[VarArg(int)], None]" +call(target=func, args=vargs) # E: Argument "target" to "call" has incompatible type "Callable[[int, str], None]"; expected "def (*int) -> None" # NOTE: This behavior may be a bit contentious, it is maybe inconsistent with our handling of # PEP646 but consistent with our handling of callable constraints. -call(target=func2, args=vargs) # E: Argument "target" to "call" has incompatible type "Callable[[int, int], None]"; expected "Callable[[VarArg(int)], None]" +call(target=func2, args=vargs) # E: Argument "target" to "call" has incompatible type "Callable[[int, int], None]"; expected "def (*int) -> None" call(target=func3, args=vargs) call(target=func3, args=(0,1)) -call(target=func3, args=(0,'foo')) # E: Argument "target" to "call" has incompatible type "Callable[[VarArg(int)], None]"; expected "Callable[[int, str], None]" -call(target=func3, args=vargs_str) # E: Argument "target" to "call" has incompatible type "Callable[[VarArg(int)], None]"; expected "Callable[[VarArg(str)], None]" +call(target=func3, args=(0,'foo')) # E: Argument "target" to "call" has incompatible type "def func3(*args: int) -> None"; expected "Callable[[int, str], None]" +call(target=func3, args=vargs_str) # E: Argument "target" to "call" has incompatible type "def func3(*args: int) -> None"; expected "def (*str) -> None" [builtins fixtures/tuple.pyi] [case testTypeVarTuplePep646CallableWithPrefixSuffix] @@ -1903,7 +1903,7 @@ def foo3(func: Callable[[int, Unpack[Args2]], T], *args: Unpack[Args2]) -> T: return submit2(func, 1, *args) def foo_bad(func: Callable[[Unpack[Args2]], T], *args: Unpack[Args2]) -> T: - return submit2(func, 1, *args) # E: Argument 1 to "submit2" has incompatible type "Callable[[VarArg(Unpack[Args2])], T]"; expected "Callable[[int, VarArg(Unpack[Args2])], T]" + return submit2(func, 1, *args) # E: Argument 1 to "submit2" has incompatible type "def (*Unpack[Args2]) -> T"; expected "def (int, /, *Unpack[Args2]) -> T" [builtins fixtures/tuple.pyi] [case testTypeVarTupleParamSpecInteraction] @@ -2321,8 +2321,8 @@ higher_order(good2) higher_order(ok1) higher_order(ok2) -higher_order(bad1) # E: Argument 1 to "higher_order" has incompatible type "Callable[[NamedArg(str, 'd')], int]"; expected "Callable[[VarArg(Any)], Any]" -higher_order(bad2) # E: Argument 1 to "higher_order" has incompatible type "Callable[[KwArg(None)], None]"; expected "Callable[[VarArg(Any)], Any]" +higher_order(bad1) # E: Argument 1 to "higher_order" has incompatible type "def bad1(*, d: str) -> int"; expected "def (*Any) -> Any" +higher_order(bad2) # E: Argument 1 to "higher_order" has incompatible type "def bad2(**kwargs: None) -> None"; expected "def (*Any) -> Any" [builtins fixtures/tuple.pyi] [case testAliasToCallableWithUnpack2] @@ -2338,10 +2338,10 @@ def bad3(*, d: str) -> int: ... def bad4(**kwargs: None) -> None: ... higher_order(good) -higher_order(bad1) # E: Argument 1 to "higher_order" has incompatible type "Callable[[str, int], None]"; expected "Callable[[int, str, VarArg(Unpack[tuple[Unpack[tuple[Any, ...]], int]])], Any]" -higher_order(bad2) # E: Argument 1 to "higher_order" has incompatible type "Callable[[bytes, VarArg(int)], str]"; expected "Callable[[int, str, VarArg(Unpack[tuple[Unpack[tuple[Any, ...]], int]])], Any]" -higher_order(bad3) # E: Argument 1 to "higher_order" has incompatible type "Callable[[NamedArg(str, 'd')], int]"; expected "Callable[[int, str, VarArg(Unpack[tuple[Unpack[tuple[Any, ...]], int]])], Any]" -higher_order(bad4) # E: Argument 1 to "higher_order" has incompatible type "Callable[[KwArg(None)], None]"; expected "Callable[[int, str, VarArg(Unpack[tuple[Unpack[tuple[Any, ...]], int]])], Any]" +higher_order(bad1) # E: Argument 1 to "higher_order" has incompatible type "Callable[[str, int], None]"; expected "def (int, str, /, *Unpack[tuple[Unpack[tuple[Any, ...]], int]]) -> Any" +higher_order(bad2) # E: Argument 1 to "higher_order" has incompatible type "def bad2(c: bytes, *args: int) -> str"; expected "def (int, str, /, *Unpack[tuple[Unpack[tuple[Any, ...]], int]]) -> Any" +higher_order(bad3) # E: Argument 1 to "higher_order" has incompatible type "def bad3(*, d: str) -> int"; expected "def (int, str, /, *Unpack[tuple[Unpack[tuple[Any, ...]], int]]) -> Any" +higher_order(bad4) # E: Argument 1 to "higher_order" has incompatible type "def bad4(**kwargs: None) -> None"; expected "def (int, str, /, *Unpack[tuple[Unpack[tuple[Any, ...]], int]]) -> Any" [builtins fixtures/tuple.pyi] [case testAliasToCallableWithUnpackInvalid] @@ -2357,7 +2357,7 @@ Alias = Callable[[Unpack[T]], int] # E: "T" cannot be unpacked (must be tuple o x: Alias[int] reveal_type(x) # N: Revealed type is "def (*Any) -> builtins.int" x = good -x = bad # E: Incompatible types in assignment (expression has type "Callable[[VarArg(int), NamedArg(int, 'y')], int]", variable has type "Callable[[VarArg(Any)], int]") +x = bad # E: Incompatible types in assignment (expression has type "def bad(*x: int, y: int) -> int", variable has type "def (*Any) -> int") [builtins fixtures/tuple.pyi] [case testTypeVarTupleInvariant] @@ -2443,7 +2443,7 @@ class CM(Generic[R]): ... def cm(fn: Callable[P, List[R]]) -> Callable[P, CM[R]]: ... Ts = TypeVarTuple("Ts") -@cm # E: Argument 1 to "cm" has incompatible type "Callable[[VarArg(Unpack[Ts])], tuple[Unpack[Ts]]]"; expected "Callable[[VarArg(Never)], list[Never]]" +@cm # E: Argument 1 to "cm" has incompatible type "def [Ts`-1] test(*args: Unpack[Ts]) -> tuple[Unpack[Ts]]"; expected "def (*args: Never) -> list[Never]" def test(*args: Unpack[Ts]) -> Tuple[Unpack[Ts]]: ... reveal_type(test) # N: Revealed type is "def (*args: Never) -> __main__.CM[Never]" diff --git a/test-data/unit/check-varargs.test b/test-data/unit/check-varargs.test index 680021a166f2..3b80b9e8829a 100644 --- a/test-data/unit/check-varargs.test +++ b/test-data/unit/check-varargs.test @@ -660,7 +660,7 @@ x: Callable[[int], None] def f(*x: int) -> None: pass def g(*x: str) -> None: pass x = f -x = g # E: Incompatible types in assignment (expression has type "Callable[[VarArg(str)], None]", variable has type "Callable[[int], None]") +x = g # E: Incompatible types in assignment (expression has type "def g(*x: str) -> None", variable has type "Callable[[int], None]") [builtins fixtures/list.pyi] [out] diff --git a/test-data/unit/pythoneval.test b/test-data/unit/pythoneval.test index 2069d082df17..7dfcf7447b61 100644 --- a/test-data/unit/pythoneval.test +++ b/test-data/unit/pythoneval.test @@ -616,7 +616,7 @@ def f(*args: str) -> str: return args[0] map(f, ['x']) map(f, [1]) [out] -_program.py:4: error: Argument 1 to "map" has incompatible type "Callable[[VarArg(str)], str]"; expected "Callable[[int], str]" +_program.py:4: error: Argument 1 to "map" has incompatible type "def f(*args: str) -> str"; expected "Callable[[int], str]" [case testMapStr] import typing From 6ae4460204896e47791cc521d3cefc73e82f1f95 Mon Sep 17 00:00:00 2001 From: theodoreando Date: Mon, 27 Oct 2025 11:27:26 -0500 Subject: [PATCH 2/4] extract condition of if arg is should use complex formatting --- mypy/messages.py | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/mypy/messages.py b/mypy/messages.py index 094b51c91398..2a5e06f8a3ed 100644 --- a/mypy/messages.py +++ b/mypy/messages.py @@ -2530,6 +2530,15 @@ def quote_type_string(type_string: str) -> str: return f'"{type_string}"' +def should_format_arg_as_type(arg_kind: ArgKind, arg_name: str | None, verbosity: int) -> bool: + """ + Determine whether a function argument should be formatted as its Type or with name. + """ + return (arg_kind == ARG_POS and arg_name is None) or ( + verbosity == 0 and arg_kind.is_positional() + ) + + def format_callable_args( arg_types: list[Type], arg_kinds: list[ArgKind], @@ -2540,7 +2549,7 @@ def format_callable_args( """Format a bunch of Callable arguments into a string""" arg_strings = [] for arg_name, arg_type, arg_kind in zip(arg_names, arg_types, arg_kinds): - if arg_kind == ARG_POS and arg_name is None or verbosity == 0 and arg_kind.is_positional(): + if should_format_arg_as_type(arg_kind, arg_name, verbosity): arg_strings.append(format(arg_type)) else: constructor = ARG_CONSTRUCTOR_NAMES[arg_kind] @@ -2769,10 +2778,8 @@ def format_literal_value(typ: LiteralType) -> str: # Use pretty format (def-style) for complex signatures with named, optional, or star args. # Use compact Callable[[...], ...] only for signatures with all simple positional args. has_complex_args = any( - not ( - (kind == ARG_POS and name is None) or (verbosity == 0 and kind.is_positional()) - ) - for name, kind in zip(func.arg_names, func.arg_kinds) + not should_format_arg_as_type(kind, name, verbosity) + for kind, name in zip(func.arg_kinds, func.arg_names) ) if use_pretty_callable and has_complex_args: return pretty_callable(func, options) From 61ebceded1c28a383e7a70a242a0bf7ceca24675 Mon Sep 17 00:00:00 2001 From: theodoreando Date: Mon, 27 Oct 2025 12:08:19 -0500 Subject: [PATCH 3/4] docstring improvements for previously undocumented args --- mypy/messages.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/mypy/messages.py b/mypy/messages.py index 2a5e06f8a3ed..3554bab5a0c2 100644 --- a/mypy/messages.py +++ b/mypy/messages.py @@ -2575,9 +2575,10 @@ def format_type_inner( Args: typ: type to be formatted verbosity: a coarse grained control on the verbosity of the type + options: Options object controlling formatting fullnames: a set of names that should be printed in full - use_pretty_callable: use MessageBuilder.pretty_callable to format Callable - types. + module_names: whether to show module names for module types + use_pretty_callable: use pretty_callable to format Callable types. """ def format(typ: Type) -> str: @@ -2777,11 +2778,11 @@ def format_literal_value(typ: LiteralType) -> str: # Use pretty format (def-style) for complex signatures with named, optional, or star args. # Use compact Callable[[...], ...] only for signatures with all simple positional args. - has_complex_args = any( + would_use_arg_constructors = any( not should_format_arg_as_type(kind, name, verbosity) for kind, name in zip(func.arg_kinds, func.arg_names) ) - if use_pretty_callable and has_complex_args: + if use_pretty_callable and would_use_arg_constructors: return pretty_callable(func, options) args = format_callable_args( From 35a0c4dc95c2a07b516052758d411052aae55ea7 Mon Sep 17 00:00:00 2001 From: hauntsaninja Date: Sun, 2 Nov 2025 23:34:00 -0800 Subject: [PATCH 4/4] microoptimise --- mypy/messages.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/mypy/messages.py b/mypy/messages.py index 3554bab5a0c2..a9e8ee2e43ab 100644 --- a/mypy/messages.py +++ b/mypy/messages.py @@ -2778,12 +2778,12 @@ def format_literal_value(typ: LiteralType) -> str: # Use pretty format (def-style) for complex signatures with named, optional, or star args. # Use compact Callable[[...], ...] only for signatures with all simple positional args. - would_use_arg_constructors = any( - not should_format_arg_as_type(kind, name, verbosity) - for kind, name in zip(func.arg_kinds, func.arg_names) - ) - if use_pretty_callable and would_use_arg_constructors: - return pretty_callable(func, options) + if use_pretty_callable: + if any( + not should_format_arg_as_type(kind, name, verbosity) + for kind, name in zip(func.arg_kinds, func.arg_names) + ): + return pretty_callable(func, options) args = format_callable_args( func.arg_types, func.arg_kinds, func.arg_names, format, verbosity