Skip to content

Commit

Permalink
[red-knot] Statically known branches
Browse files Browse the repository at this point in the history
  • Loading branch information
sharkdp committed Dec 16, 2024
1 parent a623d8f commit 44bbbb8
Show file tree
Hide file tree
Showing 21 changed files with 2,470 additions and 668 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ def f():

## `typing.Never`

`typing.Never` is only available in Python 3.11 and later:
`typing.Never` is only available in Python 3.11 and later.

### Python 3.11

```toml
[environment]
Expand All @@ -57,8 +59,17 @@ python-version = "3.11"
```py
from typing import Never

x: Never
reveal_type(Never) # revealed: typing.Never
```

def f():
reveal_type(x) # revealed: Never
### Python 3.10

```toml
[environment]
python-version = "3.10"
```

```py
# error: [unresolved-import]
from typing import Never
```
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ b: tuple[int] = (42,)
c: tuple[str, int] = ("42", 42)
d: tuple[tuple[str, str], tuple[int, int]] = (("foo", "foo"), (42, 42))
e: tuple[str, ...] = ()
# TODO: we should not emit this error
# error: [call-possibly-unbound-method] "Method `__class_getitem__` of type `Literal[tuple]` is possibly unbound"
f: tuple[str, *tuple[int, ...], bytes] = ("42", b"42")
g: tuple[str, Unpack[tuple[int, ...]], bytes] = ("42", b"42")
h: tuple[list[int], list[int]] = ([], [])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,10 @@ def _(flag: bool):

```py
if True or (x := 1):
# TODO: infer that the second arm is never executed, and raise `unresolved-reference`.
# error: [possibly-unresolved-reference]
reveal_type(x) # revealed: Literal[1]
# error: [unresolved-reference]
reveal_type(x) # revealed: Unknown

if True and (x := 1):
# TODO: infer that the second arm is always executed, do not raise a diagnostic
# error: [possibly-unresolved-reference]
reveal_type(x) # revealed: Literal[1]
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,14 @@ def _(flag: bool):
## Partial declarations

```py
def _(flag: bool):
if flag:
def _(flag1: bool, flag2: bool):
if flag1:
x: str
elif flag2:
x: int

x = 1 # error: [conflicting-declarations] "Conflicting declared types for `x`: Unknown, int"
# Here, the declared type for `x` is `int | str | Unknown`.
x = 1 # error: [conflicting-declarations] "Conflicting declared types for `x`: str, int"
```

## Incompatible declarations with bad assignment
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def _(flag: bool):
reveal_type(1 if flag else 2) # revealed: Literal[1, 2]
```

## Statically known branches
## Statically known conditions in if-expressions

```py
reveal_type(1 if True else 2) # revealed: Literal[1]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,23 @@
# Ellipsis literals

## Simple
## Python 3.9

```toml
[environment]
python-version = "3.9"
```

```py
reveal_type(...) # revealed: ellipsis
```

## Python 3.10

```toml
[environment]
python-version = "3.10"
```

```py
reveal_type(...) # revealed: EllipsisType | ellipsis
reveal_type(...) # revealed: EllipsisType
```
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,14 @@ def _(t: type[object]):

### Handling of `None`

`types.NoneType` is only available in Python 3.10 and later:

```toml
[environment]
python-version = "3.10"
```

```py
# TODO: this error should ideally go away once we (1) understand `sys.version_info` branches,
# and (2) set the target Python version for this test to 3.10.
# error: [possibly-unbound-import] "Member `NoneType` of module `types` is possibly unbound"
from types import NoneType

def _(flag: bool):
Expand Down
Loading

0 comments on commit 44bbbb8

Please sign in to comment.