Skip to content

Commit 41ddcdb

Browse files
authored
Limit binder special-casing to imprecise Any (#22050)
There is a bunch of special-casing for `Any` types in binder. The goal of this special-casing is to limit the spread of `Any` types from legacy code with missing/imprecise types. This special-casing is quite ad-hoc, and thus should be limited only to imprecise kinds of `Any`. Note this should help with #22011
1 parent bb2a456 commit 41ddcdb

6 files changed

Lines changed: 92 additions & 17 deletions

File tree

‎mypy/binder.py‎

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from mypy.subtypes import is_same_type, is_subtype
2222
from mypy.typeops import make_simplified_union
2323
from mypy.types import (
24+
IMPRECISE_ANYS,
2425
AnyType,
2526
Instance,
2627
NoneType,
@@ -499,8 +500,7 @@ def assign_type(self, expr: Expression, type: Type, declared_type: Type | None)
499500
return
500501

501502
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):
504504
# Any type requires some special casing, for both historical reasons,
505505
# and to optimise user experience without sacrificing correctness too much.
506506
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:
643643
if isinstance(expr, RefExpr):
644644
if isinstance(expr.node, Var):
645645
type = expr.node.type
646-
if not isinstance(get_proper_type(type), PartialType):
646+
if not isinstance(type, PartialType):
647647
return type
648648
elif isinstance(expr.node, TypeInfo):
649649
return TypeType(fill_typevars_with_any(expr.node))
@@ -708,3 +708,14 @@ def collapse_variadic_union(typ: UnionType) -> Type:
708708
else:
709709
simplified = TupleType(prefix + [unpack] + suffix, fallback=last.partial_fallback)
710710
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

‎mypy/types.py‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,20 @@ class TypeOfAny:
260260
suggestion_engine: Final = 9
261261

262262

263+
# Some parts of the code distinguish two categories of Any types:
264+
# * Intentional: explicit, from_error, special_form - these are what other
265+
# languages/type-checkers may call "Dynamic", i.e. too tricky to express.
266+
# * Imprecise: unannotated, from_unimported_type, from_omitted_generics - these
267+
# are what other languages/type-checkers may call "Unknown", i.e. values in
268+
# partially annotated legacy code. These are usually "non-sticky", after assigning
269+
# such value to a variable, the variable keeps the original (more precise) type.
270+
IMPRECISE_ANYS: Final = (
271+
TypeOfAny.unannotated,
272+
TypeOfAny.from_unimported_type,
273+
TypeOfAny.from_omitted_generics,
274+
)
275+
276+
263277
def deserialize_type(data: JsonDict | str) -> Type:
264278
if isinstance(data, str):
265279
return Instance.deserialize(data)

‎test-data/unit/check-narrowing.test‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4299,3 +4299,50 @@ def func(y: H) -> H:
42994299
else:
43004300
return y
43014301
[builtins fixtures/primitives.pyi]
4302+
4303+
[case testNarrowingToAnyDifferentKinds]
4304+
# flags: --ignore-missing-imports
4305+
from typing import Any
4306+
from whatever import Unimported
4307+
4308+
explicit: Any
4309+
4310+
def foo(): ...
4311+
imprecise = foo()
4312+
unimported: Unimported
4313+
4314+
x: int
4315+
if int():
4316+
x = explicit
4317+
reveal_type(x) # N: Revealed type is "Any"
4318+
4319+
y: int
4320+
if int():
4321+
y = from_error # type: ignore
4322+
reveal_type(y) # N: Revealed type is "Any"
4323+
4324+
x1: int
4325+
if int():
4326+
x1 = imprecise
4327+
reveal_type(x1) # N: Revealed type is "builtins.int"
4328+
4329+
y1: int
4330+
if int():
4331+
y1 = unimported
4332+
reveal_type(y) # N: Revealed type is "builtins.int"
4333+
4334+
[case testNarrowingToImpreciseAnyFromUnionWithExplicit]
4335+
from typing import Any, Union
4336+
4337+
def foo(): ...
4338+
imprecise = foo()
4339+
4340+
x: Union[Any, int]
4341+
if int():
4342+
x = imprecise
4343+
reveal_type(x) # N: Revealed type is "Any"
4344+
4345+
y: Union[str, int]
4346+
if int():
4347+
y = imprecise
4348+
reveal_type(y) # N: Revealed type is "builtins.str | builtins.int"

‎test-data/unit/check-redefine2.test‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1525,10 +1525,11 @@ def test(x: Union[str, Int]) -> None:
15251525
reveal_type(y) # N: Revealed type is "Any"
15261526

15271527
[case testNewRedefineNarrowingForNestedUnionWithNone]
1528-
# flags: --allow-redefinition
1529-
from typing import Any, Union
1528+
# flags: --allow-redefinition --ignore-missing-imports
1529+
from typing import Union
1530+
from somewhere import Unknown
15301531

1531-
a: Any
1532+
a: Unknown
15321533
Int = Union[int, None]
15331534

15341535
def test(x: Union[str, Int]) -> None:
@@ -1552,9 +1553,11 @@ def test(x: Union[str, Int]) -> None:
15521553
reveal_type(y) # N: Revealed type is "Any"
15531554

15541555
[case testRegularNarrowingForNestedUnionWithNone]
1555-
from typing import Any, Union
1556+
# flags: --ignore-missing-imports
1557+
from typing import Union
1558+
from somewhere import Unknown
15561559

1557-
a: Any
1560+
a: Unknown
15581561
Int = Union[int, None]
15591562

15601563
def test(x: Union[str, Int]) -> None:

‎test-data/unit/check-unions.test‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -524,9 +524,11 @@ def foo(bar: Union[Union[A, B], C]) -> None:
524524
[out]
525525

526526
[case testAssignAnyToUnion]
527-
from typing import Union, Any
527+
from typing import Union
528+
from somewhere import Unknown # type: ignore
529+
528530
x: Union[int, str]
529-
a: Any
531+
a: Unknown
530532
if bool():
531533
x = a
532534
reveal_type(x) # N: Revealed type is "builtins.int | builtins.str"

‎test-data/unit/check-unreachable-code.test‎

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -777,10 +777,8 @@ reveal_type(x) # N: Revealed type is "__main__.B"
777777
[typing fixtures/typing-medium.pyi]
778778

779779
[case testUnreachableWhenSuperclassIsAny]
780-
from typing import Any
780+
from somewhere import Parent # type: ignore
781781

782-
# This can happen if we're importing a class from a missing module
783-
Parent: Any
784782
class Child(Parent):
785783
def foo(self) -> int:
786784
reveal_type(self) # N: Revealed type is "__main__.Child"
@@ -793,11 +791,11 @@ class Child(Parent):
793791
def bar(self) -> int:
794792
if 1:
795793
self = super(Child, self).something()
796-
reveal_type(self) # N: Revealed type is "__main__.Child"
794+
reveal_type(self) # N: Revealed type is "Any"
797795
if self is None:
798-
reveal_type(self)
799-
return None
800-
reveal_type(self) # N: Revealed type is "__main__.Child"
796+
reveal_type(self) # N: Revealed type is "None"
797+
return None # E: Incompatible return value type (got "None", expected "int")
798+
reveal_type(self) # N: Revealed type is "Any"
801799
return 3
802800
[builtins fixtures/isinstance.pyi]
803801

0 commit comments

Comments
 (0)