Skip to content

Commit

Permalink
Remove special casing for "cannot" in error messages (#15428)
Browse files Browse the repository at this point in the history
Co-authored-by: hauntsaninja <hauntsaninja@gmail.com>
  • Loading branch information
ikonst and hauntsaninja authored Jun 13, 2023
1 parent cab8c67 commit 61a7f3b
Show file tree
Hide file tree
Showing 7 changed files with 151 additions and 119 deletions.
6 changes: 0 additions & 6 deletions mypy/test/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,9 @@ def run_mypy(args: list[str]) -> None:
def assert_string_arrays_equal(expected: list[str], actual: list[str], msg: str) -> None:
"""Assert that two string arrays are equal.
We consider "can't" and "cannot" equivalent, by replacing the
former with the latter before comparing.
Display any differences in a human-readable form.
"""
actual = clean_up(actual)
actual = [line.replace("can't", "cannot") for line in actual]
expected = [line.replace("can't", "cannot") for line in expected]

if actual != expected:
num_skip_start = num_skipped_prefix_lines(expected, actual)
num_skip_end = num_skipped_suffix_lines(expected, actual)
Expand Down
4 changes: 2 additions & 2 deletions test-data/unit/check-classes.test
Original file line number Diff line number Diff line change
Expand Up @@ -2019,7 +2019,7 @@ tmp/foo.pyi:5: note: @overload
tmp/foo.pyi:5: note: def __add__(self, int, /) -> int
tmp/foo.pyi:5: note: @overload
tmp/foo.pyi:5: note: def __add__(self, str, /) -> str
tmp/foo.pyi:5: note: Overloaded operator methods cannot have wider argument types in overrides
tmp/foo.pyi:5: note: Overloaded operator methods can't have wider argument types in overrides

[case testOperatorMethodOverrideWideningArgumentType]
import typing
Expand Down Expand Up @@ -2138,7 +2138,7 @@ tmp/foo.pyi:8: note: @overload
tmp/foo.pyi:8: note: def __add__(self, str, /) -> A
tmp/foo.pyi:8: note: @overload
tmp/foo.pyi:8: note: def __add__(self, type, /) -> A
tmp/foo.pyi:8: note: Overloaded operator methods cannot have wider argument types in overrides
tmp/foo.pyi:8: note: Overloaded operator methods can't have wider argument types in overrides

[case testOverloadedOperatorMethodOverrideWithSwitchedItemOrder]
from foo import *
Expand Down
149 changes: 76 additions & 73 deletions test-data/unit/check-overloading.test
Original file line number Diff line number Diff line change
Expand Up @@ -4745,7 +4745,7 @@ main:12: note: @overload
main:12: note: def __add__(self, Other, /) -> B
main:12: note: @overload
main:12: note: def __add__(self, A, /) -> A
main:12: note: Overloaded operator methods cannot have wider argument types in overrides
main:12: note: Overloaded operator methods can't have wider argument types in overrides
main:32: note: Revealed type is "__main__.Other"

[case testOverloadErrorMessageManyMatches]
Expand Down Expand Up @@ -5405,26 +5405,26 @@ if False:
def f2(g): ...
reveal_type(f2(A())) # N: Revealed type is "__main__.A"
reveal_type(f2(C())) # E: No overload variant of "f2" matches argument type "C" \
# N: Possible overload variants: \
# N: def f2(g: A) -> A \
# N: def f2(g: B) -> B \
# N: Revealed type is "Any"
# N: Possible overload variants: \
# N: def f2(g: A) -> A \
# N: def f2(g: B) -> B \
# N: Revealed type is "Any"

@overload
def f3(g: A) -> A: ...
@overload
def f3(g: B) -> B: ...
if maybe_true: # E: Condition cannot be inferred, unable to merge overloads \
if maybe_true: # E: Condition can't be inferred, unable to merge overloads \
# E: Name "maybe_true" is not defined
@overload
def f3(g: C) -> C: ...
def f3(g): ...
reveal_type(f3(A())) # N: Revealed type is "__main__.A"
reveal_type(f3(C())) # E: No overload variant of "f3" matches argument type "C" \
# N: Possible overload variants: \
# N: def f3(g: A) -> A \
# N: def f3(g: B) -> B \
# N: Revealed type is "Any"
# N: Possible overload variants: \
# N: def f3(g: A) -> A \
# N: def f3(g: B) -> B \
# N: Revealed type is "Any"

if True:
@overload
Expand Down Expand Up @@ -5731,10 +5731,10 @@ def f1(x): ...
reveal_type(f1(A())) # N: Revealed type is "__main__.A"
reveal_type(f1(B())) # N: Revealed type is "__main__.B"
reveal_type(f1(D())) # E: No overload variant of "f1" matches argument type "D" \
# N: Possible overload variants: \
# N: def f1(x: A) -> A \
# N: def f1(x: B) -> B \
# N: Revealed type is "Any"
# N: Possible overload variants: \
# N: def f1(x: A) -> A \
# N: def f1(x: B) -> B \
# N: Revealed type is "Any"

@overload
def f2(x: A) -> A: ...
Expand All @@ -5751,14 +5751,14 @@ def f2(x): ...
reveal_type(f2(A())) # N: Revealed type is "__main__.A"
reveal_type(f2(B())) # N: Revealed type is "__main__.B"
reveal_type(f2(C())) # E: No overload variant of "f2" matches argument type "C" \
# N: Possible overload variants: \
# N: def f2(x: A) -> A \
# N: def f2(x: B) -> B \
# N: Revealed type is "Any"
# N: Possible overload variants: \
# N: def f2(x: A) -> A \
# N: def f2(x: B) -> B \
# N: Revealed type is "Any"

@overload # E: Single overload definition, multiple required
def f3(x: A) -> A: ...
if maybe_true: # E: Condition cannot be inferred, unable to merge overloads \
if maybe_true: # E: Condition can't be inferred, unable to merge overloads \
# E: Name "maybe_true" is not defined
@overload
def f3(x: B) -> B: ...
Expand All @@ -5771,13 +5771,13 @@ else:
def f3(x): ...
reveal_type(f3(A())) # N: Revealed type is "__main__.A"
reveal_type(f3(B())) # E: No overload variant of "f3" matches argument type "B" \
# N: Possible overload variant: \
# N: def f3(x: A) -> A \
# N: Revealed type is "Any"
# N: Possible overload variant: \
# N: def f3(x: A) -> A \
# N: Revealed type is "Any"

@overload # E: Single overload definition, multiple required
def f4(x: A) -> A: ...
if maybe_true: # E: Condition cannot be inferred, unable to merge overloads \
if maybe_true: # E: Condition can't be inferred, unable to merge overloads \
# E: Name "maybe_true" is not defined
@overload
def f4(x: B) -> B: ...
Expand All @@ -5787,9 +5787,10 @@ else:
def f4(x): ...
reveal_type(f4(A())) # N: Revealed type is "__main__.A"
reveal_type(f4(B())) # E: No overload variant of "f4" matches argument type "B" \
# N: Possible overload variant: \
# N: def f4(x: A) -> A \
# N: Revealed type is "Any"
# N: Possible overload variant: \
# N: def f4(x: A) -> A \
# N: Revealed type is "Any"


[case testOverloadIfElse3]
# flags: --always-false False
Expand Down Expand Up @@ -5817,18 +5818,18 @@ else:
def f1(x): ...
reveal_type(f1(A())) # N: Revealed type is "__main__.A"
reveal_type(f1(B())) # E: No overload variant of "f1" matches argument type "B" \
# N: Possible overload variants: \
# N: def f1(x: A) -> A \
# N: def f1(x: D) -> D \
# N: Revealed type is "Any"
# N: Possible overload variants: \
# N: def f1(x: A) -> A \
# N: def f1(x: D) -> D \
# N: Revealed type is "Any"
reveal_type(f1(D())) # N: Revealed type is "__main__.D"

@overload # E: Single overload definition, multiple required
def f2(x: A) -> A: ...
if False:
@overload
def f2(x: B) -> B: ...
elif maybe_true: # E: Condition cannot be inferred, unable to merge overloads \
elif maybe_true: # E: Condition can't be inferred, unable to merge overloads \
# E: Name "maybe_true" is not defined
@overload
def f2(x: C) -> C: ...
Expand All @@ -5838,13 +5839,13 @@ else:
def f2(x): ...
reveal_type(f2(A())) # N: Revealed type is "__main__.A"
reveal_type(f2(C())) # E: No overload variant of "f2" matches argument type "C" \
# N: Possible overload variant: \
# N: def f2(x: A) -> A \
# N: Revealed type is "Any"
# N: Possible overload variant: \
# N: def f2(x: A) -> A \
# N: Revealed type is "Any"

@overload # E: Single overload definition, multiple required
def f3(x: A) -> A: ...
if maybe_true: # E: Condition cannot be inferred, unable to merge overloads \
if maybe_true: # E: Condition can't be inferred, unable to merge overloads \
# E: Name "maybe_true" is not defined
@overload
def f3(x: B) -> B: ...
Expand All @@ -5857,14 +5858,14 @@ else:
def f3(x): ...
reveal_type(f3(A())) # N: Revealed type is "__main__.A"
reveal_type(f3(B())) # E: No overload variant of "f3" matches argument type "B" \
# N: Possible overload variant: \
# N: def f3(x: A) -> A \
# N: Revealed type is "Any"
# N: Possible overload variant: \
# N: def f3(x: A) -> A \
# N: Revealed type is "Any"

def g(bool_var: bool) -> None:
@overload
def f4(x: A) -> A: ...
if bool_var: # E: Condition cannot be inferred, unable to merge overloads
if bool_var: # E: Condition can't be inferred, unable to merge overloads
@overload
def f4(x: B) -> B: ...
elif maybe_true: # E: Name "maybe_true" is not defined
Expand All @@ -5880,10 +5881,11 @@ def g(bool_var: bool) -> None:
def f4(x): ...
reveal_type(f4(E())) # N: Revealed type is "__main__.E"
reveal_type(f4(B())) # E: No overload variant of "f4" matches argument type "B" \
# N: Possible overload variants: \
# N: def f4(x: A) -> A \
# N: def f4(x: E) -> E \
# N: Revealed type is "Any"
# N: Possible overload variants: \
# N: def f4(x: A) -> A \
# N: def f4(x: E) -> E \
# N: Revealed type is "Any"


[case testOverloadIfSkipUnknownExecution]
# flags: --always-true True
Expand All @@ -5901,14 +5903,14 @@ class D: ...

@overload # E: Single overload definition, multiple required
def f1(x: A) -> A: ...
if maybe_true: # E: Condition cannot be inferred, unable to merge overloads \
if maybe_true: # E: Condition can't be inferred, unable to merge overloads \
# E: Name "maybe_true" is not defined
@overload
def f1(x: B) -> B: ...
def f1(x): ...
reveal_type(f1(A())) # N: Revealed type is "__main__.A"

if maybe_true: # E: Condition cannot be inferred, unable to merge overloads \
if maybe_true: # E: Condition can't be inferred, unable to merge overloads \
# E: Name "maybe_true" is not defined
@overload
def f2(x: A) -> A: ...
Expand All @@ -5918,23 +5920,23 @@ def f2(x: B) -> B: ...
def f2(x: C) -> C: ...
def f2(x): ...
reveal_type(f2(A())) # E: No overload variant of "f2" matches argument type "A" \
# N: Possible overload variants: \
# N: def f2(x: B) -> B \
# N: def f2(x: C) -> C \
# N: Revealed type is "Any"
# N: Possible overload variants: \
# N: def f2(x: B) -> B \
# N: def f2(x: C) -> C \
# N: Revealed type is "Any"

if True:
@overload # E: Single overload definition, multiple required
def f3(x: A) -> A: ...
if maybe_true: # E: Condition cannot be inferred, unable to merge overloads \
if maybe_true: # E: Condition can't be inferred, unable to merge overloads \
# E: Name "maybe_true" is not defined
@overload
def f3(x: B) -> B: ...
def f3(x): ...
reveal_type(f3(A())) # N: Revealed type is "__main__.A"

if True:
if maybe_true: # E: Condition cannot be inferred, unable to merge overloads \
if maybe_true: # E: Condition can't be inferred, unable to merge overloads \
# E: Name "maybe_true" is not defined
@overload
def f4(x: A) -> A: ...
Expand All @@ -5944,10 +5946,10 @@ if True:
def f4(x: C) -> C: ...
def f4(x): ...
reveal_type(f4(A())) # E: No overload variant of "f4" matches argument type "A" \
# N: Possible overload variants: \
# N: def f4(x: B) -> B \
# N: def f4(x: C) -> C \
# N: Revealed type is "Any"
# N: Possible overload variants: \
# N: def f4(x: B) -> B \
# N: def f4(x: C) -> C \
# N: Revealed type is "Any"

[case testOverloadIfDontSkipUnrelatedOverload]
# flags: --always-true True
Expand Down Expand Up @@ -6187,16 +6189,16 @@ if False:
def f8(x): ...
reveal_type(f8(A())) # N: Revealed type is "__main__.A"
reveal_type(f8(C())) # E: No overload variant of "f8" matches argument type "C" \
# N: Possible overload variants: \
# N: def f8(x: A) -> A \
# N: def f8(x: B) -> B \
# N: Revealed type is "Any"
# N: Possible overload variants: \
# N: def f8(x: A) -> A \
# N: def f8(x: B) -> B \
# N: Revealed type is "Any"

if maybe_true: # E: Condition cannot be inferred, unable to merge overloads \
if maybe_true: # E: Condition can't be inferred, unable to merge overloads \
# E: Name "maybe_true" is not defined
@overload
def f9(x: A) -> A: ...
if another_maybe_true: # E: Condition cannot be inferred, unable to merge overloads \
if another_maybe_true: # E: Condition can't be inferred, unable to merge overloads \
# E: Name "another_maybe_true" is not defined
@overload
def f9(x: B) -> B: ...
Expand All @@ -6206,18 +6208,18 @@ def f9(x: C) -> C: ...
def f9(x: D) -> D: ...
def f9(x): ...
reveal_type(f9(A())) # E: No overload variant of "f9" matches argument type "A" \
# N: Possible overload variants: \
# N: def f9(x: C) -> C \
# N: def f9(x: D) -> D \
# N: Revealed type is "Any"
# N: Possible overload variants: \
# N: def f9(x: C) -> C \
# N: def f9(x: D) -> D \
# N: Revealed type is "Any"
reveal_type(f9(C())) # N: Revealed type is "__main__.C"

if True:
if maybe_true: # E: Condition cannot be inferred, unable to merge overloads \
if maybe_true: # E: Condition can't be inferred, unable to merge overloads \
# E: Name "maybe_true" is not defined
@overload
def f10(x: A) -> A: ...
if another_maybe_true: # E: Condition cannot be inferred, unable to merge overloads \
if another_maybe_true: # E: Condition can't be inferred, unable to merge overloads \
# E: Name "another_maybe_true" is not defined
@overload
def f10(x: B) -> B: ...
Expand All @@ -6227,10 +6229,10 @@ if True:
def f10(x: D) -> D: ...
def f10(x): ...
reveal_type(f10(A())) # E: No overload variant of "f10" matches argument type "A" \
# N: Possible overload variants: \
# N: def f10(x: C) -> C \
# N: def f10(x: D) -> D \
# N: Revealed type is "Any"
# N: Possible overload variants: \
# N: def f10(x: C) -> C \
# N: def f10(x: D) -> D \
# N: Revealed type is "Any"
reveal_type(f10(C())) # N: Revealed type is "__main__.C"

if some_var: # E: Name "some_var" is not defined
Expand All @@ -6251,6 +6253,7 @@ if True:
def f12(x: B) -> B: ...
def f12(x): ...
reveal_type(f12(A())) # N: Revealed type is "__main__.A"

[typing fixtures/typing-medium.pyi]

[case testOverloadIfUnconditionalFuncDef]
Expand Down Expand Up @@ -6406,7 +6409,7 @@ def f1(g: A) -> A: ...
if True:
@overload # E: Single overload definition, multiple required
def f1(g: B) -> B: ...
if maybe_true: # E: Condition cannot be inferred, unable to merge overloads \
if maybe_true: # E: Condition can't be inferred, unable to merge overloads \
# E: Name "maybe_true" is not defined
@overload
def f1(g: C) -> C: ...
Expand All @@ -6432,7 +6435,7 @@ if True:
def f3(g: B) -> B: ...
if True:
pass # Some other node
@overload # E: Name "f3" already defined on line 32 \
@overload # E: Name "f3" already defined on line 32 \
# E: An overloaded function outside a stub file must have an implementation
def f3(g: C) -> C: ...
@overload
Expand Down
4 changes: 2 additions & 2 deletions test-data/unit/check-recursive-types.test
Original file line number Diff line number Diff line change
Expand Up @@ -409,8 +409,8 @@ def local() -> None:
x: L
reveal_type(x) # N: Revealed type is "builtins.list[Union[builtins.int, Any]]"

S = Type[S] # E: Type[...] cannot contain another Type[...]
U = Type[Union[int, U]] # E: Type[...] cannot contain another Type[...]
S = Type[S] # E: Type[...] can't contain another Type[...]
U = Type[Union[int, U]] # E: Type[...] can't contain another Type[...]
x: U
reveal_type(x) # N: Revealed type is "Type[Any]"

Expand Down
Loading

0 comments on commit 61a7f3b

Please sign in to comment.