Skip to content

Commit

Permalink
Remove builtins. from error messages (#11522)
Browse files Browse the repository at this point in the history
Closes #5072, follows up to #11490
  • Loading branch information
97littleleaf11 authored Nov 11, 2021
1 parent fc609cd commit e32f35b
Show file tree
Hide file tree
Showing 12 changed files with 98 additions and 94 deletions.
26 changes: 11 additions & 15 deletions mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -4229,31 +4229,27 @@ def _check_for_truthy_type(self, t: Type, expr: Expression) -> None:
return

def format_expr_type() -> str:
typ = format_type(t)
if isinstance(expr, MemberExpr):
return f'Member "{expr.name}" has type "{t}"'
return f'Member "{expr.name}" has type {typ}'
elif isinstance(expr, RefExpr) and expr.fullname:
return f'"{expr.fullname}" has type "{t}"'
return f'"{expr.fullname}" has type {typ}'
elif isinstance(expr, CallExpr):
if isinstance(expr.callee, MemberExpr):
return f'"{expr.callee.name}" returns "{t}"'
return f'"{expr.callee.name}" returns {typ}'
elif isinstance(expr.callee, RefExpr) and expr.callee.fullname:
return f'"{expr.callee.fullname}" returns "{t}"'
return f'Call returns "{t}"'
return f'"{expr.callee.fullname}" returns {typ}'
return f'Call returns {typ}'
else:
return f'Expression has type "{t}"'
return f'Expression has type {typ}'

if isinstance(t, FunctionLike):
self.fail(message_registry.FUNCTION_ALWAYS_TRUE.format(t), expr)
self.fail(message_registry.FUNCTION_ALWAYS_TRUE.format(format_type(t)), expr)
elif isinstance(t, UnionType):
self.fail(
message_registry.TYPE_ALWAYS_TRUE_UNIONTYPE.format(format_expr_type()),
expr,
)
self.fail(message_registry.TYPE_ALWAYS_TRUE_UNIONTYPE.format(format_expr_type()),
expr)
else:
self.fail(
message_registry.TYPE_ALWAYS_TRUE.format(format_expr_type()),
expr,
)
self.fail(message_registry.TYPE_ALWAYS_TRUE.format(format_expr_type()), expr)

def find_type_equals_check(self, node: ComparisonExpr, expr_indices: List[int]
) -> Tuple[TypeMap, TypeMap]:
Expand Down
5 changes: 4 additions & 1 deletion mypy/message_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def format(self, *args: object, **kwargs: object) -> "ErrorMessage":
code=codes.TRUTHY_BOOL,
)
FUNCTION_ALWAYS_TRUE: Final = ErrorMessage(
'Function "{}" could always be true in boolean context',
'Function {} could always be true in boolean context',
code=codes.TRUTHY_BOOL,
)
NOT_CALLABLE: Final = '{} not callable'
Expand All @@ -149,6 +149,9 @@ def format(self, *args: object, **kwargs: object) -> "ErrorMessage":
# TypeVar
INCOMPATIBLE_TYPEVAR_VALUE: Final = 'Value of type variable "{}" of {} cannot be {}'
CANNOT_USE_TYPEVAR_AS_EXPRESSION: Final = 'Type variable "{}.{}" cannot be used as an expression'
INVALID_TYPEVAR_AS_TYPEARG: Final = 'Type variable "{}" not valid as type argument value for "{}"'
INVALID_TYPEVAR_ARG_BOUND: Final = 'Type argument {} of "{}" must be a subtype of {}'
INVALID_TYPEVAR_ARG_VALUE: Final = 'Invalid type argument value for "{}"'

# Super
TOO_MANY_ARGS_FOR_SUPER: Final = 'Too many arguments for "super"'
Expand Down
5 changes: 3 additions & 2 deletions mypy/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -1151,11 +1151,12 @@ def yield_from_invalid_operand_type(self, expr: Type, context: Context) -> Type:
return AnyType(TypeOfAny.from_error)

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

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

def reveal_type(self, typ: Type, context: Context) -> None:
self.note('Revealed type is "{}"'.format(typ), context)
Expand Down
20 changes: 12 additions & 8 deletions mypy/semanal_typeargs.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from mypy.options import Options
from mypy.errorcodes import ErrorCode
from mypy import message_registry, errorcodes as codes
from mypy.messages import format_type


class TypeArgumentAnalyzer(MixedTraverserVisitor):
Expand Down Expand Up @@ -73,17 +74,19 @@ def visit_instance(self, t: Instance) -> None:
if isinstance(arg, TypeVarType):
arg_values = arg.values
if not arg_values:
self.fail('Type variable "{}" not valid as type '
'argument value for "{}"'.format(
arg.name, info.name), t, code=codes.TYPE_VAR)
self.fail(
message_registry.INVALID_TYPEVAR_AS_TYPEARG.format(
arg.name, info.name),
t, code=codes.TYPE_VAR)
continue
else:
arg_values = [arg]
self.check_type_var_values(info, arg_values, tvar.name, tvar.values, i + 1, t)
if not is_subtype(arg, tvar.upper_bound):
self.fail('Type argument "{}" of "{}" must be '
'a subtype of "{}"'.format(
arg, info.name, tvar.upper_bound), t, code=codes.TYPE_VAR)
self.fail(
message_registry.INVALID_TYPEVAR_ARG_BOUND.format(
format_type(arg), info.name, format_type(tvar.upper_bound)),
t, code=codes.TYPE_VAR)
super().visit_instance(t)

