ruff
a582555c - [ty] Fix structural pattern binding inference (#26411)

Commit
96 days ago
[ty] Fix structural pattern binding inference (#26411) ## Summary Follow-up to #25941 and prerequisite for #25942. The earlier PR extended successful-pattern binding inference to class and mapping patterns. This PR fixes edge cases in that analysis before the same structural machinery is reused for subject narrowing. ```python from typing import Literal class IntPayload: tag: Literal["int"] payload: int class StrPayload: tag: Literal["str"] payload: str def handle( value: object, Pattern: type[IntPayload] | type[StrPayload], ) -> None: match value: case Pattern(tag="int", payload=item): reveal_type(item) # int ``` Union-valued class expressions are now analyzed one class at a time, so a discriminating child pattern also selects the corresponding member types. Generic class patterns preserve specializations known from the subject, while unrelated non-final classes retain member types that can overlap through multiple inheritance. Runtime class matching uses an unknown specialization rather than a type-parameter default. Mapping entries are extracted after the successful pattern has established the mapping interface, which keeps fully static `object` subjects from introducing `Unknown`. Later `or`-pattern alternatives use mutation-safe fallthrough types, so a failed sequence alternative does not leave stale shape constraints on a value captured by another alternative.
Author
Parents
Loading