Dataclasses - Sentinel to Stop creating "Field" instances

Big plus one on this being something that should be added somehow. For me the most common usage is cache-like fields that get computed on demand but should be stored.

Using _: <name> for both doesn’t work AFAIK because the later overrides the earlier in the resulting __annotations__ dict.

With regard to Annotated, I think the syntax currently is ugly, but by other proposal would help with that:

@dataclass
class Example:
    a: bool = True
    kw_a: bool @ KW_ONLY = False
    b: str @ NO_INIT = "hello"
    c: str @ NO_FIELD = "hello"
    d: list @ NO_INIT_FACTORY = list

It would also convenient encompass the existing usage of KW_ONLY.

For Annotated you are probably going to here the counter argument that type checkers shouldn’t care about it. IMO, this is not a hill worth fighting over. Instead the goal should be to design an alternative that everything is happy with.

1 Like