Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ x += (3, 4)
reveal_type(x) # revealed: tuple[Literal[1, 2, 3, 4], ...]
```

## Walrus target

```py
def f(xs: list[int | str]) -> None:
ys = xs
ys[0] = "s"
(ys := [1])[0] += 1
```

## Dunder methods

```py
Expand Down
14 changes: 14 additions & 0 deletions crates/ty_python_semantic/resources/mdtest/attributes.md
Original file line number Diff line number Diff line change
Expand Up @@ -2504,6 +2504,20 @@ class C:
C().x
```

### Walrus reassignment of `self`

```py
class Other:
x: int = 1

class C:
def __init__(self, other: Other) -> None:
(self := other).x = 1

# error: [unresolved-attribute]
reveal_type(C(Other()).x) # revealed: Unknown
```

### Assignment to `self` after nested function

```py
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,18 @@ def _(flag: bool):
# error: [unresolved-attribute] "Class `A` has no attribute `non_existent`"
reveal_type(A.non_existent) # revealed: Unknown
```

## Walrus attribute access after later rebinding

```py
class IntBox:
attr: int

class StrBox:
attr: str

def f() -> None:
(box := IntBox()).attr = 1
box = StrBox()
reveal_type(box.attr) # revealed: str
```
21 changes: 21 additions & 0 deletions crates/ty_python_semantic/resources/mdtest/narrow/truthiness.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,27 @@ else:
reveal_type(x) # revealed: Literal[b"", b"bar", 0, False, ""] | None | tuple[()]
```

## Walrus Member Access

We can narrow on an attribute expression, even when its base is a named expression:

```py
class Foo:
val: int | None

if (foo := Foo()).val:
reveal_type(foo.val) # revealed: int & ~AlwaysFalsy
```

But we don't pick up stale narrowings from before the assignment in the named expression:

```py
foo1 = Foo()
foo1.val = None
if (foo1 := Foo()).val:
reveal_type(foo1.val) # revealed: int & ~AlwaysFalsy
```

## Function Literals

Basically functions are always truthy.
Expand Down
27 changes: 27 additions & 0 deletions crates/ty_python_semantic/resources/mdtest/subscript/lists.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,30 @@ x["a" if (y := 2) else 1] = 6
# error: [invalid-assignment]
x["a" if (y := 2) else "b"] = 6
```

## Walrus subscript access

```py
xs: list[int | None] = [1]
xs[0] = None

reveal_type((xs := [1])[0]) # revealed: int | None
```

## Walrus subscript access after rebinding

```py
def f(xs: list[int | str]) -> None:
ys = xs
ys[0] = "s"
reveal_type((ys := [1])[0]) # revealed: int
```

## Walrus subscript access after later rebinding

```py
def f() -> None:
(ys := [1])[0] = 2
ys = ["s"]
reveal_type(ys[0]) # revealed: str
```
Loading
Loading