-
-
Notifications
You must be signed in to change notification settings - Fork 672
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
[FEATURE] Support for common pydantic types #181
Comments
P.S. @tiangolo many thanks for creating another gem! |
I have immediate usecases for |
Hello, I did some experiments so I'm sharing thoughts:
My experiment was therefore not opinionated on supporting
to a later
It is important to defer it as one of the latest cases, because it is not uncommon to have subclasses of Enum to be also subclasses of With this change in mind, we've got half of the original request: we can annotate our code with the actual expected data. What's missing is the automagical conversion to the expected type when the callback gets executed. All that being said, I would like to thank you @tiangolo for the great and inspirational work you do with your libraries ❤️ , and I'd really be eager to contribute if you accept my proposal of simplified implementation of this ticket. |
Hi I am trying to pass a URL as a parameter, hence check it is valid so this feature would be great. Any update on it? Thanks! |
Hello, I've made a recent attempt to progress on the topic in order to propose a pull request. Consider the following snippet: app = Typer()
EmailArg = Annotated[EmailStr, Argument(click_type=click.STRING)]
@app.command("something_with_email")
@validate_arguments
def something(user_email: EmailArg):
print(user_email)
if __name__=='__main__':
app() It's enough IMO to do the job, and if you want to go even further, it's possible to even plug a callback function to the argument to catch a pydantic error and return a typer Exception def validate_email(ctx: Context, value: str):
if ctx.resilient_parsing: # handling autocompletion
return
try:
return EmailStr._validate(value, None) # this one is a bit hacky but just for the sake of demo
except ValueError as exc:
raise BadParameter(str(exc)) from exc
EmailArg = Annotated[EmailStr, Argument(click_type=click.STRING, callback=validate_email)]
@app.command("something_with_email")
def something(user_email: EmailArg):
print(user_email) however there are hacky parts here and there in code above, which I'd be delighted to not have to put in my production code, but I think we'd need a strong statement from @tiangolo or core team of this repository whether we want to go or not into adding pydantic as strong dependency for typer. |
Update on this ticket, I've found some free time to work a bit on a tryout for pydantic types integration while still keeping it an optional dependency. |
Note for people coming here from Google who really want
|
It will be great to add support for common pydantic types, namely
EmailStr
andHttpUrl
. Possible?The obvious use case is to pass and emails and HTTP URLs strings as command line parameters and being validated automagically - similar to what FastAPI does.
The text was updated successfully, but these errors were encountered: