Skip to content

Don't crash when partial types are used in inherited attribute - #6766

Merged
ilevkivskyi merged 3 commits into
python:masterfrom
onlined:fix/crash-partial-type-subclass
May 13, 2019
Merged

ilevkivskyi merged 3 commits into
python:masterfrom
onlined:fix/crash-partial-type-subclass

Conversation

@onlined

@onlined onlined commented May 5, 2019

Copy link
Copy Markdown
Contributor

Fix #4552.

@ilevkivskyi ilevkivskyi left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks, the idea seems reasonable to me. I have several suggestions for additional tests.

[builtins fixtures/dict.pyi]

[case testInheritedAttributeNoStrictOptional]
# flags: --no-strict-optional

Copy link
Copy Markdown
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 B(A):
x = None
x = ''

Copy link
Copy Markdown
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)?

Comment thread test-data/unit/check-inference.test Outdated
class B(A):
x = None
x = ''
reveal_type(x) # E: Revealed type is 'builtins.str' No newline at end of file

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

No new line at the end of file.

Comment thread mypy/checker.py
self.infer_partial_type(lvalue_type.var, lvalue, rvalue_type)
# Handle 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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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.

@onlined onlined May 13, 2019 •

Copy link
Copy Markdown
Contributor Author

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

@ilevkivskyi ilevkivskyi May 13, 2019 •

Copy link
Copy Markdown
Member

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?

@ilevkivskyi
ilevkivskyi merged commit e0b1329 into python:master May 13, 2019
PattenR pushed a commit to PattenR/mypy that referenced this pull request Jun 23, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Crash related to partial type in subclass

2 participants