Skip to content

Commit 2360552

Browse files
committed
Try some real madness
1 parent 182eba9 commit 2360552

2 files changed

Lines changed: 33 additions & 0 deletions

File tree

mypy/semanal_typeargs.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,10 @@
3333
TypeVarTupleType,
3434
TypeVarType,
3535
UnboundType,
36+
UnionType,
3637
UnpackType,
3738
flatten_nested_tuples,
39+
flatten_nested_unions,
3840
get_proper_type,
3941
get_proper_types,
4042
split_with_prefix_and_suffix,
@@ -118,6 +120,10 @@ def visit_tuple_type(self, t: TupleType) -> None:
118120
# and we need to return an Instance instead of TupleType.
119121
super().visit_tuple_type(t)
120122

123+
def visit_union_type(self, t: UnionType) -> None:
124+
super().visit_union_type(t)
125+
t.items = flatten_nested_unions(t.items)
126+
121127
def visit_callable_type(self, t: CallableType) -> None:
122128
super().visit_callable_type(t)
123129
t.normalize_trivial_unpack()

test-data/unit/check-redefine2.test

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1395,3 +1395,30 @@ def test() -> None:
13951395
y = li
13961396
reveal_type(y) # N: Revealed type is "builtins.list[Any]"
13971397
[builtins fixtures/primitives.pyi]
1398+
1399+
[case testNewRedefineNarrowingForNestedUnionWithAny]
1400+
# flags: --allow-redefinition-new --local-partial-types
1401+
from typing import Any, Union
1402+
1403+
a: Any
1404+
Int = Union[int, Any]
1405+
1406+
def test(x: Union[str, Int]) -> None:
1407+
x = a
1408+
reveal_type(x) # N: Revealed type is "Any"
1409+
1410+
[case testNewRedefineWidenedArgumentDeferral]
1411+
# flags: --allow-redefinition-new --local-partial-types
1412+
def foo(x: int) -> None:
1413+
reveal_type(x) # N: Revealed type is "builtins.int"
1414+
if bool():
1415+
x = "no"
1416+
c: C
1417+
c.x
1418+
reveal_type(x) # N: Revealed type is "builtins.int | builtins.str"
1419+
1420+
class C:
1421+
def __init__(self) -> None:
1422+
self.x = defer()
1423+
1424+
def defer() -> int: ...

0 commit comments

Comments
 (0)