mirror of
https://github.com/astral-sh/ruff.git
synced 2025-11-19 12:16:43 +00:00
[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:
parent
9664474c51
commit
ff3a6a8fbd
2 changed files with 27 additions and 7 deletions
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue