How to set a default value for an optional date argument within a typed function?

Viewed 28
from datetime import date

def my_func(start_date: date) -> None:
    print(start_date)

How to pass a default value to start_value while simultaneously adhere to mypy rules and doesn't provide a function in a function signature at the same time?

If I were to do start_date: date = "2020-12-31" mypy will rightly claim the following error:
Incompatible default for argument "start_date" (default has type "str", argument has type "date")

On the other hand, if I were to provide start_date: date = date(2020,12,31), I am performing a function call in an argument default. This could lead to unforeseen consequences as this function call will be executed once the function gets parsed, not when it's actually called.

So, what are my possibilities to provide a default start_date?

0 Answers
Related