Giter Site home page Giter Site logo

Comments (3)

tiangolo avatar tiangolo commented on August 10, 2024 1

Hey @lbjarre !

So, as you mention, you can actually use datetime and pass just a date with year, month, and day.

The problem is that the data type is implemented in Click, not in Typer, and implementing support for that specific use case would probably involve a lot of extra custom code in Typer itself.

Nevertheless, you can specify your own format, like:

from datetime import datetime
import typer


def main(date: datetime = typer.Argument(..., formats=["%Y-%m-%d"])):
    typer.echo(f"Year is: {date.year}")


if __name__ == "__main__":
    typer.run(main)

from typer.

github-actions avatar github-actions commented on August 10, 2024

Assuming the original issue was solved, it will be automatically closed now. But feel free to add more comments or create new issues.

from typer.

NikosAlexandris avatar NikosAlexandris commented on August 10, 2024

Nevertheless, you can specify your own format, like:

from datetime import datetime
import typer


def main(date: datetime = typer.Argument(..., formats=["%Y-%m-%d"])):
    typer.echo(f"Year is: {date.year}")


if __name__ == "__main__":
    typer.run(main)

This does not play nice with xarray's time indexing for example. An input like 2023-05 should be kept like that : 2023-05 to retrieve this slice of data. For example :

ds.sel(time="2000-01")
Out[10]: 
<xarray.Dataset>
Dimensions:  (time: 744)
Coordinates:
  * time     (time) datetime64[ns] 2000-01-01 ... 2000-01-31T23:00:00
Data variables:
    foo      (time) int64 0 1 2 3 4 5 6 7 8 ... 736 737 738 739 740 741 742 743

Instead, the following :

time: Annotated[Optional[datetime], typer.Argument(
            formats=["%Y", "%Y-%m", "%Y-%m-%d", "%Y-%m-%d %H", "%Y-%m-%d %H:%M", "%Y-%m-%d %H:%M:%S", "%Y-%m-%dT%H", "%Y-%m-%dT%H:%M", "%Y-%m-%dT%H:%M:%S", ],

will return, depending on input, always the first day and first month, even if not given :-/.
For example :

  • 2023 in the command line will be converted to 'time': datetime.datetime(2023, 1, 1, 0, 0),
  • 2023-05 in the command line will be converte to 'time': datetime.datetime(2023, 5, 1, 0, 0),
  • and so on

Trying to handle all cases manually is too much work, right? Any ideas?

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.