[red-knot] detect unreachable attribute assignments#16852
[red-knot] detect unreachable attribute assignments#16852sharkdp merged 20 commits intoastral-sh:mainfrom
Conversation
|
bab7385 to
f04ff66
Compare
96e6334 to
a5c8278
Compare
|
While trying to solve the issue, I noticed that the following code does not pass: class C:
def __init__(self):
def closure():
self.a: str = 1
closure()
[... for self.b in range(1)]
class D:
self.c = 1
# should be OK
C().a
C().b
C().cIt appears that the |
Replace * `AttributeAssignment::Annotated` -> `DefinitionKind::AnnotatedAssignment` * `AttributeAssignment::Unannotated` -> `DefinitionKind::Assignment` * `AttributeAssignment::Iterable` -> `DefinitionKind::For` * `AttributeAssignment::ContextManager` -> `DefinitionKind::WithItem`
CodSpeed Performance ReportMerging #16852 will not alter performanceComparing Summary
|
|
The commit I just made completely removes I noticed that we can also track attribute assignments in comprehension/named/augmented assignments as well, so I will be working on that. |
carljm
left a comment
There was a problem hiding this comment.
Haven't finished my review but I have to head out, so submitting the comments I have so far. Thanks for your work on this!
Given the false positives observed, I'm not sure we should keep the possibly-unbound-attribute checks, but I'm still thinking about whether there is a satisfactory way to address the false positive cases. It may have some overlap with https://github.com/astral-sh/ruff/issues/15777
fe12755 to
34965a9
Compare
|
Support for attribute assignments in comprehension/aug-assign/ I believe that the implementation of the feature originally planned in this PR is almost complete and ready for final review. |
carljm
left a comment
There was a problem hiding this comment.
This looks pretty good to me! It turns ClassLiteralType::implicit_instance_attribute into even more of a monster method, but I think I'm ok with that for now: it has a clear responsibility, and the logic in it is well encapsulated; if we need to break it up for better reuse in future, we can do that.
@sharkdp if you have a chance, I'd like to make sure this looks good to you as well.
sharkdp
left a comment
There was a problem hiding this comment.
This looks great — thank you very much. I have just one minor comment, which I can also resolve myself before merging this.
* main: (31 commits) [red-knot] Add some knowledge of `__all__` to `*`-import machinery (#17373) Update taiki-e/install-action digest to be7c31b (#17379) Update Rust crate mimalloc to v0.1.46 (#17382) Update PyO3/maturin-action action to v1.49.1 (#17384) Update Rust crate anyhow to v1.0.98 (#17380) dependencies: switch from `chrono` to `jiff` Update Rust crate bstr to v1.12.0 (#17385) [red-knot] Further optimize `*`-import visibility constraints (#17375) [red-knot] Minor 'member_lookup_with_policy' fix (#17407) [red-knot] Initial support for `dataclass`es (#17353) Sync vendored typeshed stubs (#17402) [red-knot] improve function/bound method type display (#17294) [red-knot] Move relation methods from `CallableType` to `Signature` (#17365) [syntax-errors] `await` outside async functions (#17363) [red-knot] optimize is_subtype_of for literals (#17394) [red-knot] add a large-union-of-string-literals benchmark (#17393) Update pre-commit dependencies (#17383) [red-knot] mypy_primer: Fail job on panic or internal errors (#17389) [red-knot] Document limitations of diagnostics-silencing in unreachable code (#17387) [red-knot] detect unreachable attribute assignments (#16852) ...
## Summary This PR is a follow-up to #16852. Instance variables bound in comprehensions are recorded, allowing type inference to work correctly. This required adding support for unpacking in comprehension which resolves #15369. ## Test Plan One TODO in `mdtest/attributes.md` is now resolved, and some new test cases are added. --------- Co-authored-by: Dhruv Manilawala <dhruvmanila@gmail.com>
Summary
This PR closes #15967.
Attribute assignments that are statically known to be unreachable are excluded from consideration for implicit instance attribute type inference. If none of the assignments are found to be reachable, an
unresolved-attributeerror is reported.Test Plan
A test case marked as TODO now work as intended, and new test cases have been added.