8000 Typecheck unreachable code by A5rocks · Pull Request #18707 · python/mypy · GitHub
[go: up one dir, main page]

Skip to content

Typecheck unreachable code #18707

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

Open
wants to merge 46 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
be1071e
Start type checking unreachable code
A5rocks Jan 24, 2025
d996918
Pass self-check
A5rocks Feb 19, 2025
07de5bf
Hacky fix to narrow unreachable literals
A5rocks Feb 19, 2025
bac268b
20% of tests
A5rocks Feb 19, 2025
2da4b7c
25% of tests
A5rocks Feb 19, 2025
ba9dab2
30% of tests
A5rocks Feb 19, 2025
7c39efa
40% of tests
A5rocks Feb 19, 2025
18642d0
50% of tests
A5rocks Feb 19, 2025
879b274
60% of tests
A5rocks Feb 19, 2025
c62093e
Pass all tests
A5rocks Feb 19, 2025
e242b5e
Narrow `isinstance` checks to `Never`
A5rocks Feb 20, 2025
69efb41
Don't treat `reveal_type(<Never>)` as a no-op
A5rocks Feb 20, 2025
f846af7
Ensure functions with typevar values don't get false positives
A5rocks Feb 21, 2025
390ade8
Ensure that `<Never>.x` is `Never`
A5rocks Feb 21, 2025
f3ba149
Ensure that `<Never>()` is `Never`
A5rocks Feb 21, 2025
92a4428
Only report unreachability once
A5rocks Feb 24, 2025
c76d422
Narrow `callable` checks to `Never`
A5rocks Feb 24, 2025
be1d524
Address CI failures
A5rocks Feb 24, 2025
13ad1cb
Narrow `match` captures to `Never`
A5rocks Feb 24, 2025
53553e7
Don't check unreachable code with partial types
A5rocks Feb 24, 2025
6cc9930
Avoid strange behavior regarding "narrowing" expressions for unreacha…
A5rocks Feb 24, 2025
4ddd34e
Unpack `Never` iterables
A5rocks Feb 24, 2025
3cda7db
Narrow disjoint narrows to `Never`
A5rocks Mar 3, 2025
6e97289
Fix `redundant-expr` for comprehensions
A5rocks Mar 3, 2025
28894ce
Narrow `issubclass` to `Never`
A5rocks Mar 3, 2025
be6573e
Narrow things based on key types even if the type is `Never`
A5rocks Mar 3, 2025
4718cfd
Update some tests
A5rocks Mar 4, 2025
475f8b1
`typing.assert_never(5)` is fine in unreachable code
A5rocks Mar 4, 2025
72349c0
Non-ambiguous `Never` is fine as inference result
A5rocks Mar 4, 2025
ca11151
Implement or-ing typemaps for `Never`-based unreachability
A5rocks Mar 6, 2025
22e73d5
Tuple length checks should narrow to `Never`
A5rocks Mar 6, 2025
2bd0802
Narrow walrus to `Never` if unreachable
A5rocks Mar 6, 2025
8fb6fab
Narrow unreachable captures due to guards in `match` to `Never`
A5rocks Mar 6, 2025
0f41d33
Narrow impossible values in mapping pattern to `Never`
A5rocks Mar 6, 2025
b641ed4
Smooth out edges
A5rocks Mar 6, 2025
dcc4f29
Propagate suppressing unreachability warnings
A5rocks Mar 7, 2025
98abde4
Fix typemap handling for ternaries
A5rocks Mar 9, 2025
390388a
Ensure `--strict-equality` works correctly with partial types
A5rocks Mar 9, 2025
dc8ed71
Support `Never` callables in plugins
A5rocks Mar 13, 2025
102ac11
Add a test about narrowing behavior
A5rocks Mar 13, 2025
f7f8915
Merge remote-tracking branch 'upstream/master' into experiments/typec…
A5rocks Mar 17, 2025
bc8cb90
Merge branch 'master' into experiments/typecheck-unreachable
A5rocks Mar 31, 2025
ac03c88
Address PR review
A5rocks Apr 9, 2025
4f319bb
Add an option to enable checking unreachable code
A5rocks Apr 9, 2025
d5e4a47
Update test cases to pass
A5rocks Apr 9, 2025
730d9ee
Don't typecheck unreachable noops
A5rocks Apr 9, 2025
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
Prev Previous commit
Next Next commit
Update test cases to pass
  • Loading branch information
A5rocks committed Apr 9, 2025
commit d5e4a475205c79928bb437c33af590114b51ec9d
14 changes: 10 additions & 4 deletions mypyc/test-data/irbuild-match.test
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -1765,8 +1765,10 @@ def f(x):
r10 :: object
r11 :: object[1]
r12 :: object_ptr
r13 :: object
r14 :: bool
r13, r14 :: object
r15 :: i32
r16 :: bit
r17, r18 :: bool
L0:
r0 = __main__.Foo :: type
r1 = PyObject_IsInstance(x, r0)
Expand All @@ -1790,9 +1792,13 @@ L2:
goto L8
L3:
L4:
if 0 goto L6 else goto L5 :: bool
r14 = box(bool, 0)
r15 = PyObject_IsTrue(r14)
r16 = r15 >= 0 :: signed
r17 = truncate r15: i32 to builtins.bool
if r17 goto L6 else goto L5 :: bool
L5:
r14 = raise AssertionError('Unreachable')
r18 = raise AssertionError('Unreachable')
unreachable
L6:
goto L8
Expand Down
11 changes: 7 additions & 4 deletions test-data/unit/daemon.test
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,8 @@ $ dmypy inspect foo.py:1:2:3:4
Can't find expression at span 1:2:3:4
== Return code: 1
$ dmypy inspect foo.py:17:5:17:5
"int"
No known type available for "NameExpr" (maybe unreachable or try --force-reload)
== Return code: 1

