Skip to content

Commit e32f35b

Browse files
Remove builtins. from error messages (#11522)
Closes #5072, follows up to #11490
1 parent fc609cd commit e32f35b

12 files changed

+98
-94
lines changed

mypy/checker.py

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4229,31 +4229,27 @@ def _check_for_truthy_type(self, t: Type, expr: Expression) -> None:
42294229
return
42304230

42314231
def format_expr_type() -> str:
4232+
typ = format_type(t)
42324233
if isinstance(expr, MemberExpr):
4233-
return f'Member "{expr.name}" has type "{t}"'
4234+
return f'Member "{expr.name}" has type {typ}'
42344235
elif isinstance(expr, RefExpr) and expr.fullname:
4235-
return f'"{expr.fullname}" has type "{t}"'
4236+
return f'"{expr.fullname}" has type {typ}'
42364237
elif isinstance(expr, CallExpr):
42374238
if isinstance(expr.callee, MemberExpr):
4238-
return f'"{expr.callee.name}" returns "{t}"'
4239+
return f'"{expr.callee.name}" returns {typ}'
42394240
elif isinstance(expr.callee, RefExpr) and expr.callee.fullname:
4240-
return f'"{expr.callee.fullname}" returns "{t}"'
4241-
return f'Call returns "{t}"'
4241+
return f'"{expr.callee.fullname}" returns {typ}'
4242+
return f'Call returns {typ}'
42424243
else:
4243-
return f'Expression has type "{t}"'
4244+
return f'Expression has type {typ}'
42444245

42454246
if isinstance(t, FunctionLike):
4246-
self.fail(message_registry.FUNCTION_ALWAYS_TRUE.format(t), expr)
4247+
self.fail(message_registry.FUNCTION_ALWAYS_TRUE.format(format_type(t)), expr)
42474248
elif isinstance(t, UnionType):
4248-
self.fail(
4249-
message_registry.TYPE_ALWAYS_TRUE_UNIONTYPE.format(format_expr_type()),
4250-
expr,
4251-
)
4249+
self.fail(message_registry.TYPE_ALWAYS_TRUE_UNIONTYPE.format(format_expr_type()),
4250+
expr)
42524251
else:
4253-
self.fail(
4254-
message_registry.TYPE_ALWAYS_TRUE.format(format_expr_type()),
4255-
expr,
4256-
)
4252+
self.fail(message_registry.TYPE_ALWAYS_TRUE.format(format_expr_type()), expr)
42574253

42584254
def find_type_equals_check(self, node: ComparisonExpr, expr_indices: List[int]
42594255
) -> Tuple[TypeMap, TypeMap]:

mypy/message_registry.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ def format(self, *args: object, **kwargs: object) -> "ErrorMessage":
131131
code=codes.TRUTHY_BOOL,
132132
)
133133
FUNCTION_ALWAYS_TRUE: Final = ErrorMessage(
134-
'Function "{}" could always be true in boolean context',
134+
'Function {} could always be true in boolean context',
135135
code=codes.TRUTHY_BOOL,
136136
)
137137
NOT_CALLABLE: Final = '{} not callable'
@@ -149,6 +149,9 @@ def format(self, *args: object, **kwargs: object) -> "ErrorMessage":
149149
# TypeVar
150150
INCOMPATIBLE_TYPEVAR_VALUE: Final = 'Value of type variable "{}" of {} cannot be {}'
151151
CANNOT_USE_TYPEVAR_AS_EXPRESSION: Final = 'Type variable "{}.{}" cannot be used as an expression'
152+
INVALID_TYPEVAR_AS_TYPEARG: Final = 'Type variable "{}" not valid as type argument value for "{}"'
153+
INVALID_TYPEVAR_ARG_BOUND: Final = 'Type argument {} of "{}" must be a subtype of {}'
154+
INVALID_TYPEVAR_ARG_VALUE: Final = 'Invalid type argument value for "{}"'
152155

153156
# Super
154157
TOO_MANY_ARGS_FOR_SUPER: Final = 'Too many arguments for "super"'

mypy/messages.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1151,11 +1151,12 @@ def yield_from_invalid_operand_type(self, expr: Type, context: Context) -> Type:
11511151
return AnyType(TypeOfAny.from_error)
11521152

11531153
def invalid_signature(self, func_type: Type, context: Context) -> None:
1154-
self.fail('Invalid signature "{}"'.format(func_type), context)
1154+
self.fail('Invalid signature {}'.format(format_type(func_type)), context)
11551155

11561156
def invalid_signature_for_special_method(
11571157
self, func_type: Type, context: Context, method_name: str) -> None:
1158-
self.fail('Invalid signature "{}" for "{}"'.format(func_type, method_name), context)
1158+
self.fail('Invalid signature {} for "{}"'.format(format_type(func_type), method_name),
1159+
context)
11591160

