|
7 | 7 | from typing_extensions import TypeAlias as _TypeAlias |
8 | 8 |
|
9 | 9 | from mypy.erasetype import remove_instance_last_known_values |
10 | | -from mypy.join import join_simple |
11 | 10 | from mypy.literals import Key, literal, literal_hash, subkeys |
12 | 11 | from mypy.nodes import Expression, IndexExpr, MemberExpr, NameExpr, RefExpr, TypeInfo, Var |
13 | 12 | from mypy.subtypes import is_same_type, is_subtype |
| 13 | +from mypy.typeops import make_simplified_union |
14 | 14 | from mypy.types import ( |
15 | 15 | AnyType, |
16 | 16 | Instance, |
@@ -237,27 +237,25 @@ def update_from_options(self, frames: list[Frame]) -> bool: |
237 | 237 | ): |
238 | 238 | type = AnyType(TypeOfAny.from_another_any, source_any=declaration_type) |
239 | 239 | else: |
240 | | - for other in resulting_values[1:]: |
241 | | - assert other is not None |
242 | | - type = join_simple(self.declarations[key], type, other.type) |
243 | | - # Try simplifying resulting type for unions involving variadic tuples. |
244 | | - # Technically, everything is still valid without this step, but if we do |
245 | | - # not do this, this may create long unions after exiting an if check like: |
246 | | - # x: tuple[int, ...] |
247 | | - # if len(x) < 10: |
248 | | - # ... |
249 | | - # We want the type of x to be tuple[int, ...] after this block (if it is |
250 | | - # still equivalent to such type). |
251 | | - if isinstance(type, UnionType): |
252 | | - type = collapse_variadic_union(type) |
253 | | - if isinstance(type, ProperType) and isinstance(type, UnionType): |
254 | | - # Simplify away any extra Any's that were added to the declared |
255 | | - # type when popping a frame. |
256 | | - simplified = UnionType.make_union( |
257 | | - [t for t in type.items if not isinstance(get_proper_type(t), AnyType)] |
258 | | - ) |
259 | | - if simplified == self.declarations[key]: |
260 | | - type = simplified |
| 240 | + type = make_simplified_union([t.type for t in resulting_values]) |
| 241 | + # Try simplifying resulting type for unions involving variadic tuples. |
| 242 | + # Technically, everything is still valid without this step, but if we do |
| 243 | + # not do this, this may create long unions after exiting an if check like: |
| 244 | + # x: tuple[int, ...] |
| 245 | + # if len(x) < 10: |
| 246 | + # ... |
| 247 | + # We want the type of x to be tuple[int, ...] after this block (if it is |
| 248 | + # still equivalent to such type). |
| 249 | + if isinstance(type, UnionType): |
| 250 | + type = collapse_variadic_union(type) |
| 251 | + if isinstance(type, ProperType) and isinstance(type, UnionType): |
| 252 | + # Simplify away any extra Any's that were added to the declared |
| 253 | + # type when popping a frame. |
| 254 | + simplified = UnionType.make_union( |
| 255 | + [t for t in type.items if not isinstance(get_proper_type(t), AnyType)] |
| 256 | + ) |
| 257 | + if simplified == self.declarations[key]: |
| 258 | + type = simplified |
261 | 259 | if current_value is None or not is_same_type(type, current_value[0]): |
262 | 260 | self._put(key, type, from_assignment=True) |
263 | 261 | changed = True |
|
0 commit comments