Skip to content

Commit d6566be

Browse files
JelleZijlstrailevkivskyi
authored andcommitted
Remove "builtins." from output for overloads and builtins.None (#5073)
Part of #5072. Overloads are probably the most common user-facing feature that still outputs "builtins" in error messages; this diff fixes them to use standard type formatting. I also change builtins.None to None, since builtins.None is a syntax error. A comment claimed that we use builtins.None to distinguish the type from the value, but I don't think there are a lot of contexts where that confusion is likely.
1 parent 57b57ad commit d6566be

27 files changed

+176
-167
lines changed

mypy/messages.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -748,10 +748,20 @@ def no_variant_matches_arguments(self, overload: Overloaded, arg_types: List[Typ
748748
context: Context) -> None:
749749
name = callable_name(overload)
750750
if name:
751-
self.fail('No overload variant of {} matches argument types {}'
752-
.format(name, arg_types), context)
751+
name_str = ' of {}'.format(name)
753752
else:
754-
self.fail('No overload variant matches argument types {}'.format(arg_types), context)
753+
name_str = ''
754+
arg_types_str = ', '.join(self.format(arg) for arg in arg_types)
755+
num_args = len(arg_types)
756+
if num_args == 0:
757+
self.fail('All overload variants{} require at least one argument'.format(name_str),
758+
context)
759+
elif num_args == 1:
760+
self.fail('No overload variant{} matches argument type {}'
761+
.format(name_str, arg_types_str), context)
762+
else:
763+
self.fail('No overload variant{} matches argument types {}'
764+
.format(name_str, arg_types_str), context)
755765

756766
def wrong_number_values_to_unpack(self, provided: int, expected: int,
757767
context: Context) -> None:

mypy/types.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1680,8 +1680,7 @@ def visit_any(self, t: AnyType) -> str:
16801680
return 'Any'
16811681

16821682
def visit_none_type(self, t: NoneTyp) -> str:
1683-
# Fully qualify to make this distinct from the None value.
1684-
return "builtins.None"
1683+
return "None"
16851684

16861685
def visit_uninhabited_type(self, t: UninhabitedType) -> str:
16871686
return "<nothing>"

test-data/unit/check-abstract.test

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,7 @@ B().f(1)
525525
a = B() # type: A
526526
a.f(1)
527527
a.f('')
528-
a.f(B()) # E: No overload variant of "f" of "A" matches argument types [foo.B]
528+
a.f(B()) # E: No overload variant of "f" of "A" matches argument type "B"
529529

530530
[case testOverloadedAbstractMethodWithAlternativeDecoratorOrder]
531531
from foo import *
@@ -552,7 +552,7 @@ B().f(1)
552552
a = B() # type: A
553553
a.f(1)
554554
a.f('')
555-
a.f(B()) # E: No overload variant of "f" of "A" matches argument types [foo.B]
555+
a.f(B()) # E: No overload variant of "f" of "A" matches argument type "B"
556556

557557
[case testOverloadedAbstractMethodVariantMissingDecorator1]
558558
from foo import *

test-data/unit/check-async-await.test

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -441,8 +441,8 @@ async def g() -> AsyncGenerator[int, None]:
441441
yield 'not an int' # E: Incompatible types in "yield" (actual type "str", expected type "int")
442442
# return without a value is fine
443443
return
444-
reveal_type(g) # E: Revealed type is 'def () -> typing.AsyncGenerator[builtins.int, builtins.None]'
445-
reveal_type(g()) # E: Revealed type is 'typing.AsyncGenerator[builtins.int, builtins.None]'
444+
reveal_type(g) # E: Revealed type is 'def () -> typing.AsyncGenerator[builtins.int, None]'
445+
reveal_type(g()) # E: Revealed type is 'typing.AsyncGenerator[builtins.int, None]'
446446

447447
async def h() -> None:
448448
async for item in g():
@@ -481,7 +481,7 @@ async def genfunc() -> AsyncGenerator[int, None]:
481481
async def user() -> None:
482482
gen = genfunc()
483483

484-
reveal_type(gen.__aiter__()) # E: Revealed type is 'typing.AsyncGenerator[builtins.int*, builtins.None]'
484+
reveal_type(gen.__aiter__()) # E: Revealed type is 'typing.AsyncGenerator[builtins.int*, None]'
485485

486486
reveal_type(await gen.__anext__()) # E: Revealed type is 'builtins.int*'
487487

test-data/unit/check-class-namedtuple.test

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ class HasNone(NamedTuple):
408408
x: int
409409
y: Optional[int] = None
410410

411-
reveal_type(HasNone(1)) # E: Revealed type is 'Tuple[builtins.int, Union[builtins.int, builtins.None], fallback=__main__.HasNone]'
411+
reveal_type(HasNone(1)) # E: Revealed type is 'Tuple[builtins.int, Union[builtins.int, None], fallback=__main__.HasNone]'
412412

413413
class Parameterized(NamedTuple):
414414
x: int
@@ -438,7 +438,7 @@ class HasNone(NamedTuple):
438438
x: int
439439
y: Optional[int] = None
440440

441-
reveal_type(HasNone(1)) # E: Revealed type is 'Tuple[builtins.int, Union[builtins.int, builtins.None], fallback=__main__.HasNone]'
441+
reveal_type(HasNone(1)) # E: Revealed type is 'Tuple[builtins.int, Union[builtins.int, None], fallback=__main__.HasNone]'
442442
HasNone(None) # E: Argument 1 to "HasNone" has incompatible type "None"; expected "int"
443443
HasNone(1, y=None)
444444
HasNone(1, y=2)
@@ -523,7 +523,7 @@ class Overloader(NamedTuple):
523523

524524
reveal_type(Overloader(1).method('string')) # E: Revealed type is 'builtins.str'
525525
reveal_type(Overloader(1).method(1)) # E: Revealed type is 'builtins.int'
526-
Overloader(1).method(('tuple',)) # E: No overload variant of "method" of "Overloader" matches argument types [Tuple[builtins.str]]
526+
Overloader(1).method(('tuple',)) # E: No overload variant of "method" of "Overloader" matches argument type "Tuple[str]"
527527

528528
[case testNewNamedTupleMethodInheritance]
529529
from typing import NamedTuple, TypeVar

test-data/unit/check-classes.test

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1224,9 +1224,9 @@ class D:
12241224
[builtins fixtures/bool.pyi]
12251225
[out]
12261226
main:5: error: Revealed type is 'Any'
1227-
main:5: error: No overload variant of "__get__" of "D" matches argument types [builtins.None, Type[__main__.A]]
1227+
main:5: error: No overload variant of "__get__" of "D" matches argument types "None", "Type[A]"
12281228
main:6: error: Revealed type is 'Any'
1229-
main:6: error: No overload variant of "__get__" of "D" matches argument types [__main__.A, Type[__main__.A]]
1229+
main:6: error: No overload variant of "__get__" of "D" matches argument types "A", "Type[A]"
12301230

12311231

12321232
[case testAccessingGenericNonDataDescriptor]
@@ -1324,7 +1324,7 @@ class D(Generic[T, V]):
13241324
[builtins fixtures/bool.pyi]
13251325
[out]
13261326
main:5: error: Revealed type is 'Any'
1327-
main:5: error: No overload variant of "__get__" of "D" matches argument types [builtins.None, Type[__main__.A]]
1327+
main:5: error: No overload variant of "__get__" of "D" matches argument types "None", "Type[A]"
13281328

13291329
[case testAccessingNonDataDescriptorSubclass]
13301330
from typing import Any
@@ -2094,7 +2094,7 @@ class C:
20942094
c = C(1)
20952095
c.a # E: "C" has no attribute "a"
20962096
C('', '')
2097-
C('') # E: No overload variant of "C" matches argument types [builtins.str]
2097+
C('') # E: No overload variant of "C" matches argument type "str"
20982098
[builtins fixtures/__new__.pyi]
20992099

21002100

@@ -2516,7 +2516,7 @@ def new(uc: Type[U]) -> U:
25162516
u = new(User)
25172517
[builtins fixtures/classmethod.pyi]
25182518
[out]
2519-
tmp/foo.pyi:16: error: No overload variant of "User" matches argument types [builtins.str]
2519+
tmp/foo.pyi:16: error: No overload variant of "User" matches argument type "str"
25202520
tmp/foo.pyi:17: error: Too many arguments for "foo" of "User"
25212521

25222522
[case testTypeUsingTypeCInUpperBound]
@@ -2712,7 +2712,7 @@ def f(a: int) -> None: pass
27122712
def mock() -> type: return User
27132713

27142714
f(User)
2715-
f(mock()) # E: No overload variant of "f" matches argument types [builtins.type]
2715+
f(mock()) # E: No overload variant of "f" matches argument type "type"
27162716
[builtins fixtures/classmethod.pyi]
27172717
[out]
27182718

@@ -2728,7 +2728,7 @@ def f(a: Type[User]) -> None: pass
27282728
@overload
27292729
def f(a: type) -> None: pass
27302730

2731-
f(3) # E: No overload variant of "f" matches argument types [builtins.int]
2731+
f(3) # E: No overload variant of "f" matches argument type "int"
27322732
[builtins fixtures/classmethod.pyi]
27332733
[out]
27342734

@@ -2745,7 +2745,7 @@ def f(a: Type[User]) -> None: pass
27452745
def f(a: int) -> None: pass
27462746

27472747
f(User)
2748-
f(User()) # E: No overload variant of "f" matches argument types [foo.User]
2748+
f(User()) # E: No overload variant of "f" matches argument type "User"
27492749
[builtins fixtures/classmethod.pyi]
27502750
[out]
27512751

@@ -2766,10 +2766,10 @@ def f(a: Type[B]) -> None: pass
27662766
@overload
27672767
def f(a: int) -> None: pass
27682768

2769-
f(A) # E: No overload variant of "f" matches argument types [def () -> foo.A]
2769+
f(A) # E: No overload variant of "f" matches argument type "Type[A]"
27702770
f(B)
27712771
f(C)
2772-
f(AType) # E: No overload variant of "f" matches argument types [Type[foo.A]]
2772+
f(AType) # E: No overload variant of "f" matches argument type "Type[A]"
27732773
f(BType)
27742774
f(CType)
27752775
[builtins fixtures/classmethod.pyi]

test-data/unit/check-expressions.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -866,7 +866,7 @@ from typing import overload
866866
a, b, c = None, None, None # type: (A, B, C)
867867
a[b]
868868
a[c]
869-
a[1] # E: No overload variant of "__getitem__" of "A" matches argument types [builtins.int]
869+
a[1] # E: No overload variant of "__getitem__" of "A" matches argument type "int"
870870

871871
i, s = None, None # type: (int, str)
872872
i = a[b]

test-data/unit/check-fastparse.test

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,9 @@ def f(a, # type: A
130130
**kwargs # type: F
131131
):
132132
reveal_type(a) # E: Revealed type is '__main__.A'
133-
reveal_type(b) # E: Revealed type is 'Union[__main__.B, builtins.None]'
133+
reveal_type(b) # E: Revealed type is 'Union[__main__.B, None]'
134134
reveal_type(args) # E: Revealed type is 'builtins.tuple[__main__.C]'
135-
reveal_type(d) # E: Revealed type is 'Union[__main__.D, builtins.None]'
135+
reveal_type(d) # E: Revealed type is 'Union[__main__.D, None]'
136136
reveal_type(e) # E: Revealed type is '__main__.E'
137137
reveal_type(kwargs) # E: Revealed type is 'builtins.dict[builtins.str, __main__.F]'
138138
[builtins fixtures/dict.pyi]
@@ -155,9 +155,9 @@ def f(a, # type: A
155155
):
156156
# type: (...) -> int
157157
reveal_type(a) # E: Revealed type is '__main__.A'
158-
reveal_type(b) # E: Revealed type is 'Union[__main__.B, builtins.None]'
158+
reveal_type(b) # E: Revealed type is 'Union[__main__.B, None]'
159159
reveal_type(args) # E: Revealed type is 'builtins.tuple[__main__.C]'
160-
reveal_type(d) # E: Revealed type is 'Union[__main__.D, builtins.None]'
160+
reveal_type(d) # E: Revealed type is 'Union[__main__.D, None]'
161161
reveal_type(e) # E: Revealed type is '__main__.E'
162162
reveal_type(kwargs) # E: Revealed type is 'builtins.dict[builtins.str, __main__.F]'
163163
return "not an int" # E: Incompatible return value type (got "str", expected "int")
@@ -197,7 +197,7 @@ def f(a, # type: A
197197
# kwargs not tested due to lack of 2.7 dict fixtures
198198
):
199199
reveal_type(a) # E: Revealed type is '__main__.A'
200-
reveal_type(b) # E: Revealed type is 'Union[__main__.B, builtins.None]'
200+
reveal_type(b) # E: Revealed type is 'Union[__main__.B, None]'
201201
reveal_type(args) # E: Revealed type is 'builtins.tuple[__main__.C]'
202202
[builtins fixtures/dict.pyi]
203203
[out]
@@ -215,7 +215,7 @@ def f(a, # type: A
215215
):
216216
# type: (...) -> int
217217
reveal_type(a) # E: Revealed type is '__main__.A'
218-
reveal_type(b) # E: Revealed type is 'Union[__main__.B, builtins.None]'
218+
reveal_type(b) # E: Revealed type is 'Union[__main__.B, None]'
219219
reveal_type(args) # E: Revealed type is 'builtins.tuple[__main__.C]'
220220
return "not an int" # E: Incompatible return value type (got "str", expected "int")
221221
[builtins fixtures/dict.pyi]

test-data/unit/check-functions.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,7 @@ class A:
534534
a = None # type: A
535535
a.g()
536536
a.g(B())
537-
a.g(a) # E: No overload variant matches argument types [foo.A]
537+
a.g(a) # E: No overload variant matches argument type "A"
538538

539539
[case testMethodAsDataAttributeInferredFromDynamicallyTypedMethod]
540540

test-data/unit/check-generics.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -960,7 +960,7 @@ U = Union[int]
960960
x: O
961961
y: U
962962

963-
reveal_type(x) # E: Revealed type is 'Union[builtins.int, builtins.None]'
963+
reveal_type(x) # E: Revealed type is 'Union[builtins.int, None]'
964964
reveal_type(y) # E: Revealed type is 'builtins.int'
965965

966966
U[int] # E: Bad number of arguments for type alias, expected: 0, given: 1

test-data/unit/check-incremental.test

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3798,9 +3798,9 @@ class A:
37983798

37993799
[builtins fixtures/list.pyi]
38003800
[out1]
3801-
main:2: error: Revealed type is 'def (x: Union[builtins.int, builtins.None]) -> a.a.A'
3801+
main:2: error: Revealed type is 'def (x: Union[builtins.int, None]) -> a.a.A'
38023802
[out2]
3803-
main:2: error: Revealed type is 'def (x: Union[builtins.int, builtins.None]) -> a.a.A'
3803+
main:2: error: Revealed type is 'def (x: Union[builtins.int, None]) -> a.a.A'
38043804

38053805
[case testAttrsIncrementalConverterManyStyles]
38063806
import a

test-data/unit/check-inference-context.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -867,7 +867,7 @@ T = TypeVar('T')
867867
def f(x: Union[T, List[int]]) -> Union[T, List[int]]: pass
868868
reveal_type(f(1)) # E: Revealed type is 'Union[builtins.int*, builtins.list[builtins.int]]'
869869
reveal_type(f([])) # E: Revealed type is 'builtins.list[builtins.int]'
870-
reveal_type(f(None)) # E: Revealed type is 'Union[builtins.None, builtins.list[builtins.int]]'
870+
reveal_type(f(None)) # E: Revealed type is 'Union[None, builtins.list[builtins.int]]'
871871
[builtins fixtures/list.pyi]
872872

873873
[case testUnionWithGenericTypeItemContextInMethod]

test-data/unit/check-inference.test

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1470,7 +1470,7 @@ def f(blocks: Any): # E: Name 'Any' is not defined
14701470
[case testSpecialCaseEmptyListInitialization2]
14711471
def f(blocks: object):
14721472
to_process = [] # E: Need type annotation for 'to_process'
1473-
to_process = list(blocks) # E: No overload variant of "list" matches argument types [builtins.object]
1473+
to_process = list(blocks) # E: No overload variant of "list" matches argument type "object"
14741474
[builtins fixtures/list.pyi]
14751475
[out]
14761476

@@ -1551,7 +1551,7 @@ if object():
15511551
[case testPartiallyInitializedVariableDoesNotEscapeScope1]
15521552
def f() -> None:
15531553
x = None
1554-
reveal_type(x) # E: Revealed type is 'builtins.None'
1554+
reveal_type(x) # E: Revealed type is 'None'
15551555
x = 1
15561556
[out]
15571557

@@ -2088,7 +2088,7 @@ def f() -> None:
20882088
x = 1
20892089

20902090
# TODO: "Any" could be a better type here to avoid multiple error messages
2091-
reveal_type(x) # E: Revealed type is 'builtins.None'
2091+
reveal_type(x) # E: Revealed type is 'None'
20922092

20932093
[case testLocalPartialTypesWithGlobalInitializedToNone2]
20942094
# flags: --local-partial-types
@@ -2099,7 +2099,7 @@ def f():
20992099
x = 1
21002100

21012101
# TODO: "Any" could be a better type here to avoid multiple error messages
2102-
reveal_type(x) # E: Revealed type is 'builtins.None'
2102+
reveal_type(x) # E: Revealed type is 'None'
21032103

21042104
[case testLocalPartialTypesWithGlobalInitializedToNone3]
21052105
# flags: --local-partial-types --no-strict-optional
@@ -2122,7 +2122,7 @@ def f() -> None:
21222122

21232123
x = ''
21242124
def g() -> None:
2125-
reveal_type(x) # E: Revealed type is 'Union[builtins.str, builtins.None]'
2125+
reveal_type(x) # E: Revealed type is 'Union[builtins.str, None]'
21262126

21272127
[case testLocalPartialTypesWithGlobalInitializedToNone4]
21282128
# flags: --local-partial-types --no-strict-optional
@@ -2133,7 +2133,7 @@ def f() -> None:
21332133

21342134
# TODO: This should probably be 'builtins.str', since there could be a
21352135
# call that causes a non-None value to be assigned
2136-
reveal_type(a) # E: Revealed type is 'builtins.None'
2136+
reveal_type(a) # E: Revealed type is 'None'
21372137
a = ''
21382138
reveal_type(a) # E: Revealed type is 'builtins.str'
21392139
[builtins fixtures/list.pyi]
@@ -2262,7 +2262,7 @@ class A:
22622262
class B(A):
22632263
x = None
22642264

2265-
reveal_type(B.x) # E: Revealed type is 'builtins.None'
2265+
reveal_type(B.x) # E: Revealed type is 'None'
22662266

22672267
[case testLocalPartialTypesWithInheritance2]
22682268
# flags: --local-partial-types --strict-optional
@@ -2298,8 +2298,8 @@ class C(B):
22982298
x = None
22992299

23002300
# TODO: Inferring None below is unsafe (https://github.com/python/mypy/issues/3208)
2301-
reveal_type(B.x) # E: Revealed type is 'builtins.None'
2302-
reveal_type(C.x) # E: Revealed type is 'builtins.None'
2301+
reveal_type(B.x) # E: Revealed type is 'None'
2302+
reveal_type(C.x) # E: Revealed type is 'None'
23032303

23042304
[case testLocalPartialTypesWithInheritance2-skip]
23052305
# flags: --local-partial-types
@@ -2316,7 +2316,7 @@ class B(A):
23162316
x = None
23172317
x = Y()
23182318

2319-
reveal_type(B.x) # E: revealed type is 'Union[__main__.Y, builtins.None]'
2319+
reveal_type(B.x) # E: revealed type is 'Union[__main__.Y, None]'
23202320

23212321
[case testLocalPartialTypesBinderSpecialCase]
23222322
# flags: --local-partial-types

0 commit comments

Comments
 (0)