Skip to content

Conversation

@kozlek
Copy link

@kozlek kozlek commented Aug 31, 2021

Implements an extra click type Date, based on built-in click.DateTime to enable Typer support for Python datetime.date.

This approach could unlock others unsupported Python build-in types, even if the best solution would be to implement them directly inside Click.

Another approach - more complex but more flexible - would be to autorise Type's users to define their own extra Click types at Typer's app level using a mapping "annotation -> click type implementation":

from datetime import date, datetime
from typing import Any, Optional, Sequence

import click
from typer import Typer


class Date(click.DateTime):
    name = "date"

    def __init__(self, formats: Optional[Sequence[str]] = None):
        self.formats = formats or ["%Y-%m-%d"]

    def _try_to_convert_date(self, value: Any, format: str) -> Optional[date]:
        try:
            return datetime.strptime(value, format).date()
        except ValueError:
            return None

    def __repr__(self) -> str:
        return "Date"


app = typer.Typer(custom_types={date: Date})

@app.command()
def main(birth: date):
    typer.echo(f"Interesting day to be born: {birth}")
    typer.echo(f"Birth day name: {day_name[birth.weekday()]}")

However, this would require more serious discussion and I'll be happy to open a dedicated issue if someone is interested 🙂

Most changes in this PR are tests / documentation related but I would like to say it was a pleasure to discover the powerful intrication between tests and documentation in place in this project 😃
This is well designed and very efficient to write 👏

@svlandeg svlandeg added the types Type hints and type checking label May 7, 2022
@InCogNiTo124
Copy link

bump. this is what I stumbled upon today and would be nice to have

@svlandeg svlandeg added feature New feature, enhancement or request p3 click Related to Click functionality labels Mar 6, 2024
@github-actions
Copy link
Contributor

github-actions bot commented Sep 1, 2025

This pull request has a merge conflict that needs to be resolved.

@github-actions github-actions bot added the conflicts Automatically generated when a PR has a merge conflict label Sep 1, 2025
Copy link
Member

@svlandeg svlandeg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you clarify why you'd need/want this, given that Typer already supports datetime?

@svlandeg svlandeg added waiting and removed click Related to Click functionality types Type hints and type checking labels Nov 19, 2025

```Python hl_lines="2 7 8 9"
{!../docs_src/parameter_types/date/tutorial001.py!}
```
Copy link
Member

@svlandeg svlandeg Nov 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a quick note here that if we do want to progress with this PR at some point, the docs should be updated with the new format, and using Typer() explicitely in the tutorial examples.

@github-actions github-actions bot removed the waiting label Nov 25, 2025
@kozlek
Copy link
Author

kozlek commented Jan 11, 2026

Long time no see ! 👋

I had a CLI app with a date parameter (to represent a birth day), which is a bit different from a datetime as it doesn't take care of timezone and time.
Having a date type is convenient because it matches exactly this use case, and it displays a proper format (%Y-%m-%d) when reading the help of the CLI.

Indeed, users may be confused to see that %Y-%m-%dT%H:%M:%S is a valid format to represent a birth day when using the built-in datetime type.

However, time has passed and Click custom types support has been been added to Typer: https://typer.tiangolo.com/tutorial/parameter-types/custom-types/#type-parser

Regarding this newly (2023) addition, I believe we can close this PR. Users seeking a date type are able to define their own Click type and use it using the click_type as specified in the documentation.

Thanks for your help !

@kozlek kozlek closed this Jan 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

conflicts Automatically generated when a PR has a merge conflict feature New feature, enhancement or request p3

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants