mypy icon indicating copy to clipboard operation
mypy copied to clipboard

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

Open Avishayy opened this issue 8 months ago • 1 comments

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 (https://github.com/sqlalchemy/sqlalchemy/issues/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

Avishayy avatar Jun 13 '24 07:06 Avishayy