Skip to content

Commit 3ad7fac

Browse files
committed
review comments
1 parent de0c4c1 commit 3ad7fac

2 files changed

Lines changed: 58 additions & 59 deletions

File tree

crates/ty_python_semantic/resources/mdtest/dataclasses/post_init.md

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,28 +17,23 @@ class Empty2:
1717
def __post_init__(self) -> None: ... # fine
1818

1919
@dataclass
20-
class Empty2:
20+
class Empty3:
2121
# The returned value is discarded,
2222
# so arbitrary return annotations are allowed
2323
def __post_init__(self) -> int:
2424
return 42
2525

26-
@dataclass
27-
class Empty3:
28-
def __post_init__(self, *args): ... # fine
29-
3026
@dataclass
3127
class Empty4:
32-
def __post_init__(self, **kwargs): ... # fine
28+
def __post_init__(self, *args): ... # fine
3329

3430
@dataclass
3531
class Empty5:
36-
def __post_init__(self, *args, **kargs): ... # fine
32+
def __post_init__(self, **kwargs): ... # fine
3733

3834
@dataclass
3935
class Empty6:
40-
# error: [invalid-dataclass]
41-
def __post_init__(self, required_argument: int): ...
36+
def __post_init__(self, *args, **kargs): ... # fine
4237

4338
@dataclass
4439
class Empty7:
@@ -88,9 +83,9 @@ class HasInitVarDifferentParameterName:
8883
x: InitVar[int]
8984

9085
# because arguments are always passed in positionally
91-
# to `__post_init__` methods, we allow the parameters to
92-
# have arbitrary names as long as they are annotated with
93-
# the right type. So this is fine:
86+
# to `__post_init__` methods, we allow a parameter to
87+
# have an arbitrary name as long as it is inferred has
88+
# having a compatible type. So this is fine:
9489
def __post_init__(self, xx) -> None: ...
9590

9691
@dataclass

crates/ty_python_semantic/src/types/overrides.rs

Lines changed: 51 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)