Skip to content

Commit bc0323e

Browse files
committed
Merge branch 'eigengrau-disambiguate-if-needed'
2 parents d7178c0 + 676c724 commit bc0323e

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

mypy/messages.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -417,8 +417,14 @@ def incompatible_argument(self, n: int, m: int, callee: CallableType, arg_type:
417417
expected_type = callee.arg_types[m - 1]
418418
except IndexError: # Varargs callees
419419
expected_type = callee.arg_types[-1]
420+
arg_type_str = self.format(arg_type)
421+
expected_type_str = self.format(expected_type)
422+
# If type names turn out ambiguous, attempt to disambiguate.
423+
if arg_type_str == expected_type_str:
424+
arg_type_str = self.format(arg_type, verbose=True)
425+
expected_type_str = self.format(expected_type, verbose=True)
420426
msg = 'Argument {} {}has incompatible type {}; expected {}'.format(
421-
n, target, self.format(arg_type), self.format(expected_type))
427+
n, target, arg_type_str, expected_type_str)
422428
self.fail(msg, context)
423429

424430
def invalid_index_type(self, index_type: Type, base_str: str,

mypy/test/data/check-classes.test

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1228,6 +1228,12 @@ class B:
12281228
[out]
12291229
main: note: In member "f" of class "B":
12301230

1231+
[case testClassVsInstanceDisambiguation]
1232+
class A: pass
1233+
def f(x: A) -> None: pass
1234+
f(A) # E: Argument 1 to "f" has incompatible type "A" (type object); expected "A"
1235+
[out]
1236+
12311237
-- TODO
12321238
-- attribute inherited from superclass; assign in __init__
12331239
-- refer to attribute before type has been inferred (the initialization in

0 commit comments

Comments
 (0)