diff --git a/mypy/messages.py b/mypy/messages.py index bbcc93ebfb25..8f4fd883e85a 100644 --- a/mypy/messages.py +++ b/mypy/messages.py @@ -3028,30 +3028,37 @@ def [T <: int] f(self, x: int, y: T) -> None s += ", /" slash = True - # If we got a "special arg" (i.e: self, cls, etc...), prepend it to the arg list definition = get_func_def(tp) + + # Extract function name, prefer the "human-readable" name if available. + func_name = None + if tp.name: + func_name = tp.name.split()[0] # skip "of Class" part + elif isinstance(definition, FuncDef): + func_name = definition.name + + # If we got a "special arg" (i.e: self, cls, etc...), prepend it to the arg list + first_arg = None if ( isinstance(definition, FuncDef) and hasattr(definition, "arguments") and not tp.from_concatenate ): definition_arg_names = [arg.variable.name for arg in definition.arguments] - if ( - len(definition_arg_names) > len(tp.arg_names) - and definition_arg_names[0] - and not skip_self - ): - if s: - s = ", " + s - s = definition_arg_names[0] + s - s = f"{definition.name}({s})" - elif tp.name: + if len(definition_arg_names) > len(tp.arg_names) and definition_arg_names[0]: + first_arg = definition_arg_names[0] + else: + # TODO: avoid different logic for incremental runs. first_arg = get_first_arg(tp) - if first_arg: - if s: - s = ", " + s - s = first_arg + s - s = f"{tp.name.split()[0]}({s})" # skip "of Class" part + + if tp.is_type_obj(): + skip_self = True + if first_arg and not skip_self: + if s: + s = ", " + s + s = first_arg + s + if func_name: + s = f"{func_name}({s})" else: s = f"({s})" diff --git a/test-data/unit/check-classes.test b/test-data/unit/check-classes.test index 7e00017a088f..14ae2bbaf870 100644 --- a/test-data/unit/check-classes.test +++ b/test-data/unit/check-classes.test @@ -3492,8 +3492,8 @@ c.a # E: "C" has no attribute "a" C('', '') C('') # E: No overload variant of "C" matches argument type "str" \ # N: Possible overload variants: \ - # N: def __new__(cls, foo: int) -> C \ - # N: def __new__(cls, x: str, y: str) -> C + # N: def C(foo: int) -> C \ + # N: def C(x: str, y: str) -> C [builtins fixtures/__new__.pyi] @@ -3922,8 +3922,8 @@ u = new(User) [out] tmp/foo.pyi:17: error: No overload variant of "User" matches argument type "str" tmp/foo.pyi:17: note: Possible overload variants: -tmp/foo.pyi:17: note: def __init__(self) -> U -tmp/foo.pyi:17: note: def __init__(self, arg: int) -> U +tmp/foo.pyi:17: note: def User() -> U +tmp/foo.pyi:17: note: def User(arg: int) -> U tmp/foo.pyi:18: error: Too many arguments for "foo" of "User" [case testTypeUsingTypeCInUpperBound] @@ -8498,7 +8498,7 @@ def identity_wrapper(func: FuncT) -> FuncT: def foo(self: Any) -> str: return "" -[case testParentClassWithTypeAliasAndSubclassWithMethod_no_parallel] +[case testParentClassWithTypeAliasAndSubclassWithMethod] from typing import Any, Callable, TypeVar class Parent: @@ -8517,7 +8517,7 @@ class Child(Parent): return val def bar(self, val: str) -> str: # E: Signature of "bar" incompatible with supertype "Parent" \ # N: Superclass: \ - # N: def __init__(self) -> bar \ + # N: def bar() -> bar \ # N: Subclass: \ # N: def bar(self, val: str) -> str return val diff --git a/test-data/unit/check-flags.test b/test-data/unit/check-flags.test index a892aeba5a2c..1a3623cc5a3b 100644 --- a/test-data/unit/check-flags.test +++ b/test-data/unit/check-flags.test @@ -2477,19 +2477,19 @@ cb(lambda x: a) # OK fn = lambda x: a cb(fn) -[case testShowErrorCodeLinks_no_parallel] +[case testShowErrorCodeLinks] # flags: --show-error-codes --show-error-code-links x: int = "" # E: Incompatible types in assignment (expression has type "str", variable has type "int") [assignment] list(1) # E: No overload variant of "list" matches argument type "int" [call-overload] \ # N: Possible overload variants: \ - # N: def [T] __init__(self) -> list[T] \ - # N: def [T] __init__(self, x: Iterable[T]) -> list[T] \ + # N: def [T] list() -> list[T] \ + # N: def [T] list(x: Iterable[T]) -> list[T] \ # N: See https://mypy.rtfd.io/en/stable/_refs.html#code-call-overload for more info list(2) # E: No overload variant of "list" matches argument type "int" [call-overload] \ # N: Possible overload variants: \ - # N: def [T] __init__(self) -> list[T] \ - # N: def [T] __init__(self, x: Iterable[T]) -> list[T] + # N: def [T] list() -> list[T] \ + # N: def [T] list(x: Iterable[T]) -> list[T] [builtins fixtures/list.pyi] [case testNestedGenericInAliasDisallow] diff --git a/test-data/unit/check-inference.test b/test-data/unit/check-inference.test index 7f970487bbe7..81462aad40e9 100644 --- a/test-data/unit/check-inference.test +++ b/test-data/unit/check-inference.test @@ -1748,13 +1748,13 @@ def f(blocks: Any): # E: Name "Any" is not defined \ to_process = list(blocks) [builtins fixtures/list.pyi] -[case testSpecialCaseEmptyListInitialization2_no_parallel] +[case testSpecialCaseEmptyListInitialization2] def f(blocks: object): to_process = [] to_process = list(blocks) # E: No overload variant of "list" matches argument type "object" \ # N: Possible overload variants: \ - # N: def [T] __init__(self) -> list[T] \ - # N: def [T] __init__(self, x: Iterable[T]) -> list[T] + # N: def [T] list() -> list[T] \ + # N: def [T] list(x: Iterable[T]) -> list[T] [builtins fixtures/list.pyi] [case testInferListInitializedToEmptyAndAssigned] diff --git a/test-data/unit/check-overloading.test b/test-data/unit/check-overloading.test index 68eccb46ab21..5a445c818218 100644 --- a/test-data/unit/check-overloading.test +++ b/test-data/unit/check-overloading.test @@ -625,8 +625,8 @@ a = A(a) a = A(b) a = A(object()) # E: No overload variant of "A" matches argument type "object" \ # N: Possible overload variants: \ - # N: def __init__(self, a: A) -> A \ - # N: def __init__(self, b: B) -> A + # N: def A(a: A) -> A \ + # N: def A(b: B) -> A class A: @overload diff --git a/test-data/unit/check-protocols.test b/test-data/unit/check-protocols.test index b2f751bf3572..5dc10b9736c4 100644 --- a/test-data/unit/check-protocols.test +++ b/test-data/unit/check-protocols.test @@ -3609,7 +3609,7 @@ test(C) # E: Argument 1 to "test" has incompatible type "type[C]"; expected "P" # N: Expected: \ # N: def __call__(x: int, y: int) -> Any \ # N: Got: \ - # N: def __init__(x: int, y: str) -> C \ + # N: def C(x: int, y: str) -> C \ # N: "P.__call__" has type "def __call__(self, x: int, y: int) -> Any" [case testProtocolClassObjectPureCallback] @@ -3631,7 +3631,7 @@ test(C) # E: Argument 1 to "test" has incompatible type "type[C]"; expected "P" # N: Expected: \ # N: def __call__(x: int, y: int) -> Any \ # N: Got: \ - # N: def __init__(x: int, y: str) -> C \ + # N: def C(x: int, y: str) -> C \ # N: "P.__call__" has type "def __call__(self, x: int, y: int) -> Any" [builtins fixtures/type.pyi] @@ -3654,7 +3654,7 @@ p: P = C # E: Incompatible types in assignment (expression has type "type[C]", # N: Expected: \ # N: def __call__(app: int) -> Callable[[str], None] \ # N: Got: \ - # N: def __init__(app: str) -> C \ + # N: def C(app: str) -> C \ # N: "P.__call__" has type "def __call__(self, app: int) -> Callable[[str], None]" [builtins fixtures/type.pyi] @@ -3890,7 +3890,7 @@ other_flag = False def update() -> str: ... [builtins fixtures/module.pyi] -[case testModuleAsProtocolImplementationClassObject_no_parallel] +[case testModuleAsProtocolImplementationClassObject] import runner import bad_runner from typing import Callable, Protocol @@ -3909,7 +3909,7 @@ run(bad_runner) # E: Argument 1 to "run" has incompatible type Module; expected # N: Expected: \ # N: def (int, /) -> Result \ # N: Got: \ - # N: def __init__(arg: str) -> Run + # N: def Run(arg: str) -> Run [file runner.py] class Run: @@ -3922,7 +3922,7 @@ class Run: def __init__(self, arg: str) -> None: ... [builtins fixtures/module.pyi] -[case testModuleAsProtocolImplementationTypeAlias_no_parallel] +[case testModuleAsProtocolImplementationTypeAlias] import runner import bad_runner from typing import Callable, Protocol @@ -3941,7 +3941,7 @@ run(bad_runner) # E: Argument 1 to "run" has incompatible type Module; expected # N: Expected: \ # N: def (int, /) -> Result \ # N: Got: \ - # N: def __init__(arg: str) -> Run + # N: def Run(arg: str) -> Run [file runner.py] class Run: diff --git a/test-data/unit/check-python311.test b/test-data/unit/check-python311.test index b44af8936b97..42bdb72411ec 100644 --- a/test-data/unit/check-python311.test +++ b/test-data/unit/check-python311.test @@ -137,9 +137,9 @@ myclass2 = MyClass(float, float) reveal_type(myclass2) # N: Revealed type is "__main__.MyClass[builtins.float, builtins.float]" myclass3 = MyClass(float, float, float) # E: No overload variant of "MyClass" matches argument types "type[float]", "type[float]", "type[float]" \ # N: Possible overload variants: \ - # N: def [T1, T2] __init__(self) -> MyClass[None, None] \ - # N: def [T1, T2] __init__(self, type[T1], /) -> MyClass[T1, None] \ - # N: def [T1, T2] __init__(type[T1], type[T2], /) -> MyClass[T1, T2] + # N: def [T1, T2] MyClass() -> MyClass[None, None] \ + # N: def [T1, T2] MyClass(type[T1], /) -> MyClass[T1, None] \ + # N: def [T1, T2] MyClass(type[T1], type[T2], /) -> MyClass[T1, T2] reveal_type(myclass3) # N: Revealed type is "Any" [builtins fixtures/tuple.pyi] diff --git a/test-data/unit/check-selftype.test b/test-data/unit/check-selftype.test index 685b11c532cf..fe1c903f06ea 100644 --- a/test-data/unit/check-selftype.test +++ b/test-data/unit/check-selftype.test @@ -749,7 +749,7 @@ def bad(x: str) -> str: ... reveal_type(ci.from_item(conv)) # N: Revealed type is "builtins.str" ci.from_item(bad) # E: Argument 1 to "from_item" of "C" has incompatible type "Callable[[str], str]"; expected "Callable[[int], str]" -[case testSelfTypeRestrictedMethodOverloadInit_no_parallel] +[case testSelfTypeRestrictedMethodOverloadInit] from typing import TypeVar from lib import P, C @@ -767,8 +767,8 @@ class SubP(P[T]): SubP('no') # E: No overload variant of "SubP" matches argument type "str" \ # N: Possible overload variants: \ - # N: def [T] __init__(self, use_str: Literal[True]) -> SubP[T] \ - # N: def [T] __init__(self, use_str: Literal[False]) -> SubP[T] + # N: def [T] SubP(use_str: Literal[True]) -> SubP[T] \ + # N: def [T] SubP(use_str: Literal[False]) -> SubP[T] # This is a bit unfortunate: we don't have a way to map the overloaded __init__ to subtype. x = SubP(use_str=True) # E: Need type annotation for "x" diff --git a/test-data/unit/check-serialize.test b/test-data/unit/check-serialize.test index 3be96bb53ee1..9932d5e134e7 100644 --- a/test-data/unit/check-serialize.test +++ b/test-data/unit/check-serialize.test @@ -312,8 +312,8 @@ class A: [out2] tmp/a.py:2: error: No overload variant of "A" matches argument type "object" tmp/a.py:2: note: Possible overload variants: -tmp/a.py:2: note: def A(self, x: int) -> A -tmp/a.py:2: note: def A(self, x: str) -> A +tmp/a.py:2: note: def A(x: int) -> A +tmp/a.py:2: note: def A(x: str) -> A tmp/a.py:7: error: No overload variant of "__init__" of "A" matches argument type "object" tmp/a.py:7: note: Possible overload variants: tmp/a.py:7: note: def __init__(self, x: int) -> None diff --git a/test-data/unit/check-type-aliases.test b/test-data/unit/check-type-aliases.test index 0d2e6b5f0c9d..cec44d582f0a 100644 --- a/test-data/unit/check-type-aliases.test +++ b/test-data/unit/check-type-aliases.test @@ -1339,7 +1339,7 @@ a: A reveal_type(a) # N: Revealed type is "builtins.list[builtins.str]" [builtins fixtures/tuple.pyi] -[case testTypeAliasDict_no_parallel] +[case testTypeAliasDict] D = dict[str, int] d = D() reveal_type(d) # N: Revealed type is "builtins.dict[builtins.str, builtins.int]" @@ -1347,8 +1347,8 @@ reveal_type(D()) # N: Revealed type is "builtins.dict[builtins.str, builtins.in reveal_type(D(x=1)) # N: Revealed type is "builtins.dict[builtins.str, builtins.int]" reveal_type(D(x="asdf")) # E: No overload variant of "dict" matches argument type "str" \ # N: Possible overload variants: \ - # N: def __init__(self, **kwargs: int) -> dict[str, int] \ - # N: def __init__(self, arg: Iterable[tuple[str, int]], **kwargs: int) -> dict[str, int] \ + # N: def dict(**kwargs: int) -> dict[str, int] \ + # N: def dict(arg: Iterable[tuple[str, int]], **kwargs: int) -> dict[str, int] \ # N: Revealed type is "Any" [builtins fixtures/dict.pyi] diff --git a/test-data/unit/fine-grained.test b/test-data/unit/fine-grained.test index 070d780e93f6..e9e82fccc8c4 100644 --- a/test-data/unit/fine-grained.test +++ b/test-data/unit/fine-grained.test @@ -7381,8 +7381,8 @@ class C: == a.py:2: error: No overload variant of "B" matches argument type "int" a.py:2: note: Possible overload variants: -a.py:2: note: def __init__(self, x: str) -> B -a.py:2: note: def __init__(self, x: str, y: int) -> B +a.py:2: note: def B(x: str) -> B +a.py:2: note: def B(x: str, y: int) -> B [case testOverloadedToNormalMethodMetaclass] import a