@@ -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
933931impl < ' db , ' ast > TypeInferenceBuilder < ' db , ' ast > {
@@ -6319,15 +6317,13 @@ impl<'db, 'ast> TypeInferenceBuilder<'db, 'ast> {
63196317 . match_parameters ( & call_arguments) ;
63206318 self . infer_argument_types ( arguments, & mut call_arguments, & bindings. argument_forms ) ;
63216319
6322- // Validate TypedDict constructor calls after argument type inference
6320+ // Validate ` TypedDict` constructor calls after argument type inference
63236321 if let Some ( class_literal) = callable_type. into_class_literal ( ) {
63246322 if class_literal. is_typed_dict ( self . db ( ) ) {
6325- let typed_dict_type = crate :: types:: TypedDictType :: from (
6326- self . db ( ) ,
6327- crate :: types:: ClassType :: NonGeneric ( class_literal) ,
6328- ) ;
6323+ let typed_dict_type =
6324+ TypedDictType :: from ( self . db ( ) , ClassType :: NonGeneric ( class_literal) ) ;
63296325 if let Some ( typed_dict) = typed_dict_type. into_typed_dict ( ) {
6330- // Validate keyword arguments for TypedDict constructor
6326+ // Validate keyword arguments for ` TypedDict` constructor
63316327 for keyword in & arguments. keywords {
63326328 if let Some ( arg_name) = & keyword. arg {
63336329 // Get the already-inferred argument type
0 commit comments