Skip to content

Commit 0413a0e

Browse files
committed
rephrase message; test tuple parameter; use call to check
1 parent c446d21 commit 0413a0e

File tree

7 files changed

+58
-15
lines changed

7 files changed

+58
-15
lines changed

mypy/checker.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -740,9 +740,7 @@ def is_implicit_any(t: Type) -> bool:
740740

741741
# Type check initialization expressions.
742742
for arg in item.arguments:
743-
init = arg.initialization_statement
744-
if init:
745-
self.accept(init)
743+
self.expr_checker.check_default_arg(arg)
746744

747745
# Type check body in a new scope.
748746
with self.binder.top_frame_context():

mypy/checkexpr.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from collections import OrderedDict
44
from typing import cast, Dict, Set, List, Tuple, Callable, Union, Optional
55

6-
from mypy.errors import report_internal_error
6+
from mypy.errors import Errors, report_internal_error
77
from mypy.typeanal import has_any_from_unimported_type, check_for_explicit_any, set_any_tvars
88
from mypy.types import (
99
Type, AnyType, CallableType, Overloaded, NoneTyp, TypeVarDef,
@@ -1011,6 +1011,20 @@ def check_arg(self, caller_type: Type, original_caller_type: Type,
10111011
messages.incompatible_argument(n, m, callee, original_caller_type,
10121012
caller_kind, context)
10131013

1014+
def check_default_arg(self, arg: mypy.nodes.Argument) -> None:
1015+
rvalue = arg.initializer
1016+
lval_type = arg.variable.type
1017+
if rvalue and lval_type and not (self.chk.is_stub and isinstance(rvalue, EllipsisExpr)):
1018+
rval_type = self.accept(rvalue, lval_type)
1019+
varname = arg.variable.name()
1020+
single = CallableType([lval_type], [ARG_POS], [None],
1021+
AnyType(), self.named_type('builtins.function'))
1022+
msg = MessageBuilder(Errors(), self.chk.modules)
1023+
self.check_call(single, [rvalue], [ARG_POS], arg, arg_messages=msg)
1024+
if msg.is_errors():
1025+
self.msg.incompatible_default_argument(
1026+
varname, lval_type, rval_type, arg)
1027+
10141028
def overload_call_target(self, arg_types: List[Type], arg_kinds: List[int],
10151029
arg_names: List[str],
10161030
overload: Overloaded, context: Context,

mypy/messages.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,12 @@ def untyped_function_call(self, callee: CallableType, context: Context) -> Type:
492492
self.fail('Call to untyped function {} in typed context'.format(name), context)
493493
return AnyType()
494494

495+
def incompatible_default_argument(self, name: str, lvalue: Type, rvalue: Type,
496+
context: Context) -> None:
497+
lvalt, rvalt = self.format_distinctly(lvalue, rvalue)
498+
fmt = 'Incompatible default for argument "{}" (argument has type {}, default has type {})'
499+
self.fail(fmt.format(name, lvalt, rvalt), context)
500+
495501
def incompatible_argument(self, n: int, m: int, callee: CallableType, arg_type: Type,
496502
arg_kind: int, context: Context) -> None:
497503
"""Report an error about an incompatible argument type.

test-data/unit/check-functions.test

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -388,17 +388,42 @@ class A: pass
388388

389389
[case testDefaultArgumentExpressions2]
390390
import typing
391-
def f(x: 'A' = B()) -> None: # E: Incompatible types in assignment (expression has type "B", variable has type "A")
391+
def f(x: 'A' = B()) -> None: # E: Incompatible default for argument "x" (argument has type "A", default has type "B")
392392
b = x # type: B # E: Incompatible types in assignment (expression has type "A", variable has type "B")
393393
a = x # type: A
394394

395395
class B: pass
396396
class A: pass
397-
[out]
397+
398+
[case testDefaultArgumentExpressionsGeneric]
399+
from typing import TypeVar
400+
T = TypeVar('T', bound='A')
401+
def f(x: T = B()) -> None: # E: Incompatible default for argument "x" (argument has type "T", default has type "B")
402+
b = x # type: B # E: Incompatible types in assignment (expression has type "T", variable has type "B")
403+
a = x # type: A
404+
405+
class B: pass
406+
class A: pass
407+
408+
[case testDefaultArgumentExpressionsPython2]
409+
# flags: --python-version 2.7
410+
from typing import Tuple
411+
def f(x = B()): # E: Incompatible default for argument "x" (argument has type "A", default has type "B")
412+
# type: (A) -> None
413+
b = x # type: B # E: Incompatible types in assignment (expression has type "A", variable has type "B")
414+
a = x # type: A
415+
416+
def g((x, y) = (A(), B())): # E: Incompatible default for argument "__tuple_arg_1" (argument has type "Tuple[B, B]", default has type "Tuple[A, B]")
417+
# type: ( Tuple[B, B] ) -> None
418+
b = x # type: B
419+
a = x # type: A # E: Incompatible types in assignment (expression has type "B", variable has type "A")
420+
421+
class B: pass
422+
class A: pass
398423

399424
[case testDefaultArgumentsWithSubtypes]
400425
import typing
401-
def f(x: 'B' = A()) -> None: # E: Incompatible types in assignment (expression has type "A", variable has type "B")
426+
def f(x: 'B' = A()) -> None: # E: Incompatible default for argument "x" (argument has type "B", default has type "A")
402427
pass
403428
def g(x: 'A' = B()) -> None:
404429
pass
@@ -409,7 +434,7 @@ class B(A): pass
409434

410435
[case testMultipleDefaultArgumentExpressions]
411436
import typing
412-
def f(x: 'A' = B(), y: 'B' = B()) -> None: # E: Incompatible types in assignment (expression has type "B", variable has type "A")
437+
def f(x: 'A' = B(), y: 'B' = B()) -> None: # E: Incompatible default for argument "x" (argument has type "A", default has type "B")
413438
pass
414439
def h(x: 'A' = A(), y: 'B' = B()) -> None:
415440
pass
@@ -420,7 +445,7 @@ class B: pass
420445

421446
[case testMultipleDefaultArgumentExpressions2]
422447
import typing
423-
def g(x: 'A' = A(), y: 'B' = A()) -> None: # E: Incompatible types in assignment (expression has type "A", variable has type "B")
448+
def g(x: 'A' = A(), y: 'B' = A()) -> None: # E: Incompatible default for argument "y" (argument has type "B", default has type "A")
424449
pass
425450

426451
class A: pass

test-data/unit/check-inference.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1119,7 +1119,7 @@ from typing import Callable
11191119
def f(a: Callable[..., None] = lambda *a, **k: None):
11201120
pass
11211121

1122-
def g(a: Callable[..., None] = lambda *a, **k: 1): # E: Incompatible types in assignment (expression has type Callable[[VarArg(Any), KwArg(Any)], int], variable has type Callable[..., None])
1122+
def g(a: Callable[..., None] = lambda *a, **k: 1): # E: Incompatible default for argument "a" (argument has type Callable[..., None], default has type Callable[[VarArg(Any), KwArg(Any)], int])
11231123
pass
11241124
[builtins fixtures/dict.pyi]
11251125

test-data/unit/check-modules.test

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -475,11 +475,11 @@ def f(x: int = ...) -> None: pass
475475
[file m.pyi]
476476
def g(x: int = '') -> None: pass
477477
[out]
478-
tmp/m.pyi:1: error: Incompatible types in assignment (expression has type "str", variable has type "int")
479-
main:2: error: Incompatible types in assignment (expression has type "ellipsis", variable has type "int")
478+
tmp/m.pyi:1: error: Incompatible default for argument "x" (argument has type "int", default has type "str")
479+
main:2: error: Incompatible default for argument "x" (argument has type "int", default has type "ellipsis")
480480

481481
[case testEllipsisDefaultArgValueInNonStub]
482-
def f(x: int = ...) -> None: pass # E: Incompatible types in assignment (expression has type "ellipsis", variable has type "int")
482+
def f(x: int = ...) -> None: pass # E: Incompatible default for argument "x" (argument has type "int", default has type "ellipsis")
483483
[out]
484484

485485
[case testStarImportOverlapping]

test-data/unit/check-optional.test

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ f(None)
127127

128128
[case testNoInferOptionalFromDefaultNone]
129129
# flags: --no-implicit-optional
130-
def f(x: int = None) -> None: # E: Incompatible types in assignment (expression has type None, variable has type "int")
130+
def f(x: int = None) -> None: # E: Incompatible default for argument "x" (argument has type "int", default has type None)
131131
pass
132132
[out]
133133

@@ -140,7 +140,7 @@ f(None)
140140

141141
[case testNoInferOptionalFromDefaultNoneComment]
142142
# flags: --no-implicit-optional
143-
def f(x=None): # E: Incompatible types in assignment (expression has type None, variable has type "int")
143+
def f(x=None): # E: Incompatible default for argument "x" (argument has type "int", default has type None)
144144
# type: (int) -> None
145145
pass
146146
[out]

0 commit comments

Comments
 (0)