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

Discussão em 'Python' iniciado por Stack, Março 1, 2025.

  1. Stack

    Stack Membro Participativo

    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.

    Continue reading...

Compartilhe esta Página