-
Notifications
You must be signed in to change notification settings - Fork 1.4k
[red-knot] basic narrowing on attribute and subscript expressions #17643
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
base: main
Are you sure you want to change the base?
Conversation
|
a9ee6ed
to
cfa79a2
Compare
Thank you for working on this!
I think it's not just a matter of additional narrowing. It looks to me like, on this branch, we can infer incorrect types by not considering attribute assignments: from typing import reveal_type
class C:
x: int | None = None
c = C()
if c.x is None:
c.x = 1
reveal_type(c.x) # this branch: None I'm afraid this is something that we need to fix before we can consider merging this. It also leads to some new false positives in the ecosystem checks. Please let me know if you need any help with this. |
cfa79a2
to
9db9728
Compare
(Converting this into draft as I think this requires #18041 ?) |
Summary
This PR closes astral-sh/ty#164.
This PR introduces a basic type narrowing mechanism for attribute/subscript expressions.
Member accesses, int literal subscripts, string literal subscripts are supported (same as mypy and pyright).
Advanced narrowing mechanisms, such as the following, are not implemented in this PR (and will be future works):
Test Plan
New test cases are added to
mdtest/narrow/composite_target.md
.