An alternative to Annotated

Hi guys, there hasn’t been much movement here since December, but the OP pointed me to the syntax proposed here as an option for the idea I have proposed for a small enhancement to the the dataclass machinery, here: Dataclasses - Sentinel to Stop creating “Field” instances

Seeing its potential I have implemented a proof-of-concept which allows to declare the attributes of the dataclasses using @ to annotate them and influence the final form of the attribute.

Github Gist: Dataclass with Field Annotations using @

This code

    @at_dataclass
    class A:
        a: int
        b: int @ KW_ONLY = 25
        c: int @ NO_INIT = 5
        d: list[str] @ NO_INIT_FACTORY = list
        e: int @ NO_INIT | Dummy() | Dummy() = 0
        f: int @ [NO_INIT, Dummy(), Dummy()] = 1
        g: int @ NO_FIELD = 7

would translate to this

    @dataclass
    class A:
        a: int
        b: int = field(kw_only=True, default=25)  # or declared after _: KW_ONLY
        c: int = field(init=False, default=5)
        d: list[str] = field(init=False, default_factory=list)
        e: Annotated[int, Dummy(), Dummy()] = field(init=False, default=0)
        f: Annotated[int, Dummy(), Dummy()] = field(init=False, default=1)
        # The following attribute which was NO_FIELD is not managed by `dataclass`
        g: int = 7

The @ syntax would seem to add clarity and readability.

| is used as one of the two options to separate annotations because it won’t throw a SyntaxError. The other option is to use a list as shown with attribute f.

The syntax allows whitespace in between @ and the declarations and also @NO_INIT with no intervening whitespace.

Personally I see the value in the proposal made by Cornelius.

Best regards