Code Monkey home page Code Monkey logo

Comments (10)

jackric avatar jackric commented on May 18, 2024 1

@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.

sylann avatar sylann commented on May 18, 2024 1

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.

jackric avatar jackric commented on May 18, 2024

I need this feature (actually for datetime.date like your example) - so please make PR!

from typer.

ananis25 avatar ananis25 commented on May 18, 2024

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.

ananis25 avatar ananis25 commented on May 18, 2024

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.

tmr232 avatar tmr232 commented on May 18, 2024

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.

jackric avatar jackric commented on May 18, 2024

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.

paulo-raca avatar paulo-raca commented on May 18, 2024

I wrote a PR that implements this, please let me know what you think: #443

from typer.

dsully avatar dsully commented on May 18, 2024

+1 would love to see @paulo-raca 's PR merged. Thanks!

from typer.

tiangolo avatar tiangolo commented on May 18, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.