@@ -158,34 +158,40 @@ impl<'db> TypeInferenceBuilder<'db, '_> {
158158 ) ;
159159 }
160160
161- let name = if let Some ( literal) = name_type. as_string_literal ( ) {
162- let name = literal. value ( db) ;
163-
164- if let Some ( assigned_name) = definition. and_then ( |definition| definition. name ( db) )
165- && name != assigned_name
166- && let Some ( builder) = self . context . report_lint ( & INVALID_ARGUMENT_TYPE , name_arg)
167- {
168- builder. into_diagnostic ( format_args ! (
169- "The name of a `TypedDict` (`{name}`) must match \
170- the name of the variable it is assigned to (`{assigned_name}`)"
171- ) ) ;
172- }
173-
174- Name :: new ( name)
175- } else {
176- if !name_type. is_assignable_to ( db, KnownClass :: Str . to_instance ( db) )
177- && let Some ( builder) = self . context . report_lint ( & INVALID_ARGUMENT_TYPE , name_arg)
178- {
179- let mut diagnostic = builder. into_diagnostic ( format_args ! (
180- "Invalid argument to parameter `typename` of `TypedDict()`"
161+ let name = name_type
162+ . as_string_literal ( )
163+ . map ( |literal| Name :: new ( literal. value ( db) ) ) ;
164+
165+ if let Some ( definition) = definition
166+ && let Some ( assigned_name) = definition. name ( db)
167+ && Some ( assigned_name. as_str ( ) ) != name. as_deref ( )
168+ && let Some ( builder) = self . context . report_lint ( & INVALID_ARGUMENT_TYPE , name_arg)
169+ {
170+ let mut diagnostic =
171+ builder. into_diagnostic ( "TypedDict name must match the variable it is assigned to" ) ;
172+ if let Some ( name) = name. as_deref ( ) {
173+ diagnostic. set_primary_message ( format_args ! (
174+ "Expected \" {assigned_name}\" , got \" {name}\" "
181175 ) ) ;
176+ } else {
182177 diagnostic. set_primary_message ( format_args ! (
183- "Expected `str`, found `{}`" ,
178+ "Expected \" {assigned_name} \" , got variable of type `{}`" ,
184179 name_type. display( db)
185180 ) ) ;
186181 }
187- Name :: new_static ( "<unknown>" )
188- } ;
182+ } else if !name_type. is_assignable_to ( db, KnownClass :: Str . to_instance ( db) )
183+ && let Some ( builder) = self . context . report_lint ( & INVALID_ARGUMENT_TYPE , name_arg)
184+ {
185+ let mut diagnostic = builder. into_diagnostic ( format_args ! (
186+ "Invalid argument to parameter `typename` of `TypedDict()`"
187+ ) ) ;
188+ diagnostic. set_primary_message ( format_args ! (
189+ "Expected `str`, found `{}`" ,
190+ name_type. display( db)
191+ ) ) ;
192+ }
193+
194+ let name = name. unwrap_or_else ( || Name :: new_static ( "<unknown>" ) ) ;
189195
190196 if let Some ( definition) = definition {
191197 self . deferred . insert ( definition) ;
0 commit comments