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

Use FieldInfo repr. This non-trivially changes how the output is displayed. #70

Merged
merged 7 commits into from
Sep 20, 2024

Conversation

mpkocher
Copy link
Owner

Use Pydantic repr on FieldInfo. I believe this is a reasonable tradeoff for robustness.

usage: example.py --input_file INPUT_FILE --alpha ALPHA --beta BETA --state STATE --name NAME [--help] [--version]

options:
  --input_file INPUT_FILE
                        FieldInfo(annotation=Literal[1, 2], required=True)
  --alpha ALPHA         FieldInfo(annotation=int, required=True, frozen=True)
  --beta BETA           FieldInfo(annotation=int, required=True, metadata=[Gt(gt=0)])
  --state STATE         FieldInfo(annotation=State, required=True, description='The State of the Job to filter on.')
  --name NAME           FieldInfo(annotation=Union[NoneType, str], required=True)
  --help                Print Help and Exit
  --version             show program's version number and exit

@mpkocher
Copy link
Owner Author

#69 and #68

@mpkocher
Copy link
Owner Author

This creates some other issues. Any other metadata (e.g., extras) is pushed to the Field and also displayed.

from pydantic import Field
from pydantic_cli import run_and_exit, Cmd


class Options(Cmd):
    alpha: int = Field(..., cli=('-a', '--alpha'), other=["other metadata"])

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

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

Yields:

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

options:
  -a ALPHA, --alpha ALPHA
                        FieldInfo(annotation=int, required=True, json_schema_extra={'cli': ('-a', '--alpha'), 'other': ['other metadata']})
  --help                Print Help and Exit
  --version             show program's version number and exit

At a minimum, cli needs to be deleted after it's processed.

It's not clear if the metadata should also be displayed. There arguments for and against displaying it here.

https://docs.pydantic.dev/latest/api/fields/

The **kwargs "extra" is deprecated (which is a bit of another problem with cli=('-a', '--alpha').

@pavan-uppari
Copy link

pavan-uppari commented Sep 19, 2024

I think __repr_str__ would be better option than repr

with repr

options:
  --input_file INPUT_FILE
                        FieldInfo(annotation=str, required=True)
  --max_records MAX_RECORDS
                        FieldInfo(annotation=int, required=True)

with __repr_str__

--input_file INPUT_FILE
                        annotation=str required=True
  --max_records MAX_RECORDS
                        annotation=int required=True

No need to display FieldInfo in the terminal

@mpkocher

@mpkocher
Copy link
Owner Author

No need to display FieldInfo in the terminal

@mpkocher

I experiment with that but it looked wonky. Specifically for cases where there's a custom description for a field.

usage: example.py --input_file INPUT_FILE --alpha ALPHA --beta BETA --state STATE --name NAME [--help] [--version]

options:
  --input_file INPUT_FILE
                        annotation=Literal[1, 2], required=True
  --alpha ALPHA         annotation=int, required=True, frozen=True
  --beta BETA           annotation=int, required=True, metadata=[Gt(gt=0)]
  --state STATE         annotation=State, required=True, description='The State of the Job to filter on.'
  --name NAME           annotation=Union[NoneType, str], required=True
  --help                Print Help and Exit
  --version             show program's version number and exit

I'm going to use "Field" and manually create the display. This is perhaps a slight improvement.

usage: example.py --input_file INPUT_FILE --alpha ALPHA --beta BETA --state STATE --name NAME [--help] [--version]

options:
  --input_file INPUT_FILE
                        Field(annotation=Literal[1, 2], required=True)
  --alpha ALPHA         Field(annotation=int, required=True, frozen=True)
  --beta BETA           Field(annotation=int, required=True, metadata=[Gt(gt=0)])
  --state STATE         Field(annotation=State, required=True, description='The State of the Job to filter on.')
  --name NAME           Field(annotation=Union[NoneType, str], required=True)
  --help                Print Help and Exit
  --version             show program's version number and exit

@mpkocher mpkocher merged commit b343dc3 into master Sep 20, 2024
3 of 4 checks passed
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