|
6 | 6 | from collections import defaultdict |
7 | 7 | from collections.abc import Iterable, Iterator, Mapping, Sequence, Set as AbstractSet |
8 | 8 | from contextlib import ExitStack, contextmanager |
9 | | -from typing import ( |
10 | | - Callable, |
11 | | - Final, |
12 | | - Generic, |
13 | | - Literal, |
14 | | - NamedTuple, |
15 | | - Optional, |
16 | | - TypeVar, |
17 | | - Union, |
18 | | - cast, |
19 | | - overload, |
20 | | -) |
| 9 | +from typing import Callable, Final, Generic, Literal, NamedTuple, TypeVar, cast, overload |
21 | 10 | from typing_extensions import TypeAlias as _TypeAlias, TypeGuard |
22 | 11 |
|
23 | 12 | import mypy.checkexpr |
|
249 | 238 | # Maximum length of fixed tuple types inferred when narrowing from variadic tuples. |
250 | 239 | MAX_PRECISE_TUPLE_SIZE: Final = 8 |
251 | 240 |
|
252 | | -DeferredNodeType: _TypeAlias = Union[FuncDef, OverloadedFuncDef, Decorator] |
253 | | -FineGrainedDeferredNodeType: _TypeAlias = Union[FuncDef, MypyFile, OverloadedFuncDef] |
| 241 | +DeferredNodeType: _TypeAlias = FuncDef | OverloadedFuncDef | Decorator |
| 242 | +FineGrainedDeferredNodeType: _TypeAlias = FuncDef | MypyFile | OverloadedFuncDef |
254 | 243 |
|
255 | 244 |
|
256 | 245 | # A node which is postponed to be processed during the next pass. |
@@ -283,7 +272,7 @@ class FineGrainedDeferredNode(NamedTuple): |
283 | 272 | # (such as two references to the same variable). TODO: it would |
284 | 273 | # probably be better to have the dict keyed by the nodes' literal_hash |
285 | 274 | # field instead. |
286 | | -TypeMap: _TypeAlias = Optional[dict[Expression, Type]] |
| 275 | +TypeMap: _TypeAlias = dict[Expression, Type] | None |
287 | 276 |
|
288 | 277 |
|
289 | 278 | # Keeps track of partial types in a single scope. In fine-grained incremental |
@@ -5309,16 +5298,22 @@ def get_types_from_except_handler(self, typ: Type, n: Expression) -> list[Type]: |
5309 | 5298 | """Helper for check_except_handler_test to retrieve handler types.""" |
5310 | 5299 | typ = get_proper_type(typ) |
5311 | 5300 | if isinstance(typ, TupleType): |
5312 | | - return typ.items |
| 5301 | + merged_type = make_simplified_union(typ.items) |
| 5302 | + if isinstance(merged_type, UnionType): |
| 5303 | + return merged_type.relevant_items() |
| 5304 | + return [merged_type] |
| 5305 | + elif is_named_instance(typ, "builtins.tuple"): |
| 5306 | + # variadic tuple |
| 5307 | + merged_type = make_simplified_union((typ.args[0],)) |
| 5308 | + if isinstance(merged_type, UnionType): |
| 5309 | + return merged_type.relevant_items() |
| 5310 | + return [merged_type] |
5313 | 5311 | elif isinstance(typ, UnionType): |
5314 | 5312 | return [ |
5315 | 5313 | union_typ |
5316 | 5314 | for item in typ.relevant_items() |
5317 | 5315 | for union_typ in self.get_types_from_except_handler(item, n) |
5318 | 5316 | ] |
5319 | | - elif is_named_instance(typ, "builtins.tuple"): |
5320 | | - # variadic tuple |
5321 | | - return [typ.args[0]] |
5322 | 5317 | else: |
5323 | 5318 | return [typ] |
5324 | 5319 |
|
|
0 commit comments