Skip to content

Commit 6d2c10c

Browse files
authored
[red-knot] Fix panic for tuple[x[y]] string annotation (#17787)
## Summary closes #17775 ## Test Plan Added corpus regression test
1 parent 3cf44e4 commit 6d2c10c

File tree

4 files changed

+20
-14
lines changed

4 files changed

+20
-14
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
t: "tuple[list[int]]"
Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,30 @@
11
Expression # cycle panic (signature_)
22
Tanjun # cycle panic (signature_)
3-
aiohttp # missing expression ID
4-
alerta # missing expression ID
53
altair # cycle panics (try_metaclass_)
64
antidote # hangs / slow
75
artigraph # cycle panics (value_type_)
86
colour # cycle panics (try_metaclass_)
97
core # cycle panics (value_type_)
10-
cpython # missing expression ID, access to field whilst being initialized, too many cycle iterations
8+
cpython # access to field whilst being initialized, too many cycle iterations
119
discord.py # some kind of hang, only when multi-threaded?
1210
freqtrade # cycle panics (try_metaclass_)
1311
hydpy # cycle panics (try_metaclass_)
1412
ibis # cycle panics (try_metaclass_)
1513
manticore # stack overflow
1614
materialize # stack overflow
17-
meson # missing expression ID
1815
mypy # cycle panic (signature_)
1916
pandas # slow
2017
pandas-stubs # cycle panics (try_metaclass_)
2118
pandera # cycle panics (try_metaclass_)
2219
prefect # slow
23-
pytest # cycle panics (signature_), missing expression ID
20+
pytest # cycle panics (signature_)
2421
pywin32 # bad use-def map (binding with definitely-visible unbound)
2522
schemathesis # cycle panics (signature_)
2623
scikit-learn # success, but mypy-primer hangs processing the output
27-
scipy # missing expression ID
24+
scipy # missing expression type ("expression should belong to this TypeInference region")
2825
spack # success, but mypy-primer hangs processing the output
2926
spark # cycle panics (try_metaclass_)
30-
sphinx # missing expression ID
31-
steam.py # missing expression ID
27+
steam.py # cycle panics (try_metaclass_), often hangs when multi-threaded
3228
streamlit # cycle panic (signature_)
3329
sympy # stack overflow
34-
trio # missing expression ID
3530
xarray # cycle panics (try_metaclass_)

crates/red_knot_python_semantic/resources/primer/good.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ AutoSplit
22
PyGithub
33
PyWinCtl
44
SinbadCogs
5+
aiohttp
56
aiohttp-devtools
67
aioredis
78
aiortc
89
alectryon
10+
alerta
911
anyio
1012
apprise
1113
arviz
@@ -47,6 +49,7 @@ jinja
4749
koda-validate
4850
kopf
4951
kornia
52+
meson
5053
mitmproxy
5154
mkdocs
5255
mkosi
@@ -92,11 +95,13 @@ scrapy
9295
setuptools
9396
sockeye
9497
speedrun.com_global_scoreboard_webapp
98+
sphinx
9599
starlette
96100
static-frame
97101
stone
98102
strawberry
99103
tornado
104+
trio
100105
twine
101106
typeshed-stats
102107
urllib3

crates/red_knot_python_semantic/src/types/infer.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7615,7 +7615,7 @@ impl<'db> TypeInferenceBuilder<'db> {
76157615
fn element_could_alter_type_of_whole_tuple(
76167616
element: &ast::Expr,
76177617
element_ty: Type,
7618-
builder: &TypeInferenceBuilder,
7618+
builder: &mut TypeInferenceBuilder,
76197619
) -> bool {
76207620
if !element_ty.is_todo() {
76217621
return false;
@@ -7624,10 +7624,15 @@ impl<'db> TypeInferenceBuilder<'db> {
76247624
match element {
76257625
ast::Expr::EllipsisLiteral(_) | ast::Expr::Starred(_) => true,
76267626
ast::Expr::Subscript(ast::ExprSubscript { value, .. }) => {
7627-
matches!(
7628-
builder.expression_type(value),
7629-
Type::KnownInstance(KnownInstanceType::Unpack)
7630-
)
7627+
let value_ty = if builder.deferred_state.in_string_annotation() {
7628+
// Using `.expression_type` does not work in string annotations, because
7629+
// we do not store types for sub-expressions. Re-infer the type here.
7630+
builder.infer_expression(value)
7631+
} else {
7632+
builder.expression_type(value)
7633+
};
7634+
7635+
matches!(value_ty, Type::KnownInstance(KnownInstanceType::Unpack))
76317636
}
76327637
_ => false,
76337638
}

0 commit comments

Comments
 (0)