Skip to content

Commit bea85c3

Browse files
committed
refactor: better code
1 parent 86d514a commit bea85c3

File tree

1 file changed

+37
-39
lines changed
  • crates/ty_python_semantic/src/types

1 file changed

+37
-39
lines changed

crates/ty_python_semantic/src/types/infer.rs

Lines changed: 37 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -879,44 +879,8 @@ fn validate_typed_dict_key_assignment<'db, 'ast>(
879879
let db = context.db();
880880
let items = typed_dict.items(db);
881881

882-
if let Some((_, item)) = items.iter().find(|(name, _)| *name == key) {
883-
// Key exists, check if value type is assignable to declared type
884-
if value_ty.is_assignable_to(db, item.declared_ty) {
885-
return true;
886-
}
887-
888-
// Invalid assignment - emit diagnostic
889-
if let Some(builder) =
890-
context.report_lint(assignment_kind.diagnostic_type(), value_node.into())
891-
{
892-
let typed_dict_ty = Type::TypedDict(typed_dict);
893-
let typed_dict_d = typed_dict_ty.display(db);
894-
let value_d = value_ty.display(db);
895-
let item_type_d = item.declared_ty.display(db);
896-
897-
let mut diagnostic = builder.into_diagnostic(format_args!(
898-
"Invalid {} to key \"{key}\" with declared type `{item_type_d}` on TypedDict `{typed_dict_d}`",
899-
assignment_kind.diagnostic_name(),
900-
));
901-
902-
diagnostic.set_primary_message(format_args!("value of type `{value_d}`"));
903-
904-
diagnostic.annotate(
905-
context
906-
.secondary(typed_dict_node.into())
907-
.message(format_args!("TypedDict `{typed_dict_d}`")),
908-
);
909-
910-
diagnostic.annotate(
911-
context
912-
.secondary(key_node.into())
913-
.message(format_args!("key has declared type `{item_type_d}`",)),
914-
);
915-
}
916-
917-
false
918-
} else {
919-
// Key doesn't exist - use existing invalid key reporting
882+
// Check if key exists in `TypedDict`
883+
let Some((_, item)) = items.iter().find(|(name, _)| *name == key) else {
920884
report_invalid_key_on_typed_dict(
921885
context,
922886
typed_dict_node.into(),
@@ -925,9 +889,43 @@ fn validate_typed_dict_key_assignment<'db, 'ast>(
925889
Type::string_literal(db, key),
926890
&items,
927891
);
892+
return false;
893+
};
928894

929-
false
895+
// Key exists, check if value type is assignable to declared type
896+
if value_ty.is_assignable_to(db, item.declared_ty) {
897+
return true;
930898
}
899+
900+
// Invalid assignment - emit diagnostic
901+
if let Some(builder) = context.report_lint(assignment_kind.diagnostic_type(), value_node.into())
902+
{
903+
let typed_dict_ty = Type::TypedDict(typed_dict);
904+
let typed_dict_d = typed_dict_ty.display(db);
905+
let value_d = value_ty.display(db);
906+
let item_type_d = item.declared_ty.display(db);
907+
908+
let mut diagnostic = builder.into_diagnostic(format_args!(
909+
"Invalid {} to key \"{key}\" with declared type `{item_type_d}` on TypedDict `{typed_dict_d}`",
910+
assignment_kind.diagnostic_name(),
911+
));
912+
913+
diagnostic.set_primary_message(format_args!("value of type `{value_d}`"));
914+
915+
diagnostic.annotate(
916+
context
917+
.secondary(typed_dict_node.into())
918+
.message(format_args!("TypedDict `{typed_dict_d}`")),
919+
);
920+
921+
diagnostic.annotate(
922+
context
923+
.secondary(key_node.into())
924+
.message(format_args!("key has declared type `{item_type_d}`")),
925+
);
926+
}
927+
928+
false
931929
}
932930

933931
impl<'db, 'ast> TypeInferenceBuilder<'db, 'ast> {

0 commit comments

Comments
 (0)