def check_type_var_values(self, type: TypeInfo, actuals: List[Type], arg_name: str,
Expand All @@ -93,8 +96,9 @@ def check_type_var_values(self, type: TypeInfo, actuals: List[Type], arg_name: s
not any(is_same_type(actual, value)
for value in valids)):
if len(actuals) > 1 or not isinstance(actual, Instance):
self.fail('Invalid type argument value for "{}"'.format(
type.name), context, code=codes.TYPE_VAR)
self.fail(
message_registry.INVALID_TYPEVAR_ARG_VALUE.format(type.name),
context, code=codes.TYPE_VAR)
else:
class_name = '"{}"'.format(type.name)
actual_type_name = '"{}"'.format(actual.type.name)
Expand Down
6 changes: 3 additions & 3 deletions test-data/unit/check-bound.test
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class G(Generic[T]):

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

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


-- Using information from upper bounds
Expand Down
24 changes: 12 additions & 12 deletions test-data/unit/check-classes.test
Original file line number Diff line number Diff line change
Expand Up @@ -2024,8 +2024,8 @@ class B:
class C:
def __radd__(self, other, oops) -> int: ...
[out]
tmp/foo.pyi:3: error: Invalid signature "def (foo.B) -> foo.A"
tmp/foo.pyi:5: error: Invalid signature "def (foo.C, Any, Any) -> builtins.int"
tmp/foo.pyi:3: error: Invalid signature "Callable[[B], A]"
tmp/foo.pyi:5: error: Invalid signature "Callable[[C, Any, Any], int]"

[case testReverseOperatorOrderingCase1]
class A:
Expand Down Expand Up @@ -2627,8 +2627,8 @@ class C:
class D:
def __getattribute__(self, x: str) -> None: pass
[out]
main:4: error: Invalid signature "def (__main__.B, __main__.A) -> __main__.B" for "__getattribute__"
main:6: error: Invalid signature "def (__main__.C, builtins.str, builtins.str) -> __main__.C" for "__getattribute__"
main:4: error: Invalid signature "Callable[[B, A], B]" for "__getattribute__"
main:6: error: Invalid signature "Callable[[C, str, str], C]" for "__getattribute__"

[case testGetattr]

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

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

class D:
__setattr__ = 'hello' # E: Invalid signature "builtins.str" for "__setattr__"
__setattr__ = 'hello' # E: Invalid signature "str" for "__setattr__"

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

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

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

Expand All @@ -2769,7 +2769,7 @@ b = B()
b.integer = 5

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

Expand Down Expand Up @@ -5500,8 +5500,8 @@ A = G
x: A[B[int]] # E
B = G
[out]
main:8:4: error: Type argument "__main__.G[builtins.int]" of "G" must be a subtype of "builtins.str"
main:8:6: error: Type argument "builtins.int" of "G" must be a subtype of "builtins.str"
main:8:4: error: Type argument "G[int]" of "G" must be a subtype of "str"
main:8:6: error: Type argument "int" of "G" must be a subtype of "str"

[case testExtremeForwardReferencing]
from typing import TypeVar, Generic
Expand Down
14 changes: 7 additions & 7 deletions test-data/unit/check-errorcodes.test
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ class D(Generic[S]): pass
class E(Generic[S, T]): pass

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

def h(a: TT, s: S) -> None:
Expand Down Expand Up @@ -854,7 +854,7 @@ class Foo:
pass

foo = Foo()
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]
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]
pass

zero = 0
Expand All @@ -880,18 +880,18 @@ if not good_union:
pass

bad_union: Union[Foo, object] = Foo()
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]
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]
pass
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]
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]
pass

def f():
pass
if f: # E: Function "def () -> Any" could always be true in boolean context [truthy-bool]
if f: # E: Function "Callable[[], Any]" could always be true in boolean context [truthy-bool]
pass
if not f: # E: Function "def () -> Any" could always be true in boolean context [truthy-bool]
if not f: # E: Function "Callable[[], Any]" could always be true in boolean context [truthy-bool]
pass
conditional_result = 'foo' if f else 'bar' # E: Function "def () -> Any" could always be true in boolean context [truthy-bool]
conditional_result = 'foo' if f else 'bar' # E: Function "Callable[[], Any]" could always be true in boolean context [truthy-bool]

lst: List[int] = []
if lst:
Expand Down
8 changes: 4 additions & 4 deletions test-data/unit/check-modules.test
Original file line number Diff line number Diff line change
Expand Up @@ -2000,7 +2000,7 @@ reveal_type(has_getattr.any_attribute)
def __getattr__(x: int, y: str) -> str: ...

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

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

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

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

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

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

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

[builtins fixtures/module.pyi]
Expand Down
2 changes: 1 addition & 1 deletion test-data/unit/check-namedtuple.test
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,7 @@ T = TypeVar('T', bound='M')
class G(Generic[T]):
x: T

yb: G[int] # E: Type argument "builtins.int" of "G" must be a subtype of "Tuple[builtins.int, fallback=__main__.M]"
yb: G[int] # E: Type argument "int" of "G" must be a subtype of "M"
yg: G[M]
reveal_type(G[M]().x.x) # N: Revealed type is "builtins.int"
reveal_type(G[M]().x[0]) # N: Revealed type is "builtins.int"
Expand Down
Loading

0 comments on commit e32f35b

Please sign in to comment.