Skip to content

Commit 3c6fbe7

Browse files
committed
Add expression_type_or_unknown() method to UnpackResult
1 parent 67a4f92 commit 3c6fbe7

3 files changed

Lines changed: 18 additions & 29 deletions

File tree

crates/ty_python_semantic/src/types/class/static_literal.rs

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2154,11 +2154,7 @@ impl<'db> StaticClassLiteral<'db> {
21542154
// [.., self.name, ..] = <value>
21552155

21562156
let unpacked = infer_unpack_types(db, unpack);
2157-
Some(
2158-
unpacked
2159-
.try_expression_type(assign.target(&module))
2160-
.unwrap_or_else(Type::unknown),
2161-
)
2157+
Some(unpacked.expression_type_or_unknown(assign.target(&module)))
21622158
}
21632159
TargetKind::Single => {
21642160
// We found an un-annotated attribute assignment of the form:
@@ -2179,11 +2175,7 @@ impl<'db> StaticClassLiteral<'db> {
21792175
// for .., self.name, .. in <iterable>:
21802176

21812177
let unpacked = infer_unpack_types(db, unpack);
2182-
Some(
2183-
unpacked
2184-
.try_expression_type(for_stmt.target(&module))
2185-
.unwrap_or_else(Type::unknown),
2186-
)
2178+
Some(unpacked.expression_type_or_unknown(for_stmt.target(&module)))
21872179
}
21882180
TargetKind::Single => {
21892181
// We found an attribute assignment like:
@@ -2206,11 +2198,7 @@ impl<'db> StaticClassLiteral<'db> {
22062198
// with <context_manager> as .., self.name, ..:
22072199

22082200
let unpacked = infer_unpack_types(db, unpack);
2209-
Some(
2210-
unpacked
2211-
.try_expression_type(with_item.target(&module))
2212-
.unwrap_or_else(Type::unknown),
2213-
)
2201+
Some(unpacked.expression_type_or_unknown(with_item.target(&module)))
22142202
}
22152203
TargetKind::Single => {
22162204
// We found an attribute assignment like:
@@ -2239,8 +2227,7 @@ impl<'db> StaticClassLiteral<'db> {
22392227
let unpacked = infer_unpack_types(db, unpack);
22402228
Some(
22412229
unpacked
2242-
.try_expression_type(comprehension.target(&module))
2243-
.unwrap_or_else(Type::unknown),
2230+
.expression_type_or_unknown(comprehension.target(&module)),
22442231
)
22452232
}
22462233
TargetKind::Single => {

crates/ty_python_semantic/src/types/infer/builder.rs

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1617,9 +1617,7 @@ impl<'db, 'ast> TypeInferenceBuilder<'db, 'ast> {
16171617
if unpack_position == UnpackPosition::First {
16181618
self.context.extend(unpacked.diagnostics());
16191619
}
1620-
unpacked
1621-
.try_expression_type(target)
1622-
.unwrap_or_else(Type::unknown)
1620+
unpacked.expression_type_or_unknown(target)
16231621
}
16241622
TargetKind::Single => {
16251623
let context_expr_ty =
@@ -3160,9 +3158,7 @@ impl<'db, 'ast> TypeInferenceBuilder<'db, 'ast> {
31603158
self.context.extend(unpacked.diagnostics());
31613159
}
31623160

3163-
unpacked
3164-
.try_expression_type(target)
3165-
.unwrap_or_else(Type::unknown)
3161+
unpacked.expression_type_or_unknown(target)
31663162
}
31673163
TargetKind::Single => {
31683164
// This could be an implicit type alias (OptionalList = list[T] | None). Use the definition
@@ -4335,9 +4331,7 @@ impl<'db, 'ast> TypeInferenceBuilder<'db, 'ast> {
43354331
self.context.extend(unpacked.diagnostics());
43364332
}
43374333

4338-
unpacked
4339-
.try_expression_type(target)
4340-
.unwrap_or_else(Type::unknown)
4334+
unpacked.expression_type_or_unknown(target)
43414335
}
43424336
TargetKind::Single => {
43434337
let iterable_type =
@@ -6424,9 +6418,7 @@ impl<'db, 'ast> TypeInferenceBuilder<'db, 'ast> {
64246418
self.context.extend(unpacked.diagnostics());
64256419
}
64266420

6427-
unpacked
6428-
.try_expression_type(target)
6429-
.unwrap_or_else(Type::unknown)
6421+
unpacked.expression_type_or_unknown(target)
64306422
}
64316423
TargetKind::Single => {
64326424
let iterable_type = infer_iterable_type();

crates/ty_python_semantic/src/types/unpacker.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,16 @@ impl<'db> UnpackResult<'db> {
291291
.or(self.cycle_recovery)
292292
}
293293

294+
/// Returns the type of the given expression, or `Unknown` if no type was recorded.
295+
///
296+
/// This can happen for malformed unpack targets created by error recovery.
297+
pub(crate) fn expression_type_or_unknown(
298+
&self,
299+
expr: impl Into<ExpressionNodeKey>,
300+
) -> Type<'db> {
301+
self.try_expression_type(expr).unwrap_or_else(Type::unknown)
302+
}
303+
294304
/// Returns the diagnostics in this unpacking assignment.
295305
pub(crate) fn diagnostics(&self) -> &TypeCheckDiagnostics {
296306
&self.diagnostics

0 commit comments

Comments
 (0)