-
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] Implicit instance attributes #15811
base: main
Are you sure you want to change the base?
Conversation
|
||
# TODO: Same here. This should be `Unknown | Literal[1, "a"]` | ||
reveal_type(c_instance.inferred_from_other_attribute) # revealed: @Todo(implicit instance attribute) | ||
reveal_type(c_instance.inferred_from_other_attribute) # revealed: Unknown |
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 think this doesn't work because we don't know what the type of self
is.
|
||
# TODO: Should be `Unknown | Literal[1, "a"]` | ||
reveal_type(c_instance.inferred_from_other_attribute) # revealed: @Todo(implicit instance attribute) | ||
reveal_type(c_instance.inferred_from_other_attribute) # revealed: Unknown |
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.
See above: this does not work because we don't know what the type of self
is.
@@ -266,7 +259,7 @@ reveal_type(C.pure_class_variable) # revealed: Unknown | |||
|
|||
c_instance = C() | |||
# TODO: should be `Literal["overwritten on class"]` | |||
reveal_type(c_instance.pure_class_variable) # revealed: @Todo(implicit instance attribute) | |||
reveal_type(c_instance.pure_class_variable) # revealed: Unknown | Literal["value set in class method"] |
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.
Probably not something that we're going to address here. This would require narrowing on attribute expressions. Unknown | Literal["value set in class method"]
is not wrong.
b216fc3
to
9f5ffe1
Compare
Summary
Add support for implicitly-defined instance attributes, i.e. support type inference for cases like this:
To do:
self.x: str = "a"
)self.x: str
)self.x = "a"
)__init__
self.x, self.y = …
Things intentionally left out of this PR:
self
self.x = param
)self.x, self.y = …
Test Plan
Updated and new Markdown tests