Skip to content

Support Union of Enum and/or Literal in annotations for with_annotated decorator #1686

Closed

Description

@neoniobium

Feature Request

Support Union of Enum and/or Literal in annotations for with_annotated decorator. Especially unions for different Enums seams to be desirable to reduce code duplication.

Code Example

class App(Cmd):

    @with_annotated
    def do_something_with_enums(a: EnumA, b: EnumB, combine: EnumA | EnumB) -> None:
        ...

Activity

  1. KelvinChung2000 commented on Jun 10, 2026

    @KelvinChung2000
    Contributor

    I will add support for this use case. For now, I will allow only a mix of Enum and Literal, since they are "constant-like". For most other options, I think it will be very ambiguous to tell what it should resolve to.

  2. KelvinChung2000 commented on Jun 15, 2026

    @KelvinChung2000
    Contributor

    After some thinking, I am not sure if the use case actually makes sense, or if it is better to be done via a combination of the existing construct.

    • EnumA | EnumB: I cannot think of a case where you would want to do this. If A and B are relevant inputs, you will likely put them into a single enum.
    • EnumA | Literal["auto"]: this use case makes sense, since you might want an option that is not part of the enum, but you will still want a value. In this use case, would using the _missing_ method to set a default be a better idea?
    • EnumA | Literal["autoA", ...]: this basically falls back to using _missing_ again, since likely any of the literal options will go to

    I will add documentation and update the code path. Since right now, it will only trigger a hard error. I will add a new option that takes in an unknown member and let it trigger the _missing_ on the enum.

  3. neoniobium commented on Jun 15, 2026

    @neoniobium
    ContributorAuthor

    I use EnumA, EnumB as well as EnumA | EnumB as possible arguments for different functions.

    class App(Cmd):
    
        @with_annotated
        def do_A(a: EnumA) -> None:
            ...
    
        @with_annotated
        def do_B(b: EnumA) -> None:
            ...
    
        @with_annotated
        def do_AB(ab: EnumA | EnumB) -> None:
            ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions