Skip to content

[BUG] callback with type Enum passes None back to the app command #223

@larsclaussen

Description

@larsclaussen

Describe the bug

When adding a callback to a typer.Option of type Enum the value is not passed back to the app command.

To Reproduce

Fairly easy to reproduce

  • Create a file main.py with:
import typer
from enum import Enum

app = typer.Typer()


class EndpointChoices(str, Enum):
    localhost = "localhost"
    staging = "staging"
    production = "production"


def models_callback(value: EndpointChoices):
    typer.echo(f"Validating param: {value}, {type(value)}")
    return value


@app.command()
def models(endpoint: EndpointChoices = typer.Option(
            ..., case_sensitive=False, callback=models_callback
        )
):
    """List available models"""
    raise Exception(endpoint)

if __name__ == "__main__":
    app()
  • Call it with:
python main.py --endpoint localhost
  • It outputs:
raise Exception(endpoint)
Exception: None

  • But I expected it to output:
localhost, <enum 'EndpointChoices'>

Expected behavior

I would expect the option argument to be present in the scope of the app command.

Screenshots

Environment

  • OS: Ubuntu 20.04
  • Python 3.8.5
  • Typer Version 0.3.2

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions