|
21 | 21 | from mypy.subtypes import is_same_type, is_subtype |
22 | 22 | from mypy.typeops import make_simplified_union |
23 | 23 | from mypy.types import ( |
| 24 | + IMPRECISE_ANYS, |
24 | 25 | AnyType, |
25 | 26 | Instance, |
26 | 27 | NoneType, |
@@ -499,8 +500,7 @@ def assign_type(self, expr: Expression, type: Type, declared_type: Type | None) |
499 | 500 | return |
500 | 501 |
|
501 | 502 | p_declared = get_proper_type(declared_type) |
502 | | - p_type = get_proper_type(type) |
503 | | - if isinstance(p_type, AnyType): |
| 503 | + if is_imprecise_any(type): |
504 | 504 | # Any type requires some special casing, for both historical reasons, |
505 | 505 | # and to optimise user experience without sacrificing correctness too much. |
506 | 506 | if isinstance(expr, RefExpr) and isinstance(expr.node, Var) and expr.node.is_inferred: |
@@ -643,7 +643,7 @@ def get_declaration(expr: BindableExpression) -> Type | None: |
643 | 643 | if isinstance(expr, RefExpr): |
644 | 644 | if isinstance(expr.node, Var): |
645 | 645 | type = expr.node.type |
646 | | - if not isinstance(get_proper_type(type), PartialType): |
| 646 | + if not isinstance(type, PartialType): |
647 | 647 | return type |
648 | 648 | elif isinstance(expr.node, TypeInfo): |
649 | 649 | return TypeType(fill_typevars_with_any(expr.node)) |
@@ -708,3 +708,14 @@ def collapse_variadic_union(typ: UnionType) -> Type: |
708 | 708 | else: |
709 | 709 | simplified = TupleType(prefix + [unpack] + suffix, fallback=last.partial_fallback) |
710 | 710 | return UnionType.make_union([simplified] + other_items) |
| 711 | + |
| 712 | + |
| 713 | +def is_imprecise_any(tp: Type) -> bool: |
| 714 | + """Is this an imprecise Any type?""" |
| 715 | + tp = get_proper_type(tp) |
| 716 | + if not isinstance(tp, AnyType): |
| 717 | + return False |
| 718 | + if tp.type_of_any == TypeOfAny.from_another_any: |
| 719 | + assert tp.source_any is not None |
| 720 | + tp = tp.source_any |
| 721 | + return tp.type_of_any in IMPRECISE_ANYS |
0 commit comments