@@ -542,59 +542,63 @@ fn check_post_init_signature<'db>(
542542) {
543543 let db = context. db ( ) ;
544544
545- if configuration. check_invalid_dataclasses ( )
546- && member. name == "__post_init__"
547- && let Some ( ( static_class, spec) ) = class. static_class_literal ( db)
548- {
549- let init_var_fields = static_class
550- . fields ( db, spec, policy)
551- . iter ( )
552- . filter ( |( _, field) | {
553- matches ! (
554- field. kind,
555- FieldKind :: Dataclass {
556- init_only: true ,
557- ..
558- }
559- )
560- } ) ;
561-
562- let first_parameter = Parameter :: positional_only ( Some ( Name :: new_static ( "self" ) ) )
563- . with_annotated_type ( Type :: instance ( db, class) ) ;
545+ if !configuration. check_invalid_dataclasses ( ) {
546+ return ;
547+ }
548+ if member. name != "__post_init__" {
549+ return ;
550+ }
551+ let Some ( ( static_class, spec) ) = class. static_class_literal ( db) else {
552+ return ;
553+ } ;
564554
565- let following_parameters = init_var_fields. map ( |( name, field) | {
566- Parameter :: positional_only ( Some ( name. clone ( ) ) ) . with_annotated_type ( field. declared_ty )
555+ let init_var_fields = static_class
556+ . fields ( db, spec, policy)
557+ . iter ( )
558+ . filter ( |( _, field) | {
559+ matches ! (
560+ field. kind,
561+ FieldKind :: Dataclass {
562+ init_only: true ,
563+ ..
564+ }
565+ )
567566 } ) ;
568567
569- let parameters = Parameters :: new (
570- db,
571- std:: iter:: once ( first_parameter) . chain ( following_parameters) ,
572- ) ;
568+ let first_parameter = Parameter :: positional_only ( Some ( Name :: new_static ( "self" ) ) )
569+ . with_annotated_type ( Type :: instance ( db, class) ) ;
573570
574- let expected_signature =
575- CallableType :: single ( db, Signature :: new ( parameters, Type :: object ( ) ) ) ;
571+ let following_parameters = init_var_fields. map ( |( name, field) | {
572+ Parameter :: positional_only ( Some ( name. clone ( ) ) ) . with_annotated_type ( field. declared_ty )
573+ } ) ;
576574
577- if member
578- . ty
579- . is_assignable_to ( db, Type :: Callable ( expected_signature) )
580- {
581- return ;
582- }
575+ let parameters = Parameters :: new (
576+ db,
577+ std:: iter:: chain ( [ first_parameter] , following_parameters) ,
578+ ) ;
583579
584- let Some ( builder) = context. report_lint (
585- & INVALID_DATACLASS ,
586- definition. focus_range ( db, context. module ( ) ) ,
587- ) else {
588- return ;
589- } ;
580+ let expected_signature = CallableType :: single ( db, Signature :: new ( parameters, Type :: object ( ) ) ) ;
590581
591- let mut diagnostic = builder. into_diagnostic ( format_args ! (
592- "Invalid `__post_init__` signature for dataclass `{}`" ,
593- class. name( db)
594- ) ) ;
595- diagnostic. info (
596- "`__post_init__` methods must accept all `InitVar` fields \
597- as positional-only parameters",
598- ) ;
582+ if member
583+ . ty
584+ . is_assignable_to ( db, Type :: Callable ( expected_signature) )
585+ {
586+ return ;
599587 }
588+
589+ let Some ( builder) = context. report_lint (
590+ & INVALID_DATACLASS ,
591+ definition. focus_range ( db, context. module ( ) ) ,
592+ ) else {
593+ return ;
594+ } ;
595+
596+ let mut diagnostic = builder. into_diagnostic ( format_args ! (
597+ "Invalid `__post_init__` signature for dataclass `{}`" ,
598+ class. name( db)
599+ ) ) ;
600+ diagnostic. info (
601+ "`__post_init__` methods must accept all `InitVar` fields \
602+ as positional-only parameters",
603+ ) ;
600604}
0 commit comments