Skip to content

Commit

Permalink
Remove "builtins." from output for overloads and builtins.None (pytho…
Browse files Browse the repository at this point in the history
…n#5073)

Part of python#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.
  • Loading branch information
JelleZijlstra authored and ilevkivskyi committed May 18, 2018
1 parent 57b57ad commit d6566be
Show file tree
Hide file tree
Showing 27 changed files with 176 additions and 167 deletions.
16 changes: 13 additions & 3 deletions mypy/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -748,10 +748,20 @@ def no_variant_matches_arguments(self, overload: Overloaded, arg_types: List[Typ
context: Context) -> None:
name = callable_name(overload)
if name:
self.fail('No overload variant of {} matches argument types {}'
.format(name, arg_types), context)
name_str = ' of {}'.format(name)
else:
self.fail('No overload variant matches argument types {}'.format(arg_types), context)
name_str = ''
arg_types_str = ', '.join(self.format(arg) for arg in arg_types)
num_args = len(arg_types)
if num_args == 0:
self.fail('All overload variants{} require at least one argument'.format(name_str),
context)
elif num_args == 1:
self.fail('No overload variant{} matches argument type {}'
.format(name_str, arg_types_str), context)
else:
self.fail('No overload variant{} matches argument types {}'
.format(name_str, arg_types_str), context)

def wrong_number_values_to_unpack(self, provided: int, expected: int,
context: Context) -> None:
Expand Down
3 changes: 1 addition & 2 deletions mypy/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -1680,8 +1680,7 @@ def visit_any(self, t: AnyType) -> str:
return 'Any'

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

def visit_uninhabited_type(self, t: UninhabitedType) -> str:
return "<nothing>"
Expand Down
4 changes: 2 additions & 2 deletions test-data/unit/check-abstract.test
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,7 @@ B().f(1)
a = B() # type: A
a.f(1)
a.f('')
a.f(B()) # E: No overload variant of "f" of "A" matches argument types [foo.B]
a.f(B()) # E: No overload variant of "f" of "A" matches argument type "B"

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

[case testOverloadedAbstractMethodVariantMissingDecorator1]
from foo import *
Expand Down
6 changes: 3 additions & 3 deletions test-data/unit/check-async-await.test
Original file line number Diff line number Diff line change
Expand Up @@ -441,8 +441,8 @@ async def g() -> AsyncGenerator[int, None]:
yield 'not an int' # E: Incompatible types in "yield" (actual type "str", expected type "int")
# return without a value is fine
return
reveal_type(g) # E: Revealed type is 'def () -> typing.AsyncGenerator[builtins.int, builtins.None]'
reveal_type(g()) # E: Revealed type is 'typing.AsyncGenerator[builtins.int, builtins.None]'
reveal_type(g) # E: Revealed type is 'def () -> typing.AsyncGenerator[builtins.int, None]'
reveal_type(g()) # E: Revealed type is 'typing.AsyncGenerator[builtins.int, None]'

async def h() -> None:
async for item in g():
Expand Down Expand Up @@ -481,7 +481,7 @@ async def genfunc() -> AsyncGenerator[int, None]:
async def user() -> None:
gen = genfunc()

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

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

Expand Down
6 changes: 3 additions & 3 deletions test-data/unit/check-class-namedtuple.test
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ class HasNone(NamedTuple):
x: int
y: Optional[int] = None

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

class Parameterized(NamedTuple):
x: int
Expand Down Expand Up @@ -438,7 +438,7 @@ class HasNone(NamedTuple):
x: int
y: Optional[int] = None

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

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

[case testNewNamedTupleMethodInheritance]
from typing import NamedTuple, TypeVar
Expand Down
20 changes: 10 additions & 10 deletions test-data/unit/check-classes.test
Original file line number Diff line number Diff line change
Expand Up @@ -1224,9 +1224,9 @@ class D:
[builtins fixtures/bool.pyi]
[out]
main:5: error: Revealed type is 'Any'
main:5: error: No overload variant of "__get__" of "D" matches argument types [builtins.None, Type[__main__.A]]
main:5: error: No overload variant of "__get__" of "D" matches argument types "None", "Type[A]"
main:6: error: Revealed type is 'Any'
main:6: error: No overload variant of "__get__" of "D" matches argument types [__main__.A, Type[__main__.A]]
main:6: error: No overload variant of "__get__" of "D" matches argument types "A", "Type[A]"


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

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


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

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

f(User)
f(mock()) # E: No overload variant of "f" matches argument types [builtins.type]
f(mock()) # E: No overload variant of "f" matches argument type "type"
[builtins fixtures/classmethod.pyi]
[out]

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

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

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

f(User)
f(User()) # E: No overload variant of "f" matches argument types [foo.User]
f(User()) # E: No overload variant of "f" matches argument type "User"
[builtins fixtures/classmethod.pyi]
[out]

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

f(A) # E: No overload variant of "f" matches argument types [def () -> foo.A]
f(A) # E: No overload variant of "f" matches argument type "Type[A]"
f(B)
f(C)
f(AType) # E: No overload variant of "f" matches argument types [Type[foo.A]]
f(AType) # E: No overload variant of "f" matches argument type "Type[A]"
f(BType)
f(CType)
[builtins fixtures/classmethod.pyi]
Expand Down
2 changes: 1 addition & 1 deletion test-data/unit/check-expressions.test
Original file line number Diff line number Diff line change
Expand Up @@ -866,7 +866,7 @@ from typing import overload
a, b, c = None, None, None # type: (A, B, C)
a[b]
a[c]
a[1] # E: No overload variant of "__getitem__" of "A" matches argument types [builtins.int]
a[1] # E: No overload variant of "__getitem__" of "A" matches argument type "int"

i, s = None, None # type: (int, str)
i = a[b]
Expand Down
12 changes: 6 additions & 6 deletions test-data/unit/check-fastparse.test
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,9 @@ def f(a, # type: A
**kwargs # type: F
):
reveal_type(a) # E: Revealed type is '__main__.A'
reveal_type(b) # E: Revealed type is 'Union[__main__.B, builtins.None]'
reveal_type(b) # E: Revealed type is 'Union[__main__.B, None]'
reveal_type(args) # E: Revealed type is 'builtins.tuple[__main__.C]'
reveal_type(d) # E: Revealed type is 'Union[__main__.D, builtins.None]'
reveal_type(d) # E: Revealed type is 'Union[__main__.D, None]'
reveal_type(e) # E: Revealed type is '__main__.E'
reveal_type(kwargs) # E: Revealed type is 'builtins.dict[builtins.str, __main__.F]'
[builtins fixtures/dict.pyi]
Expand All @@ -155,9 +155,9 @@ def f(a, # type: A
):
# type: (...) -> int
reveal_type(a) # E: Revealed type is '__main__.A'
reveal_type(b) # E: Revealed type is 'Union[__main__.B, builtins.None]'
reveal_type(b) # E: Revealed type is 'Union[__main__.B, None]'
reveal_type(args) # E: Revealed type is 'builtins.tuple[__main__.C]'
reveal_type(d) # E: Revealed type is 'Union[__main__.D, builtins.None]'
reveal_type(d) # E: Revealed type is 'Union[__main__.D, None]'
reveal_type(e) # E: Revealed type is '__main__.E'
reveal_type(kwargs) # E: Revealed type is 'builtins.dict[builtins.str, __main__.F]'
return "not an int" # E: Incompatible return value type (got "str", expected "int")
Expand Down Expand Up @@ -197,7 +197,7 @@ def f(a, # type: A
# kwargs not tested due to lack of 2.7 dict fixtures
):
reveal_type(a) # E: Revealed type is '__main__.A'
reveal_type(b) # E: Revealed type is 'Union[__main__.B, builtins.None]'
reveal_type(b) # E: Revealed type is 'Union[__main__.B, None]'
reveal_type(args) # E: Revealed type is 'builtins.tuple[__main__.C]'
[builtins fixtures/dict.pyi]
[out]
Expand All @@ -215,7 +215,7 @@ def f(a, # type: A
):
# type: (...) -> int
reveal_type(a) # E: Revealed type is '__main__.A'
reveal_type(b) # E: Revealed type is 'Union[__main__.B, builtins.None]'
reveal_type(b) # E: Revealed type is 'Union[__main__.B, None]'
reveal_type(args) # E: Revealed type is 'builtins.tuple[__main__.C]'
return "not an int" # E: Incompatible return value type (got "str", expected "int")
[builtins fixtures/dict.pyi]
Expand Down
2 changes: 1 addition & 1 deletion test-data/unit/check-functions.test
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ class A:
a = None # type: A
a.g()
a.g(B())
a.g(a) # E: No overload variant matches argument types [foo.A]
a.g(a) # E: No overload variant matches argument type "A"

[case testMethodAsDataAttributeInferredFromDynamicallyTypedMethod]

Expand Down
2 changes: 1 addition & 1 deletion test-data/unit/check-generics.test
Original file line number Diff line number Diff line change
Expand Up @@ -960,7 +960,7 @@ U = Union[int]
x: O
y: U

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

U[int] # E: Bad number of arguments for type alias, expected: 0, given: 1
Expand Down
4 changes: 2 additions & 2 deletions test-data/unit/check-incremental.test
Original file line number Diff line number Diff line change
Expand Up @@ -3798,9 +3798,9 @@ class A:

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

[case testAttrsIncrementalConverterManyStyles]
import a
Expand Down
2 changes: 1 addition & 1 deletion test-data/unit/check-inference-context.test
Original file line number Diff line number Diff line change
Expand Up @@ -867,7 +867,7 @@ T = TypeVar('T')
def f(x: Union[T, List[int]]) -> Union[T, List[int]]: pass
reveal_type(f(1)) # E: Revealed type is 'Union[builtins.int*, builtins.list[builtins.int]]'
reveal_type(f([])) # E: Revealed type is 'builtins.list[builtins.int]'
reveal_type(f(None)) # E: Revealed type is 'Union[builtins.None, builtins.list[builtins.int]]'
reveal_type(f(None)) # E: Revealed type is 'Union[None, builtins.list[builtins.int]]'
[builtins fixtures/list.pyi]

[case testUnionWithGenericTypeItemContextInMethod]
Expand Down
20 changes: 10 additions & 10 deletions test-data/unit/check-inference.test
Original file line number Diff line number Diff line change
Expand Up @@ -1470,7 +1470,7 @@ def f(blocks: Any): # E: Name 'Any' is not defined
[case testSpecialCaseEmptyListInitialization2]
def f(blocks: object):
to_process = [] # E: Need type annotation for 'to_process'
to_process = list(blocks) # E: No overload variant of "list" matches argument types [builtins.object]
to_process = list(blocks) # E: No overload variant of "list" matches argument type "object"
[builtins fixtures/list.pyi]
[out]

Expand Down Expand Up @@ -1551,7 +1551,7 @@ if object():
[case testPartiallyInitializedVariableDoesNotEscapeScope1]
def f() -> None:
x = None
reveal_type(x) # E: Revealed type is 'builtins.None'
reveal_type(x) # E: Revealed type is 'None'
x = 1
[out]

Expand Down Expand Up @@ -2088,7 +2088,7 @@ def f() -> None:
x = 1

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

[case testLocalPartialTypesWithGlobalInitializedToNone2]
# flags: --local-partial-types
Expand All @@ -2099,7 +2099,7 @@ def f():
x = 1

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

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

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

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

# TODO: This should probably be 'builtins.str', since there could be a
# call that causes a non-None value to be assigned
reveal_type(a) # E: Revealed type is 'builtins.None'
reveal_type(a) # E: Revealed type is 'None'
a = ''
reveal_type(a) # E: Revealed type is 'builtins.str'
[builtins fixtures/list.pyi]
Expand Down Expand Up @@ -2262,7 +2262,7 @@ class A:
class B(A):
x = None

reveal_type(B.x) # E: Revealed type is 'builtins.None'
reveal_type(B.x) # E: Revealed type is 'None'

[case testLocalPartialTypesWithInheritance2]
# flags: --local-partial-types --strict-optional
Expand Down Expand Up @@ -2298,8 +2298,8 @@ class C(B):
x = None

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

[case testLocalPartialTypesWithInheritance2-skip]
# flags: --local-partial-types
Expand All @@ -2316,7 +2316,7 @@ class B(A):
x = None
x = Y()

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

[case testLocalPartialTypesBinderSpecialCase]
# flags: --local-partial-types
Expand Down
Loading

0 comments on commit d6566be

Please sign in to comment.