Skip to content
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

Fix types of inherited attributes in generic dataclasses #12656

Merged
merged 4 commits into from Apr 25, 2022

Conversation

JukkaL
Copy link
Collaborator

@JukkaL JukkaL commented Apr 22, 2022

Fixes #12633.

@github-actions

This comment has been minimized.

@JukkaL
Copy link
Collaborator Author

JukkaL commented Apr 22, 2022

mypy_primer found some regressions.

@JukkaL
Copy link
Collaborator Author

JukkaL commented Apr 22, 2022

The issue is that we are performing type operations during semantic analysis, when not everything may be correctly set up. We'll need to move any type operations to a later phase. We should probably have an assertion that gets triggered if trying to perform type operations when it's not okay to do so. This would save a lot of debugging.

@cdce8p
Copy link
Collaborator

cdce8p commented Apr 23, 2022

mypy_primer found some regressions.

Isolated the regression with Home Assistant. Hope that helps.

from __future__ import annotations
from dataclasses import dataclass
from typing import Generic, TypeVar

T = TypeVar("T")

@dataclass
class Parent2(Generic[T]):
    key: str | None = None

@dataclass
class Child2(Parent2[T]): ...


Child2(key=None)  # <-- error
error: Argument "key" to "Child2" has incompatible type "None"; expected "str"

@github-actions
Copy link
Contributor

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

@cdce8p
Copy link
Collaborator

cdce8p commented Apr 25, 2022

The last change does resolve all dataclass regressions I saw with Home Assistant.

A bit unfortunate that mypy_primer didn't found them earlier but I think it might be because it doesn't install project dependencies.

@JukkaL JukkaL merged commit cb6581a into master Apr 25, 2022
@JukkaL JukkaL deleted the fix-dataclass-regression branch April 25, 2022 18:12
JukkaL added a commit that referenced this pull request May 13, 2022
Move the attrs plugin to a later pass, so that we won't have placeholders. Fix
various issues related to forward references and generic inheritance, including
some crashes.

This is follow-up to #12762 and related to #12656 and #12633.
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.

Regression with generic TypeVar inheritance
3 participants