-
-
Notifications
You must be signed in to change notification settings - Fork 671
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
Support custom parameter types #443
Conversation
@@ -38,7 +38,7 @@ Documentation = "https://typer.tiangolo.com/" | |||
[tool.flit.metadata.requires-extra] | |||
test = [ | |||
"shellingham >=1.3.0,<2.0.0", | |||
"pytest >=4.4.0,<5.4.0", | |||
"pytest >=6.2.5", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if I should bump it on this PR, but it was necessary to test it under Python 3.10
Codecov Report
@@ Coverage Diff @@
## master #443 +/- ##
===========================================
- Coverage 100.00% 99.98% -0.02%
===========================================
Files 285 285
Lines 6467 6506 +39
===========================================
+ Hits 6467 6505 +38
- Misses 0 1 +1
Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here. |
📝 Docs preview for commit 847d207 at: https://62faaa2b7f883f06b9e3e56a--typertiangolo.netlify.app |
While most CLI parameters are strings, ints, etc, we sometimes need custom types. Currently typer has no support for it (See fastapi#77), and while there are a few hacks, the likely solution is to add a 'str' argument and parse it inside the main function. This PR adds support for custom types in 3 different ways: - Manually specifying a `click_type` - Manually specifying a `parse` function - Using a Callable type annotation (It is very common for types to have a string constructor, like `int("1")`)
847d207
to
24466a3
Compare
@tiangolo any reason this has not been merged? I'm constantly missing Decimal type. Is there anything I can do to help / speed this up? |
I've been able to verify that this PR supports parsing of custom parameter types: import click
import typer
from typing import Optional
class CustomClass:
def __init__(self, value):
self.value = value
def __str__(self):
return f"<{self.__class__.__name__}: {self.value=}>"
def __repr__(self):
return str(self)
class CustomClassParser(click.ParamType):
name = "CustomClass"
def convert(self, value, param, ctx):
return CustomClass(value*3)
def parse_custom_class(value):
return CustomClass(value*2)
def main(
arg_1: str = typer.Argument("arg_1", click_type=CustomClassParser()),
arg_2: str = typer.Argument("arg_2", parser=parse_custom_class),
opt_1: str = typer.Option("opt_1", click_type=CustomClassParser()),
opt_2: str = typer.Option("opt_2", parser=parse_custom_class)
):
print(
(
arg_1,
arg_2,
opt_1,
opt_2,
)
)
if __name__ == "__main__":
typer.run(main) invocation:
And the corresponding help message
|
Since I'm not able to update this PR, I have created #583. @paulo-raca , I've kept your contribution in the git commit history. Thank you for getting this started 🎉 |
I'm happy to see this moving on, thank you! Closing this PR in favour of yours |
…) (#583) * Support custom parameter types While most CLI parameters are strings, ints, etc, we sometimes need custom types. Currently typer has no support for it (See #77), and while there are a few hacks, the likely solution is to add a 'str' argument and parse it inside the main function. This PR adds support for custom types in 3 different ways: - Manually specifying a `click_type` - Manually specifying a `parse` function - Using a Callable type annotation (It is very common for types to have a string constructor, like `int("1")`) * Document how to parse custom objects with Typer. Typer supports parsing of cutom types with: - A user provided parser class - A click custom type parser * 🎨 [pre-commit.ci] Auto format from pre-commit.com hooks * 📝 Tweak and simplify docs * ✅ Tweak tests to run scripts and remove pragma: nocover in examples * 📝 Tweak examples for docs, add types and remove # pragma: nocover * ♻️ Tweak implementation checking for parser and Click type --------- Co-authored-by: Paulo Costa <me@paulo.costa.nom.br> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Sebastián Ramírez <tiangolo@gmail.com>
While most CLI parameters are strings, ints, etc, we sometimes need custom
types.
Currently typer has no support for it (See #77), and while there are a few hacks, the
likely solution is to add a 'str' argument and parse it inside the main function.
This PR adds support for custom types in 3 different ways:
click_type
parse
functionint("1")
)