64
64
65
65
# Type of callback user for checking individual function arguments. See
66
66
# check_args() below for details.
67
- ArgChecker = Callable [[Type , Type , int , Type , int , int , CallableType , Context , MessageBuilder ],
67
+ ArgChecker = Callable [[Type ,
68
+ Type ,
69
+ int ,
70
+ Type ,
71
+ int ,
72
+ int ,
73
+ CallableType ,
74
+ Context ,
75
+ Context ,
76
+ MessageBuilder ],
68
77
None ]
69
78
70
79
# Maximum nesting level for math union in overloads, setting this to large values
@@ -842,7 +851,7 @@ def check_callable_call(self,
842
851
self .check_argument_count (callee , arg_types , arg_kinds ,
843
852
arg_names , formal_to_actual , context , self .msg )
844
853
845
- self .check_argument_types (arg_types , arg_kinds , callee , formal_to_actual , context ,
854
+ self .check_argument_types (arg_types , arg_kinds , args , callee , formal_to_actual , context ,
846
855
messages = arg_messages )
847
856
848
857
if (callee .is_type_obj () and (len (arg_types ) == 1 )
@@ -1169,7 +1178,8 @@ def check_argument_count(self,
1169
1178
if messages :
1170
1179
assert context , "Internal error: messages given without context"
1171
1180
elif context is None :
1172
- context = TempNode (AnyType (TypeOfAny .special_form )) # Avoid "is None" checks
1181
+ # Avoid "is None" checks
1182
+ context = TempNode (AnyType (TypeOfAny .special_form ))
1173
1183
1174
1184
# TODO(jukka): We could return as soon as we find an error if messages is None.
1175
1185
@@ -1271,6 +1281,7 @@ def check_for_extra_actual_arguments(self,
1271
1281
def check_argument_types (self ,
1272
1282
arg_types : List [Type ],
1273
1283
arg_kinds : List [int ],
1284
+ args : List [Expression ],
1274
1285
callee : CallableType ,
1275
1286
formal_to_actual : List [List [int ]],
1276
1287
context : Context ,
@@ -1303,12 +1314,19 @@ def check_argument_types(self,
1303
1314
callee .arg_names [i ], callee .arg_kinds [i ])
1304
1315
check_arg (expanded_actual , actual_type , arg_kinds [actual ],
1305
1316
callee .arg_types [i ],
1306
- actual + 1 , i + 1 , callee , context , messages )
1317
+ actual + 1 , i + 1 , callee , args [ actual ], context , messages )
1307
1318
1308
- def check_arg (self , caller_type : Type , original_caller_type : Type ,
1319
+ def check_arg (self ,
1320
+ caller_type : Type ,
1321
+ original_caller_type : Type ,
1309
1322
caller_kind : int ,
1310
- callee_type : Type , n : int , m : int , callee : CallableType ,
1311
- context : Context , messages : MessageBuilder ) -> None :
1323
+ callee_type : Type ,
1324
+ n : int ,
1325
+ m : int ,
1326
+ callee : CallableType ,
1327
+ context : Context ,
1328
+ outer_context : Context ,
1329
+ messages : MessageBuilder ) -> None :
1312
1330
"""Check the type of a single argument in a call."""
1313
1331
caller_type = get_proper_type (caller_type )
1314
1332
original_caller_type = get_proper_type (original_caller_type )
@@ -1326,8 +1344,13 @@ def check_arg(self, caller_type: Type, original_caller_type: Type,
1326
1344
elif not is_subtype (caller_type , callee_type ):
1327
1345
if self .chk .should_suppress_optional_error ([caller_type , callee_type ]):
1328
1346
return
1329
- code = messages .incompatible_argument (n , m , callee , original_caller_type ,
1330
- caller_kind , context )
1347
+ code = messages .incompatible_argument (n ,
1348
+ m ,
1349
+ callee ,
1350
+ original_caller_type ,
1351
+ caller_kind ,
1352
+ context = context ,
1353
+ outer_context = outer_context )
1331
1354
messages .incompatible_argument_note (original_caller_type , callee_type , context ,
1332
1355
code = code )
1333
1356
@@ -1404,7 +1427,7 @@ def check_overload_call(self,
1404
1427
# Neither alternative matches, but we can guess the user probably wants the
1405
1428
# second one.
1406
1429
erased_targets = self .overload_erased_call_targets (plausible_targets , arg_types ,
1407
- arg_kinds , arg_names , context )
1430
+ arg_kinds , arg_names , args , context )
1408
1431
1409
1432
# Step 5: We try and infer a second-best alternative if possible. If not, fall back
1410
1433
# to using 'Any'.
@@ -1569,14 +1592,16 @@ def overload_erased_call_targets(self,
1569
1592
arg_types : List [Type ],
1570
1593
arg_kinds : List [int ],
1571
1594
arg_names : Optional [Sequence [Optional [str ]]],
1595
+ args : List [Expression ],
1572
1596
context : Context ) -> List [CallableType ]:
1573
1597
"""Returns a list of all targets that match the caller after erasing types.
1574
1598
1575
1599
Assumes all of the given targets have argument counts compatible with the caller.
1576
1600
"""
1577
1601
matches = [] # type: List[CallableType]
1578
1602
for typ in plausible_targets :
1579
- if self .erased_signature_similarity (arg_types , arg_kinds , arg_names , typ , context ):
1603
+ if self .erased_signature_similarity (arg_types , arg_kinds , arg_names , args , typ ,
1604
+ context ):
1580
1605
matches .append (typ )
1581
1606
return matches
1582
1607
@@ -1755,8 +1780,11 @@ def combine_function_signatures(self, types: Sequence[Type]) -> Union[AnyType, C
1755
1780
variables = variables ,
1756
1781
implicit = True )
1757
1782
1758
- def erased_signature_similarity (self , arg_types : List [Type ], arg_kinds : List [int ],
1783
+ def erased_signature_similarity (self ,
1784
+ arg_types : List [Type ],
1785
+ arg_kinds : List [int ],
1759
1786
arg_names : Optional [Sequence [Optional [str ]]],
1787
+ args : List [Expression ],
1760
1788
callee : CallableType ,
1761
1789
context : Context ) -> bool :
1762
1790
"""Determine whether arguments could match the signature at runtime, after
@@ -1772,16 +1800,23 @@ def erased_signature_similarity(self, arg_types: List[Type], arg_kinds: List[int
1772
1800
# Too few or many arguments -> no match.
1773
1801
return False
1774
1802
1775
- def check_arg (caller_type : Type , original_caller_type : Type , caller_kind : int ,
1776
- callee_type : Type , n : int , m : int , callee : CallableType ,
1777
- context : Context , messages : MessageBuilder ) -> None :
1803
+ def check_arg (caller_type : Type ,
1804
+ original_ccaller_type : Type ,
1805
+ caller_kind : int ,
1806
+ callee_type : Type ,
1807
+ n : int ,
1808
+ m : int ,
1809
+ callee : CallableType ,
1810
+ context : Context ,
1811
+ outer_context : Context ,
1812
+ messages : MessageBuilder ) -> None :
1778
1813
if not arg_approximate_similarity (caller_type , callee_type ):
1779
1814
# No match -- exit early since none of the remaining work can change
1780
1815
# the result.
1781
1816
raise Finished
1782
1817
1783
1818
try :
1784
- self .check_argument_types (arg_types , arg_kinds , callee ,
1819
+ self .check_argument_types (arg_types , arg_kinds , args , callee ,
1785
1820
formal_to_actual , context = context , check_arg = check_arg )
1786
1821
return True
1787
1822
except Finished :
@@ -2429,7 +2464,7 @@ def check_op(self, method: str, base_type: Type,
2429
2464
result , inferred = self .check_op_reversible (
2430
2465
op_name = method ,
2431
2466
left_type = left_possible_type ,
2432
- left_expr = TempNode (left_possible_type ),
2467
+ left_expr = TempNode (left_possible_type , context = context ),
2433
2468
right_type = right_type ,
2434
2469
right_expr = arg ,
2435
2470
context = context ,
@@ -2456,7 +2491,8 @@ def check_op(self, method: str, base_type: Type,
2456
2491
right_variants = [(right_type , arg )]
2457
2492
right_type = get_proper_type (right_type )
2458
2493
if isinstance (right_type , UnionType ):
2459
- right_variants = [(item , TempNode (item )) for item in right_type .relevant_items ()]
2494
+ right_variants = [(item , TempNode (item , context = context ))
2495
+ for item in right_type .relevant_items ()]
2460
2496
2461
2497
msg = self .msg .clean_copy ()
2462
2498
msg .disable_count = 0
@@ -2468,7 +2504,7 @@ def check_op(self, method: str, base_type: Type,
2468
2504
result , inferred = self .check_op_reversible (
2469
2505
op_name = method ,
2470
2506
left_type = left_possible_type ,
2471
- left_expr = TempNode (left_possible_type ),
2507
+ left_expr = TempNode (left_possible_type , context = context ),
2472
2508
right_type = right_possible_type ,
2473
2509
right_expr = right_expr ,
2474
2510
context = context ,
@@ -2481,9 +2517,9 @@ def check_op(self, method: str, base_type: Type,
2481
2517
if len (left_variants ) >= 2 and len (right_variants ) >= 2 :
2482
2518
self .msg .warn_both_operands_are_from_unions (context )
2483
2519
elif len (left_variants ) >= 2 :
2484
- self .msg .warn_operand_was_from_union ("Left" , base_type , context )
2520
+ self .msg .warn_operand_was_from_union ("Left" , base_type , context = right_expr )
2485
2521
elif len (right_variants ) >= 2 :
2486
- self .msg .warn_operand_was_from_union ("Right" , right_type , context )
2522
+ self .msg .warn_operand_was_from_union ("Right" , right_type , context = right_expr )
2487
2523
2488
2524
# See the comment in 'check_overload_call' for more details on why
2489
2525
# we call 'combine_function_signature' instead of just unioning the inferred
@@ -2812,10 +2848,10 @@ def visit_reveal_expr(self, expr: RevealExpr) -> Type:
2812
2848
assert expr .expr is not None
2813
2849
revealed_type = self .accept (expr .expr , type_context = self .type_context [- 1 ])
2814
2850
if not self .chk .current_node_deferred :
2815
- self .msg .reveal_type (revealed_type , expr )
2851
+ self .msg .reveal_type (revealed_type , expr . expr )
2816
2852
if not self .chk .in_checked_function ():
2817
2853
self .msg .note ("'reveal_type' always outputs 'Any' in unchecked functions" ,
2818
- expr )
2854
+ expr . expr )
2819
2855
return revealed_type
2820
2856
else :
2821
2857
# REVEAL_LOCALS
@@ -3064,7 +3100,14 @@ def visit_dict_expr(self, e: DictExpr) -> Type:
3064
3100
if key is None :
3065
3101
stargs .append (value )
3066
3102
else :
3067
- args .append (TupleExpr ([key , value ]))
3103
+ tup = TupleExpr ([key , value ])
3104
+ if key .line >= 0 :
3105
+ tup .line = key .line
3106
+ tup .column = key .column
3107
+ else :
3108
+ tup .line = value .line
3109
+ tup .column = value .column
3110
+ args .append (tup )
3068
3111
# Define type variables (used in constructors below).
3069
3112
ktdef = TypeVarDef ('KT' , 'KT' , - 1 , [], self .object_type ())
3070
3113
vtdef = TypeVarDef ('VT' , 'VT' , - 2 , [], self .object_type ())
0 commit comments