Skip to content

Commit c9ca199

Browse files
committed
a few adjustments
1 parent c38ec21 commit c9ca199

File tree

2 files changed

+25
-25
lines changed

2 files changed

+25
-25
lines changed

crates/ty_python_semantic/resources/mdtest/dataclasses.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ class MyFrozenGeneric[T]:
413413
x: T
414414

415415
frozen_instance = MyFrozenGeneric[int](1)
416-
frozen_instance.x = 2 # TODO
416+
frozen_instance.x = 2 # error: [invalid-assignment]
417417
```
418418

419419
When attempting to mutate an unresolved attribute on a frozen dataclass, only `unresolved-attribute`

crates/ty_python_semantic/src/types/infer.rs

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3088,18 +3088,19 @@ impl<'db> TypeInferenceBuilder<'db> {
30883088
| Type::TypeVar(..)
30893089
| Type::AlwaysTruthy
30903090
| Type::AlwaysFalsy => {
3091-
let dataclass_params = match object_ty {
3092-
Type::NominalInstance(instance) => match instance.class() {
3093-
ClassType::NonGeneric(cls) => cls.dataclass_params(self.db()),
3094-
ClassType::Generic(cls) => {
3095-
cls.origin(self.db()).dataclass_params(self.db())
3096-
}
3097-
},
3098-
_ => None,
3099-
};
3091+
let is_read_only = || {
3092+
let dataclass_params = match object_ty {
3093+
Type::NominalInstance(instance) => match instance.class {
3094+
ClassType::NonGeneric(cls) => cls.dataclass_params(self.db()),
3095+
ClassType::Generic(cls) => {
3096+
cls.origin(self.db()).dataclass_params(self.db())
3097+
}
3098+
},
3099+
_ => None,
3100+
};
31003101

3101-
let read_only =
3102-
dataclass_params.is_some_and(|params| params.contains(DataclassParams::FROZEN));
3102+
dataclass_params.is_some_and(|params| params.contains(DataclassParams::FROZEN))
3103+
};
31033104

31043105
match object_ty.class_member(db, attribute.into()) {
31053106
meta_attr @ SymbolAndQualifiers { .. } if meta_attr.is_class_var() => {
@@ -3120,7 +3121,7 @@ impl<'db> TypeInferenceBuilder<'db> {
31203121
symbol: Symbol::Type(meta_attr_ty, meta_attr_boundness),
31213122
qualifiers: _,
31223123
} => {
3123-
if read_only {
3124+
if is_read_only() {
31243125
if emit_diagnostics {
31253126
if let Some(builder) =
31263127
self.context.report_lint(&INVALID_ASSIGNMENT, target)
@@ -3215,18 +3216,17 @@ impl<'db> TypeInferenceBuilder<'db> {
32153216
);
32163217
}
32173218

3218-
if read_only {
3219-
// TODO(thejchap): illustrating missing diagnostic
3220-
// if emit_diagnostics {
3221-
// if let Some(builder) =
3222-
// self.context.report_lint(&INVALID_ASSIGNMENT, target)
3223-
// {
3224-
// builder.into_diagnostic(format_args!(
3225-
// "Property `{attribute}` defined in `{ty}` is read-only",
3226-
// ty = object_ty.display(self.db()),
3227-
// ));
3228-
// }
3229-
// }
3219+
if is_read_only() {
3220+
if emit_diagnostics {
3221+
if let Some(builder) =
3222+
self.context.report_lint(&INVALID_ASSIGNMENT, target)
3223+
{
3224+
builder.into_diagnostic(format_args!(
3225+
"Property `{attribute}` defined in `{ty}` is read-only",
3226+
ty = object_ty.display(self.db()),
3227+
));
3228+
}
3229+
}
32303230
false
32313231
} else {
32323232
ensure_assignable_to(instance_attr_ty)

0 commit comments

Comments
 (0)