11601161
def reveal_type(self, typ: Type, context: Context) -> None:
11611162
self.note('Revealed type is "{}"'.format(typ), context)

mypy/semanal_typeargs.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from mypy.options import Options
2020
from mypy.errorcodes import ErrorCode
2121
from mypy import message_registry, errorcodes as codes
22+
from mypy.messages import format_type
2223

2324

2425
class TypeArgumentAnalyzer(MixedTraverserVisitor):
@@ -73,17 +74,19 @@ def visit_instance(self, t: Instance) -> None:
7374
if isinstance(arg, TypeVarType):
7475
arg_values = arg.values
7576
if not arg_values:
76-
self.fail('Type variable "{}" not valid as type '
77-
'argument value for "{}"'.format(
78-
arg.name, info.name), t, code=codes.TYPE_VAR)
77+
self.fail(
78+
message_registry.INVALID_TYPEVAR_AS_TYPEARG.format(
79+
arg.name, info.name),
80+
t, code=codes.TYPE_VAR)
7981
continue
8082
else:
8183
arg_values = [arg]
8284
self.check_type_var_values(info, arg_values, tvar.name, tvar.values, i + 1, t)
8385
if not is_subtype(arg, tvar.upper_bound):
84-
self.fail('Type argument "{}" of "{}" must be '
85-
'a subtype of "{}"'.format(
86-
arg, info.name, tvar.upper_bound), t, code=codes.TYPE_VAR)
86+
self.fail(
87+
message_registry.INVALID_TYPEVAR_ARG_BOUND.format(
88+
format_type(arg), info.name, format_type(tvar.upper_bound)),
89+
t, code=codes.TYPE_VAR)
8790
super().visit_instance(t)
8891

