From ee69d3800022091f2b28d60aa5c7052fb87aedc3 Mon Sep 17 00:00:00 2001 From: David Peter Date: Tue, 22 Jul 2025 16:07:20 +0200 Subject: [PATCH] =?UTF-8?q?Fix=20panic=20for=20illegal=20`Literal[?= =?UTF-8?q?=E2=80=A6]`=20annotations=20with=20inner=20subscript=20expressi?= =?UTF-8?q?ons=20(#19489)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Summary Fixes pull-types panics for illegal annotations like `Literal[object[index]]`. Originally reported by @AlexWaygood ## Test Plan * Verified that this caused panics in the playground, when typing (and potentially hovering over) `x: Literal[obj[0]]`. * Added a regression test --- .../resources/mdtest/annotations/literal.md | 4 ++++ crates/ty_python_semantic/src/types/infer.rs | 1 + 2 files changed, 5 insertions(+) diff --git a/crates/ty_python_semantic/resources/mdtest/annotations/literal.md b/crates/ty_python_semantic/resources/mdtest/annotations/literal.md index 0c266868e1..3bd9e54c85 100644 --- a/crates/ty_python_semantic/resources/mdtest/annotations/literal.md +++ b/crates/ty_python_semantic/resources/mdtest/annotations/literal.md @@ -60,6 +60,10 @@ class NotAnEnum: # error: [invalid-type-form] invalid5: Literal[NotAnEnum.x] + +a_list: list[int] = [1, 2, 3] +# error: [invalid-type-form] +invalid6: Literal[a_list[0]] ``` ## Shortening unions of literals diff --git a/crates/ty_python_semantic/src/types/infer.rs b/crates/ty_python_semantic/src/types/infer.rs index 17644cc2f9..0f9733fafc 100644 --- a/crates/ty_python_semantic/src/types/infer.rs +++ b/crates/ty_python_semantic/src/types/infer.rs @@ -10438,6 +10438,7 @@ impl<'db> TypeInferenceBuilder<'db, '_> { self.store_expression_type(parameters, ty); ty } else { + self.infer_expression(slice); self.store_expression_type(parameters, Type::unknown()); return Err(vec![parameters]);