-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Love it!
// | ||
// for self.name in <iterable>: | ||
|
||
// TODO: Potential diagnostics resulting from the iterable are currently not reported. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
for
loopswith
statementscloses #15962
Test Plan
New Markdown tests