Skip to content

Commit 6d6eb5e

Browse files
committed
Review findings
1 parent f9e1bf2 commit 6d6eb5e

File tree

3 files changed

+24
-36
lines changed

3 files changed

+24
-36
lines changed

crates/ty_python_semantic/src/types.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7861,8 +7861,10 @@ impl<'db> KnownInstanceType<'db> {
78617861
)
78627862
}
78637863
KnownInstanceType::UnionType(_) => f.write_str("types.UnionType"),
7864-
KnownInstanceType::Literal(_) => f.write_str("typing.Literal"),
7865-
KnownInstanceType::Annotated(_) => f.write_str("typing.Annotated"),
7864+
KnownInstanceType::Literal(_) => f.write_str("<typing.Literal special form>"),
7865+
KnownInstanceType::Annotated(_) => {
7866+
f.write_str("<typing.Annotated special form>")
7867+
}
78667868
}
78677869
}
78687870
}
@@ -8059,7 +8061,7 @@ impl<'db> InvalidTypeExpressionError<'db> {
80598061
fn into_fallback_type(
80608062
self,
80618063
context: &InferContext,
8062-
node: &ast::Expr,
8064+
node: &impl Ranged,
80638065
is_reachable: bool,
80648066
) -> Type<'db> {
80658067
let InvalidTypeExpressionError {

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9867,14 +9867,23 @@ impl<'db, 'ast> TypeInferenceBuilder<'db, 'ast> {
98679867
}
98689868

98699869
fn infer_subscript_load(&mut self, subscript: &ast::ExprSubscript) -> Type<'db> {
9870+
let value_ty = self.infer_expression(&subscript.value, TypeContext::default());
9871+
self.infer_subscript_load_impl(value_ty, subscript)
9872+
}
9873+
9874+
fn infer_subscript_load_impl(
9875+
&mut self,
9876+
value_ty: Type<'db>,
9877+
subscript: &ast::ExprSubscript,
9878+
) -> Type<'db> {
98709879
let ast::ExprSubscript {
98719880
range: _,
98729881
node_index: _,
9873-
value,
9882+
value: _,
98749883
slice,
98759884
ctx,
98769885
} = subscript;
9877-
let value_ty = self.infer_expression(value, TypeContext::default());
9886+
98789887
let mut constraint_keys = vec![];
98799888

98809889
// If `value` is a valid reference, we attempt type narrowing by assignment.

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

Lines changed: 8 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use ruff_python_ast as ast;
44
use super::{DeferredExpressionState, TypeInferenceBuilder};
55
use crate::types::diagnostic::{
66
self, INVALID_TYPE_FORM, NON_SUBSCRIPTABLE, report_invalid_argument_number_to_special_form,
7-
report_invalid_arguments_to_annotated, report_invalid_arguments_to_callable,
7+
report_invalid_arguments_to_callable,
88
};
99
use crate::types::signatures::Signature;
1010
use crate::types::string_annotation::parse_string_annotation;
@@ -913,36 +913,13 @@ impl<'db> TypeInferenceBuilder<'db, '_> {
913913
let arguments_slice = &*subscript.slice;
914914
match special_form {
915915
SpecialFormType::Annotated => {
916-
let ast::Expr::Tuple(ast::ExprTuple {
917-
elts: arguments, ..
918-
}) = arguments_slice
919-
else {
920-
report_invalid_arguments_to_annotated(&self.context, subscript);
921-
922-
// `Annotated[]` with less than two arguments is an error at runtime.
923-
// However, we still treat `Annotated[T]` as `T` here for the purpose of
924-
// giving better diagnostics later on.
925-
// Pyright also does this. Mypy doesn't; it falls back to `Any` instead.
926-
return self.infer_type_expression(arguments_slice);
927-
};
928-
929-
if arguments.len() < 2 {
930-
report_invalid_arguments_to_annotated(&self.context, subscript);
931-
}
932-
933-
let [type_expr, metadata @ ..] = &arguments[..] else {
934-
for argument in arguments {
935-
self.infer_expression(argument, TypeContext::default());
936-
}
937-
self.store_expression_type(arguments_slice, Type::unknown());
938-
return Type::unknown();
939-
};
940-
941-
for element in metadata {
942-
self.infer_expression(element, TypeContext::default());
943-
}
944-
945-
let ty = self.infer_type_expression(type_expr);
916+
let ty = self
917+
.infer_subscript_load_impl(
918+
Type::SpecialForm(SpecialFormType::Annotated),
919+
subscript,
920+
)
921+
.in_type_expression(db, self.scope(), None)
922+
.unwrap_or_else(|err| err.into_fallback_type(&self.context, subscript, true));
946923
self.store_expression_type(arguments_slice, ty);
947924
ty
948925
}

0 commit comments

Comments
 (0)