Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[red-knot] Statically known branches (first take) #14759

Closed
wants to merge 68 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
68 commits
Select commit Hold shift + click to select a range
26e3062
[red-knot] Statically known branches
sharkdp Nov 27, 2024
0075b4b
Use BitSet instead of HashSet
sharkdp Dec 5, 2024
8f781a9
Explain version bounds in tests
sharkdp Dec 6, 2024
f7d10b6
Add comment
sharkdp Dec 6, 2024
9d35a34
Handle static True/False for boundness
sharkdp Dec 8, 2024
3928934
Handle while loops
sharkdp Dec 9, 2024
acf7054
Rename to branching-condition
sharkdp Dec 9, 2024
d6b8acf
Handle unconditional branching
sharkdp Dec 9, 2024
531a38a
Same handling for definitions
sharkdp Dec 9, 2024
afd56f0
Remove comment
sharkdp Dec 9, 2024
6db9e8a
Add tests for 'while'-narrowing
sharkdp Dec 9, 2024
af922c9
More Boolean expression tests
sharkdp Dec 9, 2024
c29fbfc
More tests
sharkdp Dec 9, 2024
cab9c2f
Clarify TODO comments
sharkdp Dec 9, 2024
fabecf7
Add typing.TYPE_CHECKING
sharkdp Dec 9, 2024
50b6c1a
Infer Unknown instead of Never
sharkdp Dec 9, 2024
3868d75
Remove superfluous test
sharkdp Dec 9, 2024
659ef54
Proper for-loop handling
sharkdp Dec 9, 2024
88646aa
Renaming
sharkdp Dec 9, 2024
c56a50c
Clippy suggestions
sharkdp Dec 9, 2024
c9956bc
Fix typo
sharkdp Dec 9, 2024
46209e5
Missing word
sharkdp Dec 9, 2024
0762bf1
Some cleanup and performance work
sharkdp Dec 9, 2024
0336d4b
Simplify named-expression tests
sharkdp Dec 10, 2024
5db680e
Fix behavior of staticallyk-known undeclared symbols
sharkdp Dec 10, 2024
d590a49
Clippy suggestions
sharkdp Dec 10, 2024
0fda5cd
Use sub-headings instead of path=…
sharkdp Dec 10, 2024
544da53
Add test with break inside 'if True'
sharkdp Dec 10, 2024
00e97c3
Minor fixes in for-loop tests
sharkdp Dec 11, 2024
3f30833
Straighten definitions of bitsets
sharkdp Dec 11, 2024
4e10222
Documentation
sharkdp Dec 11, 2024
aeae261
Fix docs
sharkdp Dec 11, 2024
73c705d
Remove TODO
sharkdp Dec 11, 2024
e773c74
Handle control flow in try..else..finally
sharkdp Dec 11, 2024
8639b6b
Support analysis of 'match' statements
sharkdp Dec 11, 2024
888faad
Move static truthiness to new module
sharkdp Dec 11, 2024
02f9ae4
Move static-truthiness merging to dedicated module
sharkdp Dec 11, 2024
a17148e
Remove Clone
sharkdp Dec 11, 2024
b5fea25
Add documentation
sharkdp Dec 11, 2024
d2cd3de
Add doc comment
sharkdp Dec 11, 2024
fcae092
Clean up code in infer.rs
sharkdp Dec 11, 2024
e29f37b
Update bitset
sharkdp Dec 11, 2024
0ec165f
Reinstate tests
sharkdp Dec 11, 2024
86747ed
Minor fixes
sharkdp Dec 11, 2024
42da9ee
Minor
sharkdp Dec 11, 2024
33b9136
Bring back TODO comment
sharkdp Dec 11, 2024
c03be89
Remove Clone
sharkdp Dec 11, 2024
72d09eb
Visibility struct
sharkdp Dec 11, 2024
9c78c81
Add introduction section
sharkdp Dec 11, 2024
acf2ce6
Fix clippy suggestions
sharkdp Dec 11, 2024
04e80a8
Fix doc test
sharkdp Dec 11, 2024
bee7cf1
Minor doc update
sharkdp Dec 11, 2024
0746cec
Add tests for elif branches
sharkdp Dec 11, 2024
6ffa9e8
Minor doc updates
sharkdp Dec 11, 2024
57c9102
Update TODO
sharkdp Dec 11, 2024
37359bb
Add 'import sys'
sharkdp Dec 12, 2024
0372684
Fix x=1,2,3 values in test
sharkdp Dec 12, 2024
eae4796
Add tests for multiple 'elif True' / 'elif False' branches
sharkdp Dec 12, 2024
e59e8c9
Rename to record_ambiguous_branching
sharkdp Dec 12, 2024
88fef0b
Hard assert
sharkdp Dec 12, 2024
b43e9f2
Resolve TODO in tuple.md
sharkdp Dec 12, 2024
ae470f0
Document current limitations
sharkdp Dec 12, 2024
513795e
Snake case
sharkdp Dec 13, 2024
9b4bb86
Remove Truthiness::from_bool
sharkdp Dec 13, 2024
741beb7
Make is_ambiguous pub(crate)
sharkdp Dec 13, 2024
d3f460c
Derive Debug
sharkdp Dec 13, 2024
006a537
Fix rogue conflict resolution
sharkdp Dec 13, 2024
0a0e5e6
Minor
sharkdp Dec 13, 2024
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 @@ -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 @@ -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
Loading