python / python/mypy

generic dataclass with default (and typevar default)

Open
#18,812 1 comment 4 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug topic-dataclasses topic-pep-696
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the supplied reproduction with mypy 1.15.0 on Python 3.13.0 and compare the assignment error with the three reveal_type results. Trace the dataclass generic and type-variable-default handling implicated by the example; done means the assignment is accepted while all three revealed types remain as shown.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.