Skip to content

(馃悶) false negative with dataclass "non-default argument follows default argument"聽#12137

Open

Description

@KotlinIsland
from dataclasses import dataclass

@dataclass
class A:
    a: int = 0

@dataclass
class B(A):
    a: int
    b: str

Mypy shows no error but this fails at runtime with TypeError: non-default argument 'b' follows default argument

playground

Activity

  1. erictraut commented on Feb 7, 2022

    @erictraut

    I don't see anywhere that this behavior is documented, but the runtime logic is clearly inheriting the default value from the base class even thought the child class doesn't provide a default value. This check was also missing in pyright, so I just added it.

  2. sobolevn commented on Feb 10, 2022

    @sobolevn
    Member

    Funny thing, this code works correctly:

    from dataclasses import dataclass
    
    @dataclass
    class A:
        a: int = 0
    
    @dataclass
    class B(A):
        b: str

    But, mypy is confused about a redefinition in OP's example.

  3. noooonee commented on Nov 21, 2024

    @noooonee

    I happened saw a similar question, can this answer fix your issue?

    https://stackoverflow.com/a/69822584/1227329

  4. JonasFiechter commented on Jul 10, 2025

    @JonasFiechter

    Hello @noooonee. Adding (kw_only=True) into the decorator function Works for me, thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions