Skip to content

Default values in enum struct-variants #4109

@EricLBuehler

Description

@EricLBuehler

Hello everyone,

Thank you for your great work here. Our project makes extensive use of struct enum variants. However, we have many variants which should have default values: some of those variants are Option<...> and should default to None if they are not specified. This behavior would be similar to the kwargs in functions, where a default is used if the value is not specified.

Currently, for this enum:

#[pyclass]
enum Thing
  A {
    x: Option<i32>,
    y: i32,
    z: i32,
  }
}

I would use it like this: Thing.A(x=None, y=5, z=1).

However, setting x to None manually makes the code more verbose. It would be easier for the user to do the following:

#[pyclass]
enum Thing
  A {
    #[pyo3(default = None)] # <----- Add something like this?
    x: Option<i32>,
    y: i32,
    #[pyo3(default = 1)]
    z: i32
  }
}

Which would be used like this: Thing.A(y=5) where x=None and z=1.

I am not sure how difficult this is to implement, but it would be much appreciated to have this feature!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions