Skip to content

PEP-681 - Inheriting class doesn't recognize kw_only=True from parent class #17375

Open
@Avishayy

Description

@Avishayy

Bug Report

A class inheriting a class with kw_only=True and using a dataclass_transform doesn't recognize kw_only being set, even though in runtime it does not yield an error. Originally found in sqlalchemy's usage (sqlalchemy/sqlalchemy#11485)

To Reproduce

from typing import Optional, Any
import typing_extensions


def field(default: Any=None, init: bool=True, repr: bool=True) -> Any:
    pass

@typing_extensions.dataclass_transform(
    field_specifiers=(
        field,
    )
)
class ModelMeta(type):
    pass

class ModelBase(metaclass=ModelMeta):
    def __init_subclass__(cls, kw_only: bool=False) -> None:
        pass

class Base(ModelBase, kw_only=True):
    pass

class Vehicle(Base):
    b: Optional[str] = field(default=None)
    c: int

Expected Behavior

No error.

Actual Behavior

error: Attributes without a default cannot follow attributes with one  [misc]
Found 1 error in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 1.10
  • Mypy command-line flags:
  • Mypy configuration options from mypy.ini (and other config files):
  • Python version used: 3.12

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions