Skip to content

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

Merged
merged 3 commits into from
May 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -1786,7 +1786,6 @@ def check_assignment(self, lvalue: Lvalue, rvalue: Expression, infer_lvalue_type
infer_lvalue_type)
else:
lvalue_type, index_lvalue, inferred = self.check_lvalue(lvalue)

# If we're assigning to __getattr__ or similar methods, check that the signature is
# valid.
if isinstance(lvalue, NameExpr) and lvalue.node:
Expand All @@ -1803,7 +1802,9 @@ def check_assignment(self, lvalue: Lvalue, rvalue: Expression, infer_lvalue_type
else:
self.check_getattr_method(signature, lvalue, name)

if isinstance(lvalue, RefExpr):
# Defer PartialType's super type checking.
if (isinstance(lvalue, RefExpr) and
not (isinstance(lvalue_type, PartialType) and lvalue_type.type is None)):
if self.check_compatibility_all_supers(lvalue, lvalue_type, rvalue):
# We hit an error on this line; don't check for any others
return
Expand Down Expand Up @@ -1833,6 +1834,11 @@ def check_assignment(self, lvalue: Lvalue, rvalue: Expression, infer_lvalue_type
# Try to infer a partial type. No need to check the return value, as
# an error will be reported elsewhere.
self.infer_partial_type(lvalue_type.var, lvalue, rvalue_type)
# Handle None PartialType's super type checking here, after it's resolved.
if (isinstance(lvalue, RefExpr) and
self.check_compatibility_all_supers(lvalue, lvalue_type, rvalue)):
Copy link
Member

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()).

Copy link
Contributor Author

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.

Copy link
Member

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?

Copy link
Contributor Author

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.

Copy link
Member

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.

Copy link
Contributor Author

@onlined onlined May 13, 2019

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:

class A:
    x = ['a', 'b']

class B(A):
    x = []
    x.append(2)

I think it should give an error, but I could not be sure. What do you think? @ilevkivskyi

Copy link
Member

@ilevkivskyi ilevkivskyi May 13, 2019

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?

# We hit an error on this line; don't check for any others
return
elif (is_literal_none(rvalue) and
isinstance(lvalue, NameExpr) and
isinstance(lvalue.node, Var) and
Expand Down
28 changes: 28 additions & 0 deletions test-data/unit/check-inference.test
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would also add a test case with --strict-optional as well (to avoid regressions).

class A:
x: str

class B(A):
x = None
x = ''
Copy link
Member

Choose a reason for hiding this comment

The 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 int)?

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 = ''