Comments (10)
@ananis25 I think a nice approach would be to register custom types with a new function - either on typer
or a typer.Typer
object:
import typer
typer.register_type(datetime.date, lambda string: datetime.datetime.strptime(string, '%Y-%m-%d').date())
import typer
app = typer.Typer()
app.register_type(datetime.date, lambda string: datetime.datetime.strptime(string, '%Y-%m-%d').date())
typer.main.get_click_type
would then check an internal store of registered types before its default behaviour.
For bonus points, allow multiple deserializing functions to be tried in order. They could be specified as a list to register_type
, or multiple calls to register_type
from typer.
I agree with the solution proposed by @jackric.
Also, to add on top of that:
There are lots of "wrapper types" that can be called on a string input and give an augmented object that is still a string. Many more are just variants of already handled types.
So maybe if Typer did just try to call the annotated type by default, it would be a quick and easy way to solve many problems with not so much work.
It could even be enabled (or disabled) with an option in the Typer instanciation. In case some users somehow expect to get an error on unhandled types.
--
I think it should at least be possible to skip an unhandled type with a mere warning. To let users properly annotate their functions without having to use some crazy black magic.
from typer.
I need this feature (actually for datetime.date
like your example) - so please make PR!
from typer.
Hey @jackric, the method used above requires a global container, which makes me uneasy. You could probably use this snippet in your code and import the typer
module from it.
from typer.
If I understand correctly, the above snippet does exactly this, isn't it? I'd leave it to the maintainers to see if they really want to add this.
from typer.
One thing that is missing when registering custom types (or at least not explicitly clear) is the usage when I just want to add a converter to an existing type. For example - parsing a hexadecimal number. I want to extend the parsing, but the result type is still a number.
The options I see for this are either:
@app.command()
def command(number: int = Typer.Argument(converter=lambda x: int(x, 0)))
Or
class HexInt(int): pass
# Fill in registering custom type
@app.command()
def command(number: HexInt)
I am not sure what I think about either.
from typer.
My 2 cents: declaring the HexInt
class/type feels wrong, because later the type system says number
is HexInt
, when we really want to think of it as an int
.
from typer.
I wrote a PR that implements this, please let me know what you think: #443
from typer.
+1 would love to see @paulo-raca 's PR merged. Thanks!
from typer.
Thanks all! Support for this was added in #583, the new docs are here: https://typer.tiangolo.com/tutorial/parameter-types/custom-types/, it's available in Typer 0.8.0
, just released. 🎉
from typer.
Related Issues (20)
- How do I pass the None value explicitly? HOT 2
- Documentation is misleading. new `typer.run` behavior HOT 2
- See if rich 13.x is compatible HOT 4
- Using `some_type | None` syntax for type annotations causes error in python 3.11 HOT 12
- unlimited argument for an option with comma spliter HOT 1
- TAB completion is giving local directory files where command is called. HOT 2
- Support for bytes in Options and Arguments HOT 2
- Is it possible to include a Prolog in `--help` HOT 2
- Get the typer output with html format to provide it to termynal HOT 4
- how to use typer on class method __init__ with self argument, got this error: Error: Missing argument 'SELF'. HOT 2
- Support for localization of messages HOT 1
- Auto-completion when application works in 2 modes (GUI, CLI) HOT 1
- DOC: Documentation of passing multiple values in "option" vs "argument" isn't sufficiently explicit HOT 1
- SIGINT from docker is ignored HOT 4
- 🚀 Roadmap HOT 2
- Source distribution of 0.11.0 is missing the `docs_src` folder HOT 1
- 🐛 Upgrading from `typer<0.12.0` to `typer==0.12.0` breaks the install by partially removing the package/module files HOT 4
- Rich markup in generated Markdown docs
- Add CI configs to run tests on Windows and MacOS
- Rich markup in Zsh completion help lines
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from typer.