KEMBAR78
dataclasses: Fix deeply nested InitVar definitions with init=False by beckjake · Pull Request #8208 · python/mypy · GitHub
Skip to content

Conversation

beckjake
Copy link
Contributor

Fixes #8207

In #8159, I fixed InitVar handling so that it looked up the superclass __init__ and used that to determine the definition of InitVars.
That fix doesn't work in the presence of init=False subclasses of the initial init=False class. In that case, the lookup fails because the first attribute reached in MRO order on the subclass is the intermediate class, which also doesn't have the variable in its __init__. Then mypy didn't know about the InitVar but still tried to process it as if it did, resulting in an assertion error.

This PR fixes that issue by not adding the field to the set of known attrs until an actual definition is found.

In python#8159, I fixed InitVar handling so that it looked up the superclass __init__ and used that to determine the definition of InitVars.
That fix doesn't work in the presence of `init=False` subclasses of the initial `init=False` class
In that case, the lookup fails because the first attribute reached in MRO order on the subclass is the intermediate class, which also doesn't have the variable in its `__init__`.
Then mypy didn't know about the InitVar but still tried to process it as if it did, resulting in an assertion error.

This PR fixes that issue by not adding the field to the set of known attrs until an actual definition is found.
Copy link
Collaborator

@JukkaL JukkaL left a comment

Choose a reason for hiding this comment

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

Thanks! Looks good.

@JukkaL JukkaL merged commit 01ca4e0 into python:master Jan 9, 2020
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.

dataclass inheritance: deeply nested InitVar overrides crash mypy

2 participants