|
20 | 20 | from mypy.plugin import Plugin, AttributeContext |
21 | 21 | from mypy import messages |
22 | 22 | from mypy import subtypes |
| 23 | +from mypy import meet |
| 24 | + |
23 | 25 | MYPY = False |
24 | 26 | if MYPY: # import for forward declaration only |
25 | 27 | import mypy.checker |
@@ -314,7 +316,9 @@ def analyze_var(name: str, var: Var, itype: Instance, info: TypeInfo, node: Cont |
314 | 316 | # methods: the former to the instance, the latter to the |
315 | 317 | # class. |
316 | 318 | functype = t |
317 | | - check_method_type(functype, itype, var.is_classmethod, node, msg) |
| 319 | + # Use meet to simulate dispatch - e.g. reduce Union[A, B] to A on dispatch to A |
| 320 | + dispatched_type = meet.meet_types(original_type, itype) |
| 321 | + check_self_arg(functype, dispatched_type, var.is_classmethod, node, name, msg) |
318 | 322 | signature = bind_self(functype, original_type, var.is_classmethod) |
319 | 323 | if var.is_property: |
320 | 324 | # A property cannot have an overloaded type => the cast |
@@ -370,33 +374,22 @@ def lookup_member_var_or_accessor(info: TypeInfo, name: str, |
370 | 374 | return None |
371 | 375 |
|
372 | 376 |
|
373 | | -def check_method_type(functype: FunctionLike, itype: Instance, is_classmethod: bool, |
374 | | - context: Context, msg: MessageBuilder) -> None: |
| 377 | +def check_self_arg(functype: FunctionLike, original_type: Type, is_classmethod: bool, |
| 378 | + context: Context, name: str, msg: MessageBuilder) -> None: |
| 379 | + """Check that the the most precise type of the self argument is compatible |
| 380 | + with the declared type of each of the overloads. |
| 381 | + """ |
375 | 382 | for item in functype.items(): |
376 | 383 | if not item.arg_types or item.arg_kinds[0] not in (ARG_POS, ARG_STAR): |
377 | 384 | # No positional first (self) argument (*args is okay). |
378 | | - msg.invalid_method_type(item, context) |
379 | | - elif not is_classmethod: |
380 | | - # Check that self argument has type 'Any' or valid instance type. |
381 | | - selfarg = item.arg_types[0] |
382 | | - # If this is a method of a tuple class, correct for the fact that |
383 | | - # we passed to typ.fallback in analyze_member_access. See #1432. |
384 | | - if isinstance(selfarg, TupleType): |
385 | | - selfarg = selfarg.fallback |
386 | | - if not subtypes.is_subtype(selfarg, itype): |
387 | | - msg.invalid_method_type(item, context) |
| 385 | + msg.fail('Attribute function with type %s does not accept self argument' |
| 386 | + % msg.format(item), context) |
388 | 387 | else: |
389 | | - # Check that cls argument has type 'Any' or valid class type. |
390 | | - # (This is sufficient for the current treatment of @classmethod, |
391 | | - # but probably needs to be revisited when we implement Type[C] |
392 | | - # or advanced variants of it like Type[<args>, C].) |
393 | | - clsarg = item.arg_types[0] |
394 | | - if isinstance(clsarg, CallableType) and clsarg.is_type_obj(): |
395 | | - if not subtypes.is_equivalent(clsarg.ret_type, itype): |
396 | | - msg.invalid_class_method_type(item, context) |
397 | | - else: |
398 | | - if not subtypes.is_equivalent(clsarg, AnyType(TypeOfAny.special_form)): |
399 | | - msg.invalid_class_method_type(item, context) |
| 388 | + selfarg = item.arg_types[0] |
| 389 | + if is_classmethod: |
| 390 | + original_type = TypeType.make_normalized(original_type) |
| 391 | + if not subtypes.is_subtype(original_type, erase_to_bound(selfarg)): |
| 392 | + msg.invalid_method_type(name, original_type, item, is_classmethod, context) |
400 | 393 |
|
401 | 394 |
|
402 | 395 | def analyze_class_attribute_access(itype: Instance, |
|
0 commit comments