[ty] Reject using properties with Never setters or deleters#24510
Merged
charliermarsh merged 3 commits intomainfrom Apr 16, 2026
Merged
[ty] Reject using properties with Never setters or deleters#24510charliermarsh merged 3 commits intomainfrom
Never setters or deleters#24510charliermarsh merged 3 commits intomainfrom
Conversation
Typing conformance resultsNo changes detected ✅Current numbersThe percentage of diagnostics emitted that were expected errors held steady at 87.94%. The percentage of expected errors that received a diagnostic held steady at 83.36%. The number of fully passing files held steady at 79/133. |
Memory usage reportMemory usage unchanged ✅ |
|
| Lint rule | Added | Removed | Changed |
|---|---|---|---|
invalid-assignment |
2 | 0 | 0 |
| Total | 2 | 0 | 0 |
Flaky changes detected. This PR summary excludes flaky changes; see the HTML report for details.
Raw diff:
xarray (https://github.com/pydata/xarray)
+ xarray/tests/test_dataarray.py:284:13 error[invalid-assignment] Cannot assign to attribute `dims` on type `DataArray` whose `__set__` method returns `Never`/`NoReturn`
+ xarray/tests/test_variable.py:2601:13 error[invalid-assignment] Cannot assign to attribute `name` on type `IndexVariable` whose `__set__` method returns `Never`/`NoReturn`c714ac2 to
c9146b8
Compare
87d890d to
07a4f15
Compare
067475b to
0955ad4
Compare
07a4f15 to
60a22dd
Compare
0955ad4 to
df18f81
Compare
60a22dd to
9c17e51
Compare
9c17e51 to
29600f3
Compare
Contributor
|
Claude suggests that the new helper functions are kind of redundant, since we already have diff --git i/crates/ty_python_semantic/src/types/call.rs w/crates/ty_python_semantic/src/types/call.rs
index 6a1f82da27..133481e80f 100644
--- i/crates/ty_python_semantic/src/types/call.rs
+++ w/crates/ty_python_semantic/src/types/call.rs
@@ -104,10 +104,6 @@ impl<'db> Type<'db> {
pub(crate) struct CallError<'db>(pub(crate) CallErrorKind, pub(crate) Box<Bindings<'db>>);
impl<'db> CallError<'db> {
- pub(crate) fn return_type(&self, db: &'db dyn Db) -> Type<'db> {
- self.1.return_type(db)
- }
-
/// Returns `Some(property)` if the call error was caused by an attempt to set a property
/// that has no setter, and `None` otherwise.
pub(crate) fn as_attempt_to_set_property_with_no_setter(
diff --git i/crates/ty_python_semantic/src/types/infer/builder.rs w/crates/ty_python_semantic/src/types/infer/builder.rs
index 7abd69073c..609c073036 100644
--- i/crates/ty_python_semantic/src/types/infer/builder.rs
+++ w/crates/ty_python_semantic/src/types/infer/builder.rs
@@ -2029,39 +2029,6 @@ impl<'db, 'ast> TypeInferenceBuilder<'db, 'ast> {
}
}
- /// Returns `true` if `property_ty` is a property whose setter returns `Never`/`NoReturn`
- /// when called for an assignment to `object_ty` with `value_ty`.
- fn property_setter_returns_never(
- &self,
- property_ty: Type<'db>,
- object_ty: Type<'db>,
- value_ty: Type<'db>,
- ) -> bool {
- let db = self.db();
- property_ty.as_property_instance().is_some_and(|property| {
- property.setter(db).is_some_and(|setter| {
- match setter.try_call(db, &CallArguments::positional([object_ty, value_ty])) {
- Ok(result) => result.return_type(db).is_never(),
- Err(err) => err.return_type(db).is_never(),
- }
- })
- })
- }
-
- /// Returns `true` if `property_ty` is a property whose deleter returns `Never`/`NoReturn`
- /// when called for deletion on `object_ty`.
- fn property_deleter_returns_never(&self, property_ty: Type<'db>, object_ty: Type<'db>) -> bool {
- let db = self.db();
- property_ty.as_property_instance().is_some_and(|property| {
- property.deleter(db).is_some_and(|deleter| {
- match deleter.try_call(db, &CallArguments::positional([object_ty])) {
- Ok(result) => result.return_type(db).is_never(),
- Err(err) => err.return_type(db).is_never(),
- }
- })
- })
- }
-
/// Make sure that the attribute assignment `obj.attribute = value` is valid.
///
/// `target` is the node for the left-hand side, `object_ty` is the type of `obj`, `attribute` is
@@ -2375,7 +2342,7 @@ impl<'db, 'ast> TypeInferenceBuilder<'db, 'ast> {
&CallArguments::positional([meta_attr_ty, object_ty, value_ty]),
);
- if self.property_setter_returns_never(meta_attr_ty, object_ty, value_ty)
+ if matches!(&dunder_set_result, Ok(b) if b.return_type(db).is_never())
{
if emit_diagnostics
&& let Some(builder) =
@@ -2586,7 +2553,7 @@ impl<'db, 'ast> TypeInferenceBuilder<'db, 'ast> {
&CallArguments::positional([meta_attr_ty, object_ty, value_ty]),
);
- if self.property_setter_returns_never(meta_attr_ty, object_ty, value_ty)
+ if matches!(&dunder_set_result, Ok(b) if b.return_type(db).is_never())
{
if emit_diagnostics
&& let Some(builder) =
@@ -2933,7 +2900,7 @@ impl<'db, 'ast> TypeInferenceBuilder<'db, 'ast> {
TypeContext::default(),
);
- if self.property_deleter_returns_never(attr_ty, object_ty) {
+ if matches!(&delete_dunder_call_result, Ok(b) if b.return_type(db).is_never()) {
if emit_diagnostics
&& let Some(builder) =
self.context.report_lint(&INVALID_ASSIGNMENT, target)What do you think? |
df18f81 to
8a176c0
Compare
29600f3 to
7345140
Compare
Member
Author
|
Looks right to me! |
oconnor663
approved these changes
Apr 15, 2026
Member
Author
|
Oh, hmm... That refactor may have regressed the conformance suite. |
7345140 to
2b2fe22
Compare
2b2fe22 to
9c76ba3
Compare
Contributor
|
What was the story with the regression there? |
Member
Author
|
We were raising a diagnostic here, in contrast to the conformance tests: class ClassA:
x: NoReturn
y: list[NoReturn]
def __init__(self, x: NoReturn, y: list[NoReturn]) -> None:
self.x = x # ← false positive with the refactored check
self.y = yI think this is somewhat debatable? But for now we're limiting to |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
We currently error if
__setattr__returnsNeverand the user attempts to assign to an attribute, and same for deletion. This PR extends that behavior to properties with setters and deleters.For example, this is now forbidden: