Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 23 additions & 16 deletions mypy/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -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})"

Expand Down
12 changes: 6 additions & 6 deletions test-data/unit/check-classes.test
Original file line number Diff line number Diff line change
Expand Up @@ -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]


Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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:
Expand All @@ -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
Expand Down
10 changes: 5 additions & 5 deletions test-data/unit/check-flags.test
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
6 changes: 3 additions & 3 deletions test-data/unit/check-inference.test
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
4 changes: 2 additions & 2 deletions test-data/unit/check-overloading.test
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 7 additions & 7 deletions test-data/unit/check-protocols.test
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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]

Expand All @@ -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]
Expand Down Expand Up @@ -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
Expand All @@ -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:
Expand All @@ -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
Expand All @@ -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:
Expand Down
6 changes: 3 additions & 3 deletions test-data/unit/check-python311.test
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down
6 changes: 3 additions & 3 deletions test-data/unit/check-selftype.test
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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"
Expand Down
4 changes: 2 additions & 2 deletions test-data/unit/check-serialize.test
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions test-data/unit/check-type-aliases.test
Original file line number Diff line number Diff line change
Expand Up @@ -1339,16 +1339,16 @@ 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]"
reveal_type(D()) # N: Revealed type is "builtins.dict[builtins.str, builtins.int]"
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]

Expand Down
4 changes: 2 additions & 2 deletions test-data/unit/fine-grained.test
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down