Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Way to specify KW_ONLY location in the constructor like in dataclass.KW_ONLY #1241

Open
OrHayat opened this issue Feb 20, 2024 · 0 comments
Open
Labels
dataclasses dataclass features we're lacking Feature

Comments

@OrHayat
Copy link

OrHayat commented Feb 20, 2024

dataclass in python 3.10 and above got KW_ONLY sentinel value
it allows you to mark where the constructor will start the KWARGS in the class
https://docs.python.org/3/library/dataclasses.html#dataclasses.KW_ONLY
i did not found a way to do this in attrs package

@dataclass()
class Foo():
    x:str=field(default="a")
    _:KW_ONLY
    y:bool

the function signature of the init will be

(x: str = "a", *, y: bool) -> None

the dataclass one will re order the items order in the constructor and handle inheritance properly(if needed)

https://docs.python.org/3/library/dataclasses.html#re-ordering-of-keyword-only-parameters-in-init

from dataclasses import dataclass,KW_ONLY,field

@dataclass
class Base:
    x: float = 15.0
    _: KW_ONLY
    y: int = 0
    w: int = 1

@dataclass
class D(Base):
    z: int = 10
    _: KW_ONLY
    t: int = field(kw_only=False, default=0)

function signature:

(x: float = 15, z: int = 10, t: int = 0, *, y: int = 0, w: int = 1) -> None
@OrHayat OrHayat changed the title is there any way to specify KW_ONLY location in the constructor like in dataclass.KW_ONLY Way to specify KW_ONLY location in the constructor like in dataclass.KW_ONLY Feb 20, 2024
@hynek hynek added Feature dataclasses dataclass features we're lacking labels Feb 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dataclasses dataclass features we're lacking Feature
Projects
None yet
Development

No branches or pull requests

2 participants