[red-knot] Fix panic for tuple[x[y]] string annotation (#17787)

## Summary

closes #17775

## Test Plan

Added corpus regression test
This commit is contained in:
David Peter 2025-05-02 12:11:47 +02:00 committed by GitHub
parent 3cf44e401a
commit 6d2c10cca2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 20 additions and 14 deletions

View file

@ -0,0 +1 @@
t: "tuple[list[int]]"

View file

@ -1,35 +1,30 @@
Expression # cycle panic (signature_) Expression # cycle panic (signature_)
Tanjun # cycle panic (signature_) Tanjun # cycle panic (signature_)
aiohttp # missing expression ID
alerta # missing expression ID
altair # cycle panics (try_metaclass_) altair # cycle panics (try_metaclass_)
antidote # hangs / slow antidote # hangs / slow
artigraph # cycle panics (value_type_) artigraph # cycle panics (value_type_)
colour # cycle panics (try_metaclass_) colour # cycle panics (try_metaclass_)
core # cycle panics (value_type_) core # cycle panics (value_type_)
cpython # missing expression ID, access to field whilst being initialized, too many cycle iterations cpython # access to field whilst being initialized, too many cycle iterations
discord.py # some kind of hang, only when multi-threaded? discord.py # some kind of hang, only when multi-threaded?
freqtrade # cycle panics (try_metaclass_) freqtrade # cycle panics (try_metaclass_)
hydpy # cycle panics (try_metaclass_) hydpy # cycle panics (try_metaclass_)
ibis # cycle panics (try_metaclass_) ibis # cycle panics (try_metaclass_)
manticore # stack overflow manticore # stack overflow
materialize # stack overflow materialize # stack overflow
meson # missing expression ID
mypy # cycle panic (signature_) mypy # cycle panic (signature_)
pandas # slow pandas # slow
pandas-stubs # cycle panics (try_metaclass_) pandas-stubs # cycle panics (try_metaclass_)
pandera # cycle panics (try_metaclass_) pandera # cycle panics (try_metaclass_)
prefect # slow prefect # slow
pytest # cycle panics (signature_), missing expression ID pytest # cycle panics (signature_)
pywin32 # bad use-def map (binding with definitely-visible unbound) pywin32 # bad use-def map (binding with definitely-visible unbound)
schemathesis # cycle panics (signature_) schemathesis # cycle panics (signature_)
scikit-learn # success, but mypy-primer hangs processing the output scikit-learn # success, but mypy-primer hangs processing the output
scipy # missing expression ID scipy # missing expression type ("expression should belong to this TypeInference region")
spack # success, but mypy-primer hangs processing the output spack # success, but mypy-primer hangs processing the output
spark # cycle panics (try_metaclass_) spark # cycle panics (try_metaclass_)
sphinx # missing expression ID steam.py # cycle panics (try_metaclass_), often hangs when multi-threaded
steam.py # missing expression ID
streamlit # cycle panic (signature_) streamlit # cycle panic (signature_)
sympy # stack overflow sympy # stack overflow
trio # missing expression ID
xarray # cycle panics (try_metaclass_) xarray # cycle panics (try_metaclass_)

View file

@ -2,10 +2,12 @@ AutoSplit
PyGithub PyGithub
PyWinCtl PyWinCtl
SinbadCogs SinbadCogs
aiohttp
aiohttp-devtools aiohttp-devtools
aioredis aioredis
aiortc aiortc
alectryon alectryon
alerta
anyio anyio
apprise apprise
arviz arviz
@ -47,6 +49,7 @@ jinja
koda-validate koda-validate
kopf kopf
kornia kornia
meson
mitmproxy mitmproxy
mkdocs mkdocs
mkosi mkosi
@ -92,11 +95,13 @@ scrapy
setuptools setuptools
sockeye sockeye
speedrun.com_global_scoreboard_webapp speedrun.com_global_scoreboard_webapp
sphinx
starlette starlette
static-frame static-frame
stone stone
strawberry strawberry
tornado tornado
trio
twine twine
typeshed-stats typeshed-stats
urllib3 urllib3

View file

@ -7615,7 +7615,7 @@ impl<'db> TypeInferenceBuilder<'db> {
fn element_could_alter_type_of_whole_tuple( fn element_could_alter_type_of_whole_tuple(
element: &ast::Expr, element: &ast::Expr,
element_ty: Type, element_ty: Type,
builder: &TypeInferenceBuilder, builder: &mut TypeInferenceBuilder,
) -> bool { ) -> bool {
if !element_ty.is_todo() { if !element_ty.is_todo() {
return false; return false;
@ -7624,10 +7624,15 @@ impl<'db> TypeInferenceBuilder<'db> {
match element { match element {
ast::Expr::EllipsisLiteral(_) | ast::Expr::Starred(_) => true, ast::Expr::EllipsisLiteral(_) | ast::Expr::Starred(_) => true,
ast::Expr::Subscript(ast::ExprSubscript { value, .. }) => { ast::Expr::Subscript(ast::ExprSubscript { value, .. }) => {
matches!( let value_ty = if builder.deferred_state.in_string_annotation() {
builder.expression_type(value), // Using `.expression_type` does not work in string annotations, because
Type::KnownInstance(KnownInstanceType::Unpack) // we do not store types for sub-expressions. Re-infer the type here.
) builder.infer_expression(value)
} else {
builder.expression_type(value)
};
matches!(value_ty, Type::KnownInstance(KnownInstanceType::Unpack))
} }
_ => false, _ => false,
} }