[Tutor] How to type annotate complex dictionary containing lambdas?
boB Stepp
robertvstepp at gmail.com
Sun Nov 17 14:46:39 EST 2019
On Sun, Nov 17, 2019 at 9:26 AM Mats Wichmann <mats at wichmann.us> wrote:
>
I will get back later to the class refactoring operation, but thanks
for your example. It will be quite helpful!
> As you your actual question...
>
> >> goal_year_params: Dict[str, Any] = {
>
> the two types that the dict's values can be per your snip is str or
> list, so start there:
>
> goal_year_params: Dict[str, Union(str, List)]
A typo in there. "Union" has to have brackets not parentheses.
I tried that and got an error:
pages_per_day.py|93 col 9 error| Dict entry 2 has incompatible type
"str": "Callable[[Any], date]"; expected "str": "Union[str,
List[Any]]"
Apparently because of the lambda outside of the list, I apparently
must annotate them as "Callable". Before making my post on this to
Tutor, I had tried variations on this, but I must have gotten lost in
the twisted unnecessary complications of my dictionary! ~(:>)) I
will play around some more with getting the annotations right before
addressing refactoring the dictionary mess into something reasonable.
Point of honor, ya know!
> if you want to go further you can then specify what the list consists
> of, etc.
>
> >> "greeting_msg": "Please enter the date by which you wish to attain"
> >> " your goal.\n",
> >> "input_prompt": "Enter year of your goal as an integer: ",
> >> "date_value_err_ck": lambda goal_year: date(goal_year, 1, 1),
> >> "err_msg": "That is not a valid year. Please try again.",
> >> "conditions": [
> >> (
> >> lambda goal_year: goal_year < date.today().year,
> >> lambda goal_year: print(
> >> "Have you invented a time machine? If not,
> >> please enter a"
> >> " year that makes more sense!"
> >> ),
> >> ),
> >> (
> >> lambda goal_year: goal_year >= date.today().year + 100,
> >> lambda goal_year: print(
> >> "Have you discovered the secret to eternal life?
> >> And how"
> >> " long is this book anyway? Please enter a year that"
> >> " makes more sense."
> >> ),
> >> ),
> >> ],
> >> }
> >
> >
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
--
boB
More information about the Tutor
mailing list