Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added enum values when converting to pretty type #68

Conversation

pavan-uppari
Copy link

Related to: #55

from pydantic_cli import run_and_exit, Cmd

from enum import Enum

class MyEnum(Enum):
    a = 1
    b = 2
    c = 3


class Options(Cmd):
    input_file: MyEnum

    def run(self) -> None:
        print(f"Mock example running with {self}")

if __name__ == "__main__":
    run_and_exit(Options, description=__doc__, version="0.1.0")

Before:

--input_file INPUT_FILE    (type:MyEnum *required*)

After:

--input_file INPUT_FILE    (type:MyEnum[1, 2, 3] *required*)

@mpkocher
Copy link
Owner

Is it possible to poke around in Pydantic and see if it has a utility function or mechanism to prettifying types?

I'm trying to avoid getting tangled up in prettifying every random type in the stdlib.

@pavan-uppari
Copy link
Author

Is it possible to poke around in Pydantic and see if it has a utility function or mechanism to prettifying types?

I'm trying to avoid getting tangled up in prettifying every random type in the stdlib.

Unfortunately, pydantic doesn't have any such option

@pavan-uppari
Copy link
Author

We can generate json schema for the model, and if there is any tool to generate prettified string for a field in schema, we can use that to prettify any type

@mpkocher
Copy link
Owner

Here's a related discussion from argparse and Enum support and the friction point with integrating these.

https://bugs.python.org/issue25061

@mpkocher
Copy link
Owner

mpkocher commented Sep 18, 2024

The workaround here is to explicitly translate/communicate the allowed values in the description of Field

class MyEnum(Enum):
    a = 1
    b = 2
    c = 3


class Options(Cmd):
    input_file: MyEnum = Field(..., description=f"Allowed values:{[x.value for x in MyEnum]}")

    def run(self) -> None:
        print(f"Mock example running with {self}")

Yields

usage: b.py --alpha ALPHA [--help] [--version]

options:
  --alpha ALPHA  Allowed values:[1, 2, 3] (type:MyEnum *required*)
  --help         Print Help and Exit
  --version      show program's version number and exit

Alternatively, just put garbage data in and the Pydantic (the source of truth) will tell you the allowed values. Not a great user experience, but it will work.

python b.py --alpha dragons
1 validation error for Options
alpha
  Input should be 1, 2 or 3 [type=enum, input_value='dragons', input_type=str]

(It's surprising to me that the specific enum class name (e.g., MyEnum), isn't communicated in the default error message from pydantic).

@pavan-uppari
Copy link
Author

Closing in favor of #70

@pavan-uppari pavan-uppari deleted the feat_enum_type_to_pretty_type branch September 20, 2024 05:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants