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

generic dataclass with default (and typevar default) #18812

Open
asottile-sentry opened this issue Mar 17, 2025 · 1 comment
Open

generic dataclass with default (and typevar default) #18812

asottile-sentry opened this issue Mar 17, 2025 · 1 comment
Labels
bug mypy got something wrong topic-pep-696 TypeVar defaults

Comments

@asottile-sentry
Copy link

Bug Report

this is the sameish as #12962 however now that PEP 696 exists I expect this to work

To Reproduce

from typing import Literal
from dataclasses import dataclass

@dataclass
class D[TBool: (Literal[True], Literal[False]) = Literal[True]]:
    val: TBool = True


reveal_type(D())
reveal_type(D(True))
reveal_type(D(False))

Expected Behavior

I expect no errors however

Actual Behavior

t3.py:6: error: Incompatible types in assignment (expression has type "Literal[True]", variable has type "TBool")  [assignment]
t3.py:9: note: Revealed type is "t3.D[Literal[True]]"
t3.py:10: note: Revealed type is "t3.D[Literal[True]]"
t3.py:11: note: Revealed type is "t3.D[Literal[False]]"
Found 1 error in 1 file (checked 1 source file)

(the reveal types are all correct -- mostly just to demonstrate that the idea works)

Your Environment

  • Mypy version used: 1.15.0
  • Mypy command-line flags: n/a
  • Mypy configuration options from mypy.ini (and other config files): n/a
  • Python version used: 3.13.0
@asottile-sentry asottile-sentry added the bug mypy got something wrong label Mar 17, 2025
@sterliakov
Copy link
Collaborator

sterliakov commented Mar 18, 2025

This is very similar to #3737 but is even more complex (substituting in one signature vs checking the whole class for conformance). As of now, mypy does not support any variables/parameters with concrete defaults annotated with a TypeVar (maybe except for a few local narrowing cases, but those are irrelevant here). You can overcome this with __init__ overloads similar to the function example in #3737, but that would probably defeat the benefits of using a dataclass.

@sterliakov sterliakov added the topic-pep-696 TypeVar defaults label Mar 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-pep-696 TypeVar defaults
Projects
None yet
Development

No branches or pull requests

2 participants