PEP-681 - Inheriting class doesn't recognize kw_only=True from parent class
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
The typing spec (and the original PEP 681) are not clear on whether this should work. Currently, it does not work in any of the major type checkers. I think one could make a good argument in favor of it. It would be good for the typing spec to be updated to clarify. @Avishayy, if you're interested in pursuing this, here's a link to the process for modifying the typing spec.