8992
def check_type_var_values(self, type: TypeInfo, actuals: List[Type], arg_name: str,
@@ -93,8 +96,9 @@ def check_type_var_values(self, type: TypeInfo, actuals: List[Type], arg_name: s
9396
not any(is_same_type(actual, value)
9497
for value in valids)):
9598
if len(actuals) > 1 or not isinstance(actual, Instance):
96-
self.fail('Invalid type argument value for "{}"'.format(
97-
type.name), context, code=codes.TYPE_VAR)
99+
self.fail(
100+
message_registry.INVALID_TYPEVAR_ARG_VALUE.format(type.name),
101+
context, code=codes.TYPE_VAR)
98102
else:
99103
class_name = '"{}"'.format(type.name)
100104
actual_type_name = '"{}"'.format(actual.type.name)

test-data/unit/check-bound.test

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class G(Generic[T]):
3939

4040
v = None # type: G[A]
4141
w = None # type: G[B]
42-
x = None # type: G[str] # E: Type argument "builtins.str" of "G" must be a subtype of "__main__.A"
42+
x = None # type: G[str] # E: Type argument "str" of "G" must be a subtype of "A"
4343
y = G('a') # E: Value of type variable "T" of "G" cannot be "str"
4444
z = G(A())
4545
z = G(B())
@@ -93,9 +93,9 @@ TA = TypeVar('TA', bound=A)
9393

9494
class C(Generic[TA]): pass
9595
class D0(C[TA], Generic[TA]): pass
96-
class D1(C[T], Generic[T]): pass # E: Type argument "T`1" of "C" must be a subtype of "__main__.A"
96+
class D1(C[T], Generic[T]): pass # E: Type argument "T" of "C" must be a subtype of "A"
9797
class D2(C[A]): pass
98-
class D3(C[str]): pass # E: Type argument "builtins.str" of "C" must be a subtype of "__main__.A"
98+
class D3(C[str]): pass # E: Type argument "str" of "C" must be a subtype of "A"
9999

100100

101101
-- Using information from upper bounds

test-data/unit/check-classes.test

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2024,8 +2024,8 @@ class B:
20242024
class C:
20252025
def __radd__(self, other, oops) -> int: ...
20262026
[out]
2027-
tmp/foo.pyi:3: error: Invalid signature "def (foo.B) -> foo.A"
2028-
tmp/foo.pyi:5: error: Invalid signature "def (foo.C, Any, Any) -> builtins.int"
2027+
tmp/foo.pyi:3: error: Invalid signature "Callable[[B], A]"
2028+
tmp/foo.pyi:5: error: Invalid signature "Callable[[C, Any, Any], int]"
20292029

20302030
[case testReverseOperatorOrderingCase1]
20312031
class A:
@@ -2627,8 +2627,8 @@ class C:
26272627
class D:
26282628
def __getattribute__(self, x: str) -> None: pass
26292629
[out]
2630-
main:4: error: Invalid signature "def (__main__.B, __main__.A) -> __main__.B" for "__getattribute__"
2631-
main:6: error: Invalid signature "def (__main__.C, builtins.str, builtins.str) -> __main__.C" for "__getattribute__"
2630+
main:4: error: Invalid signature "Callable[[B, A], B]" for "__getattribute__"
2631+
main:6: error: Invalid signature "Callable[[C, str, str], C]" for "__getattribute__"
26322632

26332633
[case testGetattr]
26342634

@@ -2704,8 +2704,8 @@ class C:
27042704
class D:
27052705
def __getattr__(self, x: str) -> None: pass
27062706
[out]
2707-
main:4: error: Invalid signature "def (__main__.B, __main__.A) -> __main__.B" for "__getattr__"
2708-
main:6: error: Invalid signature "def (__main__.C, builtins.str, builtins.str) -> __main__.C" for "__getattr__"
2707+
main:4: error: Invalid signature "Callable[[B, A], B]" for "__getattr__"
2708+
main:6: error: Invalid signature "Callable[[C, str, str], C]" for "__getattr__"
27092709

27102710
[case testSetattr]
27112711
from typing import Union, Any
@@ -2729,7 +2729,7 @@ c = C()
27292729
c.fail = 4 # E: Incompatible types in assignment (expression has type "int", variable has type "str")
27302730

27312731
class D:
2732-
__setattr__ = 'hello' # E: Invalid signature "builtins.str" for "__setattr__"
2732+
__setattr__ = 'hello' # E: Invalid signature "str" for "__setattr__"
27332733

27342734
d = D()
27352735
d.crash = 4 # E: "D" has no attribute "crash"
@@ -2754,12 +2754,12 @@ s.fail = 'fail' # E: Incompatible types in assignment (expression has type "str
27542754
from typing import Any
27552755

27562756
class Test:
2757-
def __setattr__() -> None: ... # E: Method must have at least one argument # E: Invalid signature "def ()" for "__setattr__"
2757+
def __setattr__() -> None: ... # E: Method must have at least one argument # E: Invalid signature "Callable[[], None]" for "__setattr__"
27582758
t = Test()
27592759
t.crash = 'test' # E: "Test" has no attribute "crash"
27602760

27612761
class A:
2762-
def __setattr__(self): ... # E: Invalid signature "def (__main__.A) -> Any" for "__setattr__"
2762+
def __setattr__(self): ... # E: Invalid signature "Callable[[A], Any]" for "__setattr__"
27632763
a = A()
27642764
a.test = 4 # E: "A" has no attribute "test"
27652765

@@ -2769,7 +2769,7 @@ b = B()
27692769
b.integer = 5
27702770

27712771
class C:
2772-
def __setattr__(self, name: int, value: int) -> None: ... # E: Invalid signature "def (__main__.C, builtins.int, builtins.int)" for "__setattr__"
2772+
def __setattr__(self, name: int, value: int) -> None: ... # E: Invalid signature "Callable[[C, int, int], None]" for "__setattr__"
27732773
c = C()
27742774
c.check = 13
27752775

@@ -5500,8 +5500,8 @@ A = G
55005500
x: A[B[int]] # E
55015501
B = G
55025502
[out]
5503-
main:8:4: error: Type argument "__main__.G[builtins.int]" of "G" must be a subtype of "builtins.str"
5504-
main:8:6: error: Type argument "builtins.int" of "G" must be a subtype of "builtins.str"
5503+
main:8:4: error: Type argument "G[int]" of "G" must be a subtype of "str"
5504+
main:8:6: error: Type argument "int" of "G" must be a subtype of "str"
55055505

55065506
[case testExtremeForwardReferencing]
55075507
from typing import TypeVar, Generic

test-data/unit/check-errorcodes.test

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ class D(Generic[S]): pass
440440
class E(Generic[S, T]): pass
441441

442442
x: C[object] # E: Value of type variable "T" of "C" cannot be "object" [type-var]
443-
y: D[int] # E: Type argument "builtins.int" of "D" must be a subtype of "builtins.str" [type-var]
443+
y: D[int] # E: Type argument "int" of "D" must be a subtype of "str" [type-var]
444444
z: D[int, int] # E: "D" expects 1 type argument, but 2 given [type-arg]
445445

446446
def h(a: TT, s: S) -> None:
@@ -854,7 +854,7 @@ class Foo:
854854
pass
855855

856856
foo = Foo()
857-
if foo: # E: "__main__.foo" has type "__main__.Foo" which does not implement __bool__ or __len__ so it could always be true in boolean context [truthy-bool]
857+
if foo: # E: "__main__.foo" has type "Foo" which does not implement __bool__ or __len__ so it could always be true in boolean context [truthy-bool]
858858
pass
859859

860860
zero = 0
@@ -880,18 +880,18 @@ if not good_union:
880880
pass
881881

882882
bad_union: Union[Foo, object] = Foo()
883-
if bad_union: # E: "__main__.bad_union" has type "Union[__main__.Foo, builtins.object]" of which no members implement __bool__ or __len__ so it could always be true in boolean context [truthy-bool]
883+
if bad_union: # E: "__main__.bad_union" has type "Union[Foo, object]" of which no members implement __bool__ or __len__ so it could always be true in boolean context [truthy-bool]
884884
pass
885-
if not bad_union: # E: "__main__.bad_union" has type "builtins.object" which does not implement __bool__ or __len__ so it could always be true in boolean context [truthy-bool]
885+
if not bad_union: # E: "__main__.bad_union" has type "object" which does not implement __bool__ or __len__ so it could always be true in boolean context [truthy-bool]
886886
pass
887887

888888
def f():
889889
pass
890-
if f: # E: Function "def () -> Any" could always be true in boolean context [truthy-bool]
890+
if f: # E: Function "Callable[[], Any]" could always be true in boolean context [truthy-bool]
891891
pass
892-
if not f: # E: Function "def () -> Any" could always be true in boolean context [truthy-bool]
892+
if not f: # E: Function "Callable[[], Any]" could always be true in boolean context [truthy-bool]
893893
pass
894-
conditional_result = 'foo' if f else 'bar' # E: Function "def () -> Any" could always be true in boolean context [truthy-bool]
894+
conditional_result = 'foo' if f else 'bar' # E: Function "Callable[[], Any]" could always be true in boolean context [truthy-bool]
895895

896896
lst: List[int] = []
897897
if lst:

test-data/unit/check-modules.test

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2000,7 +2000,7 @@ reveal_type(has_getattr.any_attribute)
20002000
def __getattr__(x: int, y: str) -> str: ...
20012001

20022002
[out]
2003-
tmp/has_getattr.pyi:1: error: Invalid signature "def (builtins.int, builtins.str) -> builtins.str" for "__getattr__"
2003+
tmp/has_getattr.pyi:1: error: Invalid signature "Callable[[int, str], str]" for "__getattr__"
20042004
main:3: note: Revealed type is "builtins.str"
20052005

20062006
[builtins fixtures/module.pyi]
@@ -2014,7 +2014,7 @@ reveal_type(has_getattr.any_attribute)
20142014
__getattr__ = 3
20152015

20162016
[out]
2017-
tmp/has_getattr.pyi:1: error: Invalid signature "builtins.int" for "__getattr__"
2017+
tmp/has_getattr.pyi:1: error: Invalid signature "int" for "__getattr__"
20182018
main:3: note: Revealed type is "Any"
20192019

20202020
[builtins fixtures/module.pyi]
@@ -2141,7 +2141,7 @@ def make_getattr_bad() -> Callable[[], int]: ...
21412141
__getattr__ = make_getattr_bad()
21422142

21432143
[out]
2144-
tmp/non_stub.py:4: error: Invalid signature "def () -> builtins.int" for "__getattr__"
2144+
tmp/non_stub.py:4: error: Invalid signature "Callable[[], int]" for "__getattr__"
21452145
main:3: note: Revealed type is "builtins.int"
21462146

21472147
[case testModuleLevelGetattrImportedGood]
@@ -2167,7 +2167,7 @@ from has_getattr import __getattr__
21672167
def __getattr__() -> int: ...
21682168

21692169
[out]
2170-
tmp/has_getattr.py:1: error: Invalid signature "def () -> builtins.int" for "__getattr__"
2170+
tmp/has_getattr.py:1: error: Invalid signature "Callable[[], int]" for "__getattr__"
21712171
main:3: note: Revealed type is "builtins.int"
21722172

21732173
[builtins fixtures/module.pyi]

test-data/unit/check-namedtuple.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -576,7 +576,7 @@ T = TypeVar('T', bound='M')
576576
class G(Generic[T]):
577577
x: T
578578

579-
yb: G[int] # E: Type argument "builtins.int" of "G" must be a subtype of "Tuple[builtins.int, fallback=__main__.M]"
579+
yb: G[int] # E: Type argument "int" of "G" must be a subtype of "M"
580580
yg: G[M]
581581
reveal_type(G[M]().x.x) # N: Revealed type is "builtins.int"
582582
reveal_type(G[M]().x[0]) # N: Revealed type is "builtins.int"

0 commit comments

Comments
 (0)