pythonapifieldpython-dataclasses

Avoid explicit `default` keyword in Python's dataclass field


PEP 681 introduced dataclass transforms. Among it features, it provides “field specifiers, which describe attributes of individual fields that a static type checker must be aware of, such as whether a default value is provided for the field"

Is there a way to use default as a positional argument, instead of a keyword argument?

I would like that this code:

@create_model(init=False)
class CustomerModel:
    id: int = model_field(default=3)
    name: str

looks like this:

@create_model(init=False)
class CustomerModel:
    id: int = model_field(3)
    name: str

without any complains from the type checkers.


Solution

  • In the spec it seems to pretty clearly state:

    This specification formalizes the names and meanings of the parameters that must be understood for static type checkers. These standardized parameters must be keyword-only.

    So I think that means the answer is no.