[file foo.py]
from typing import Optional
Expand Down Expand Up @@ -522,8 +523,9 @@ $ dmypy inspect foo.py:1:2
Can't find any expressions at position 1:2
== Return code: 1
$ dmypy inspect foo.py:11:5 --force-reload
"int"
"int"
No known type available for "NameExpr" (maybe unreachable)
No known type available for "OpExpr" (maybe unreachable)
== Return code: 1

[file foo.py]
from typing import Optional
Expand All @@ -545,7 +547,8 @@ $ dmypy check foo.py bar.py --export-types
$ dmypy inspect foo.py:9:1 --show attrs --include-span --include-kind -vv
NameExpr:9:1:9:1 -> {"foo.C": ["a", "x", "y"], "foo.B": ["a", "b"]}
$ dmypy inspect foo.py:11:10 --show attrs
{"str": ["__add__", "__contains__", "__eq__", "__ge__", "__getitem__", "__getnewargs__", "__gt__", "__hash__", "__iter__", "__le__", "__len__", "__lt__", "__mod__", "__mul__", "__ne__", "__new__", "__rmul__", "capitalize", "casefold", "center", "count", "encode", "endswith", "expandtabs", "find", "format", "format_map", "index", "isalnum", "isalpha", "isascii", "isdecimal", "isdigit", "is 8000 identifier", "islower", "isnumeric", "isprintable", "isspace", "istitle", "isupper", "join", "ljust", "lower", "lstrip", "maketrans", "partition", "removeprefix", "removesuffix", "replace", "rfind", "rindex", "rjust", "rpartition", "rsplit", "rstrip", "split", "splitlines", "startswith", "strip", "swapcase", "title", "translate", "upper", "zfill"], "Sequence": ["__contains__", "__getitem__", "__iter__", "__reversed__", "count", "index"], "Reversible": ["__reversed__"], "Collection": ["__len__"], "Iterable": ["__iter__"], "Container": ["__contains__"]}
No known type available for "StrExpr" (maybe unreachable or try --force-reload)
== Return code: 1
$ dmypy inspect foo.py:1:1 --show attrs
Can't find any expressions at position 1:1
== Return code: 1
Expand Down
6 changes: 4 additions & 2 deletions test-data/unit/fine-grained.test
Original file line number Diff line number Diff line change
Expand Up @@ -9692,7 +9692,8 @@ reveal_type(z)
[out]
c.py:2: note: Revealed type is "a.<subclass of "a.A" and "a.B">"
==
c.py:2: note: Revealed type is "Never"
c.py:2: note: Revealed type is "Any"
b.py:2: error: Cannot determine type of "y"

[case testIsInstanceAdHocIntersectionFineGrainedIncrementalUnreachableToIntersection]
import c
Expand Down Expand Up @@ -9723,7 +9724,8 @@ from b import z
reveal_type(z)
[builtins fixtures/isinstance.pyi]
[out]
c.py:2: note: Revealed type is "Never"
b.py:2: error: Cannot determine type of "y"
c.py:2: note: Revealed type is "Any"
==
c.py:2: note: Revealed type is "a.<subclass of "a.A" and "a.B">"

Expand Down
3 changes: 0 additions & 3 deletions test-data/unit/pythoneval.test
Original file line number Diff line number Diff line change
Expand Up @@ -1481,10 +1481,8 @@ if isinstance(x, int):
[out]
_testIsInstanceAdHocIntersectionWithStrAndBytes.py:3: error: Subclass of "str" and "bytes" cannot exist: would have incompatible method signatures
_testIsInstanceAdHocIntersectionWithStrAndBytes.py:4: error: Statement is unreachable
_testIsInstanceAdHocIntersectionWithStrAndBytes.py:4: note: Revealed type is "Never"
_testIsInstanceAdHocIntersectionWithStrAndBytes.py:6: error: Subclass of "str" and "int" cannot exist: would have incompatible method signatures
_testIsInstanceAdHocIntersectionWithStrAndBytes.py:7: error: Statement is unreachable
_testIsInstanceAdHocIntersectionWithStrAndBytes.py:7: note: Revealed type is "Never"

[case testAsyncioFutureWait]
# mypy: strict-optional
Expand Down Expand Up @@ -1555,7 +1553,6 @@ if isinstance(obj, Awaitable):
[out]
_te 8000 stSpecialTypingProtocols.py:6: note: Revealed type is "Tuple[builtins.int]"
_testSpecialTypingProtocols.py:8: error: Statement is unreachable
_testSpecialTypingProtocols.py:8: note: Revealed type is "Never"

[case testTypeshedRecursiveTypesExample]
from typing import List, Union
Expand Down
7 changes: 3 additions & 4 deletions test-data/unit/reports.test
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,7 @@ def bar(x):
<span class="line-any" title="Any Types on this line:
Explicit (x1)"> any_f(x)</span>
<span class="line-precise" title="No Anys on this line!"> assert False</span>
<span class="line-any" title="Any Types on this line:
Explicit (x1)"> any_f(x)</span>
<span class="line-unanalyzed" title="No Anys on this line!"> any_f(x)</span>
</pre></td>
</tr></tbody>
</table>
Expand Down Expand Up @@ -297,10 +296,10 @@ def bar(x):
[outfile report/any-exprs.txt]
Name Anys Exprs Coverage
---------------------------------
i 0 8 100.00%
i 1 6 83.33%
j 0 5 100.00%
---------------------------------
Total 0 13 100.00%
Total 1 11 90.91%

[case testAnyExprReportHigherKindedTypesAreNotAny]
# cmd: mypy --any-exprs-report report i.py
Expand Down
0