Skip to content

Correctly handle TypeOfAny.from_another_any #15497

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion mypy/checkexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down
6 changes: 6 additions & 0 deletions mypy/test/testtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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?")
Expand Down
12 changes: 12 additions & 0 deletions test-data/unit/check-flags.test
Original file line number Diff line number Diff line change
Expand Up @@ -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]