I'm trying to understand how to use Python 3.9 type hint to annotate a function that receives a dict with optional or multiple type values. I know that with Typing module, I can use easily Union
and Optional
. But I'd like to know if it is possible to achieve that using only Python 3.9 annotations. I didn't found anything on this matter on PEP 585.
Reference: How should I use the Optional type hint?
Example:
From Typing import Dict, Optional, Union
def foo(bar: Dict[Union[str, int], Optional[str]]) -> bool: return True
Should I do instead:
from __future__ import annotations
def foo(bar: dict[[str, int], [str, None]) -> bool: return True