diff --git a/mypy/checkexpr.py b/mypy/checkexpr.py index 43896171eadc..f6352ae9868c 100644 --- a/mypy/checkexpr.py +++ b/mypy/checkexpr.py @@ -5363,7 +5363,14 @@ def __init__(self, ignore_in_type_obj: bool) -> None: self.ignore_in_type_obj = ignore_in_type_obj def visit_any(self, t: AnyType) -> bool: - return t.type_of_any != TypeOfAny.special_form # special forms are not real Any types + # Special forms are not real Any types (note that we don't need to recurse + # since AnyType constructor finds actual source). + if t.type_of_any == TypeOfAny.special_form: + return False + if t.type_of_any == TypeOfAny.from_another_any: + assert t.source_any is not None + return t.source_any.type_of_any != TypeOfAny.special_form + return True def visit_callable_type(self, t: CallableType) -> bool: if self.ignore_in_type_obj and t.is_type_obj(): diff --git a/mypy/test/testtypes.py b/mypy/test/testtypes.py index 5f6943de3199..664360b0c951 100644 --- a/mypy/test/testtypes.py +++ b/mypy/test/testtypes.py @@ -6,6 +6,7 @@ from unittest import TestCase, skipUnless import mypy.expandtype +from mypy.checkexpr import has_any_type from mypy.erasetype import erase_type, remove_instance_last_known_values from mypy.expandtype import expand_type from mypy.indirection import TypeIndirectionVisitor @@ -64,6 +65,11 @@ def setUp(self) -> None: def test_any(self) -> None: assert_equal(str(AnyType(TypeOfAny.special_form)), "Any") + def test_has_any_special(self) -> None: + a = AnyType(TypeOfAny.special_form) + assert not has_any_type(a) + assert not has_any_type(AnyType(TypeOfAny.from_another_any, source_any=a)) + def test_simple_unbound_type(self) -> None: u = UnboundType("Foo") assert_equal(str(u), "Foo?") diff --git a/test-data/unit/check-flags.test b/test-data/unit/check-flags.test index 6ec0849146c0..3c91bf3a8abc 100644 --- a/test-data/unit/check-flags.test +++ b/test-data/unit/check-flags.test @@ -2195,3 +2195,15 @@ cb(lambda x: a) # OK fn = lambda x: a cb(fn) + +[case testDisallowAnyFromAnotherAny] +# flags: --disallow-any-expr +from typing import Tuple, TypeVar + +T = TypeVar("T") + +def f(x: T) -> Tuple[T, float]: ... + +def a() -> None: + _ = f(42) +[builtins fixtures/tuple.pyi]