Skip to content
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

Proposal: don't simplify unions in expand_type() #14178

Merged
merged 3 commits into from
Nov 24, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
14 changes: 9 additions & 5 deletions mypy/expandtype.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@
UninhabitedType,
UnionType,
UnpackType,
flatten_nested_unions,
get_proper_type,
remove_trivial,
)
from mypy.typevartuples import (
find_unpack_in_list,
Expand Down Expand Up @@ -429,11 +431,13 @@ def visit_literal_type(self, t: LiteralType) -> Type:
return t

def visit_union_type(self, t: UnionType) -> Type:
# After substituting for type variables in t.items,
# some of the resulting types might be subtypes of others.
from mypy.typeops import make_simplified_union # asdf

return make_simplified_union(self.expand_types(t.items), t.line, t.column)
expanded = self.expand_types(t.items)
# After substituting for type variables in t.items, some resulting types
# might be subtypes of others, however calling make_simplified_union()
# can cause recursion, so we just remove strict duplicates.
return UnionType.make_union(
remove_trivial(flatten_nested_unions(expanded)), t.line, t.column
)

def visit_partial_type(self, t: PartialType) -> Type:
return t
Expand Down
2 changes: 1 addition & 1 deletion mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,6 @@
fix_instance_types,
has_any_from_unimported_type,
no_subscript_builtin_alias,
remove_dups,
type_constructors,
)
from mypy.typeops import function_type, get_type_vars
Expand Down Expand Up @@ -281,6 +280,7 @@
is_dunder,
is_typeshed_file,
module_prefix,
remove_dups,
unmangle,
unnamed_function,
)
Expand Down
12 changes: 1 addition & 11 deletions mypy/typeanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
)
from mypy.typetraverser import TypeTraverserVisitor
from mypy.typevars import fill_typevars
from mypy.util import remove_dups

T = TypeVar("T")

Expand Down Expand Up @@ -1729,17 +1730,6 @@ def set_any_tvars(
return TypeAliasType(node, [any_type] * len(node.alias_tvars), newline, newcolumn)


def remove_dups(tvars: Iterable[T]) -> list[T]:
# Get unique elements in order of appearance
all_tvars: set[T] = set()
new_tvars: list[T] = []
for t in tvars:
if t not in all_tvars:
new_tvars.append(t)
all_tvars.add(t)
return new_tvars


def flatten_tvars(ll: Iterable[list[T]]) -> list[T]:
return remove_dups(chain.from_iterable(ll))

Expand Down
29 changes: 28 additions & 1 deletion mypy/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
SymbolNode,
)
from mypy.state import state
from mypy.util import IdMapper
from mypy.util import IdMapper, remove_dups

T = TypeVar("T")

Expand Down Expand Up @@ -3430,3 +3430,30 @@ def store_argument_type(
if not isinstance(arg_type, ParamSpecType) and not typ.unpack_kwargs:
arg_type = named_type("builtins.dict", [named_type("builtins.str", []), arg_type])
defn.arguments[i].variable.type = arg_type


def remove_trivial(types: Iterable[Type]) -> list[Type]:
"""Make trivial simplifications on a list of types without calling is_subtype().

This makes following simplifications:
* Remove bottom types (taking into account strict optional setting)
* Remove everything else if there is an `object`
* Remove strict duplicate types
"""
removed_none = False
new_types: list[Type] = []
for t in types:
p_t = get_proper_type(t)
if isinstance(p_t, UninhabitedType):
continue
if isinstance(p_t, NoneType) and not state.strict_optional:
removed_none = True
continue
if isinstance(p_t, Instance) and p_t.type.fullname == "builtins.object":
return [p_t]
new_types.append(t)
if new_types:
return remove_dups(new_types)
if removed_none:
return [NoneType()]
return [UninhabitedType()]
11 changes: 11 additions & 0 deletions mypy/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -820,3 +820,14 @@ def plural_s(s: int | Sized) -> str:
return "s"
else:
return ""


def remove_dups(items: Iterable[T]) -> list[T]:
# Get unique elements in order of appearance
all_items: set[T] = set()
new_items: list[T] = []
for item in items:
if item not in all_items:
new_items.append(item)
all_items.add(item)
return new_items
34 changes: 34 additions & 0 deletions test-data/unit/check-recursive-types.test
Original file line number Diff line number Diff line change
Expand Up @@ -837,3 +837,37 @@ def foo(x: T) -> C: ...

Nested = Union[C, Sequence[Nested]]
x: Nested = foo(42)

[case testNoRecursiveExpandInstanceUnionCrash]
from typing import List, Union

class Tag(List[Union[Tag, List[Tag]]]): ...
Tag()

[case testNoRecursiveExpandInstanceUnionCrashGeneric]
from typing import Generic, Iterable, TypeVar, Union

ValueT = TypeVar("ValueT")
class Recursive(Iterable[Union[ValueT, Recursive[ValueT]]]):
pass

class Base(Generic[ValueT]):
def __init__(self, element: ValueT):
pass
class Sub(Base[Union[ValueT, Recursive[ValueT]]]):
pass

x: Iterable[str]
reveal_type(Sub) # N: Revealed type is "def [ValueT] (element: Union[ValueT`1, __main__.Recursive[ValueT`1]]) -> __main__.Sub[ValueT`1]"
reveal_type(Sub(x)) # N: Revealed type is "__main__.Sub[typing.Iterable[builtins.str]]"

[case testNoRecursiveExpandInstanceUnionCrashInference]
from typing import TypeVar, Union, Generic, List

T = TypeVar("T")
InList = Union[T, InListRecurse[T]]
class InListRecurse(Generic[T], List[InList[T]]): ...

def list_thing(transforming: InList[T]) -> T:
...
reveal_type(list_thing([5])) # N: Revealed type is "builtins.list[builtins.int]"