[red-knot] Unpacking and for loop assignments to attributes#16004
[red-knot] Unpacking and for loop assignments to attributes#16004
Conversation
crates/red_knot_python_semantic/src/semantic_index/attribute_assignment.rs
Show resolved
Hide resolved
a539809 to
a00483a
Compare
| // | ||
| // for self.name in <iterable>: | ||
|
|
||
| // TODO: Potential diagnostics resulting from the iterable are currently not reported. |
There was a problem hiding this comment.
I expect they would still be reported by normal type inference, no? So I'm not sure any special handling is needed here, thus maybe we don't need a TODO either?
Maybe ideally we'd add some tests for cases where diagnostics occur either in iteration, or more generally in type inference of the RHS of a self.x = ... assignment, and verify that we do surface those diagnostics.
There was a problem hiding this comment.
I expect they would still be reported by normal type inference, no?
I also hoped that would be the case, but no. We emit the non-iterable diagnostic from infer_for_statement_definition, and attribute assignments are not definitions. I added a test to demonstrate this.
We have a similar problem where we do not yet identify cases where (normal) assignments to attributes conflict the declared type of that attribute. We have existing tests for this as well.
(Edit: no, I already implemented this 😄)
I'll also open a new ticket to address both of these issues.
or more generally in type inference of the RHS of a
self.x = ...assignment
The general case works fine. We only fail to emit diagnostics if they originate from infer_…_definition calls, which do not apply to attribute assignments. I added the following test case:
class C:
def __init__(self) -> None:
# error: [too-many-positional-arguments]
self.x: int = len(1, 2, 3)
Summary
forloopswithstatementscloses #15962
Test Plan
New Markdown tests