-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Don't crash when partial types are used in inherited attribute #6766
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2689,3 +2689,31 @@ reveal_type(x) # E: Revealed type is 'builtins.list[Any]' | |
reveal_type(y) # E: Revealed type is 'builtins.dict[Any, Any]' | ||
|
||
[builtins fixtures/dict.pyi] | ||
|
||
[case testInheritedAttributeNoStrictOptional] | ||
# flags: --no-strict-optional | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would also add a test case with |
||
class A: | ||
x: str | ||
|
||
class B(A): | ||
x = None | ||
x = '' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you please also add a test where this causes an incompatible override error (an |
||
reveal_type(x) # E: Revealed type is 'builtins.str' | ||
|
||
[case testIncompatibleInheritedAttributeNoStrictOptional] | ||
# flags: --no-strict-optional | ||
class A: | ||
x: str | ||
|
||
class B(A): | ||
x = None | ||
x = 2 # E: Incompatible types in assignment (expression has type "int", base class "A" defined the type as "str") | ||
|
||
[case testInheritedAttributeStrictOptional] | ||
# flags: --strict-optional | ||
class A: | ||
x: str | ||
|
||
class B(A): | ||
x = None # E: Incompatible types in assignment (expression has type "None", base class "A" defined the type as "str") | ||
x = '' |
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.
The exception you added above removes the check for all kinds of partial types, while this only re-adds the check for partial
None
types. I don't say it is necessarily wrong, but I would at least add test cases with partial list and dict types (e.g. with an empty list in subclass that is subsequently gets an.append()
).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.
Yes, you are right. I will change the code, actually.
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.
Are you going to add the tests with an empty list in the subclass?
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 don't think I should add them anymore, because now I make the exception for only
None
partial types.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.
But there may be a similar crash (or a regression later), I think it is better to have one in any case.
Uh oh!
There was an error while loading. Please reload this page.
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 tried this on both master and this branch, it didn't give an error:
I think it should give an error, but I could not be sure. What do you think? @ilevkivskyi
Uh oh!
There was an error while loading. Please reload this page.
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.
Yes, this should be an error. It looks like another consequence of #4547. Let's then merge this PR as is, and consider adding this test when #4547 is fixed. Could you please add a comment in that issue?