[ty] Support type context of union attribute assignments (#21170)

## Summary

Turns out this is easy to implement. Resolves
https://github.com/astral-sh/ty/issues/1375.
This commit is contained in:
Ibraheem Ahmed 2025-10-31 12:41:14 -04:00 committed by GitHub
parent 9664474c51
commit ff3a6a8fbd
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 27 additions and 7 deletions

View file

@ -200,7 +200,7 @@ def f() -> list[Literal[1]]:
return [1]
```
## Instance attribute
## Instance attributes
```toml
[environment]
@ -235,6 +235,24 @@ def _(flag: bool):
C.x = lst(1)
```
For union targets, each element of the union is considered as a separate type context:
```py
from typing import Literal
class X:
x: list[int | str]
class Y:
x: list[int | None]
def lst[T](x: T) -> list[T]:
return [x]
def _(xy: X | Y):
xy.x = lst(1)
```
## Class constructor parameters
```toml