-
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] Extend instance/class attribute tests #15959
Conversation
# TODO: We should probably infer `int | str` here. | ||
reveal_type(c_instance.z) # revealed: int |
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.
Since we'll have emitted a diagnostic above complaining about the redeclaration, I think the current behaviour here is also reasonable TBH. I wouldn't worry too much about inferring a union here if it complicates the implementation.
# TODO: should be `Literal["overwritten on class"]` once/if we support | ||
# local narrowing. |
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.
interesting... do mypy/pyright support narrowing like this, where you narrow it on the class but then read it from an instance?
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.
Mypy doesn't seem to support this detection of implicit pure_class_variable
s at all. Pyright supports narrowing in the case above (C.pure_class_variable
), but not here.
Maybe I should remove the TODO here?
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.
Yeah, I don't think this is something we "have" to support -- I don't think it's something users will expect of us. (I think they will expect attribute narrowing when the attribute is accessed on the same variable, such as C.pure_class_variable
above.) Obviously if it's easy and naturally falls out of the implementation, then that's fantastic -- I think it isn't any more unsound than attribute narrowing in general. But I don't think it's something we need a TODO for.
reveal_type(D.x) # revealed: Unknown | ||
|
||
# TODO: this should raise `unresolved-attribute` as well, and the type should be `Unknown` | ||
reveal_type(D().x) # revealed: Unknown | Literal[1] |
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.
Pyright revals int
here, which is wrong.
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.
oof. might be worth a bug report to pyright!
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.
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 won't be surprised if aliasing staticmethod
is just considered out-of-scope for pyright to handle. I will be very happy if we can handle it, however!
Summary
In preparation for creating some (sub) issues for #14164, I'm trying to document the current behavior (and a bug) a bit better.