Avoid inferring types for invalid binary expressions in string annotations

This commit is contained in:
Charlie Marsh 2025-12-10 19:55:34 -05:00
parent 29bf2cd201
commit 0c00633346
2 changed files with 9 additions and 1 deletions

View File

@ -156,6 +156,9 @@ a: "1 or 2"
b: "(x := 1)" b: "(x := 1)"
# error: [invalid-type-form] # error: [invalid-type-form]
c: "1 + 2" c: "1 + 2"
# Regression test for https://github.com/astral-sh/ty/issues/1847
# error: [invalid-type-form]
c2: "a*(i for i in [])"
d: "lambda x: x" d: "lambda x: x"
e: "x if True else y" e: "x if True else y"
f: "{'a': 1, 'b': 2}" f: "{'a': 1, 'b': 2}"

View File

@ -155,7 +155,12 @@ impl<'db> TypeInferenceBuilder<'db, '_> {
} }
// anything else is an invalid annotation: // anything else is an invalid annotation:
op => { op => {
self.infer_binary_expression(binary, TypeContext::default()); // Avoid inferring the types of invalid binary expressions that have been
// parsed from a string annotation, as they are not present in the semantic
// index.
if !self.deferred_state.in_string_annotation() {
self.infer_binary_expression(binary, TypeContext::default());
}
self.report_invalid_type_expression( self.report_invalid_type_expression(
expression, expression,
format_args!( format_args!(