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

extra arguments are not tolerated by the parser, even with Extra.ignore #40

Open
lpasselin opened this issue Aug 27, 2021 · 6 comments
Open

Comments

@lpasselin
Copy link

lpasselin commented Aug 27, 2021

The parser raises an error when extra arguments are provided.

It could be useful to only parse known args.
For example, ifMyPydanticModel.Config.extra == pydantic.Extra.ignore, use parser.parse_known_args() to tolerate extra arguments.

@mpkocher
Copy link
Owner

I'm not sure that strongly coupling the model config of extras=ignore|forbid|allow semantics with the command line semantics would always be desired or expected behavior.

The current workaround for this case is to explicitly add an 'cli_args', 'others' or similar keyword to your Pydantic model.

from typing import Optional

from pydantic import BaseModel

from pydantic_cli import run_and_exit


class Options(BaseModel):
    alpha: int
    beta: float
    others: Optional[str]


def example_runner(opts: Options) -> int:
    print(f"Mock example running with {opts}")
    return 0


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

Running

$> python pydantic_cli_with_extras.py --alpha 1 --beta 1 --others '--pass-through-options 1234 a=1 b=2'
Mock example running with alpha=1 beta=1.0 others='--pass-through-options 1234 a=1 b=2'

@lpasselin
Copy link
Author

Consider the situation where the user cannot modify the arguments passed in the terminal.
We only want to parse some arguments and ignore others.

Also, by default, BaseModel.Config.extra == Extra.ignore.

@mpkocher
Copy link
Owner

Also, by default, BaseModel.Config.extra == Extra.ignore.

I think that pydantic's default of Extra.ignore would create a lot of frustrating and difficult to debug cases at the command line level. Having the default behavior to ignore unrecognized arguments seems like a lackluster default user experience. It would also be easy to miss this key behavior in docs unless it's documented in a several of places.

Also, pydantic_cli doesn't uses any of the core config values pydantic.BaseConfig to avoid getting tangled up with trying to attempting to interpret these semantically. To namespace the pydantic_cli config values, it defines settings using the "CLI_*" keys.

@lpasselin
Copy link
Author

Ok, thank you for your help. Fell free to reject the associated PR and close this issue if you have no plans on adding an option to ignore extra arguments.

I will most likely fork the project to fit my personal needs.

@mpkocher
Copy link
Owner

I don't think that using Extra.* will work.

However, adding a new key to the config of CLI_IGNORE_EXTRAS:bool = False seems like a reasonable compromise?

https://github.com/mpkocher/pydantic-cli/blob/master/pydantic_cli/core.py

@mpkocher
Copy link
Owner

@lpasselin Adding a PR that is very similar to one you closed but leveraging CLI_IGNORE_EXTRAS will probably work.

Please add an inline docs to the DefaultConfig in core.py and add a new test and we should be good to go.

Thanks for the feedback and comments.

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

No branches or pull requests

2 participants