I have some Python 3.7 code and I am trying to add types to it. One of the types I want to add is actually an Union
of several possible strings:
from typing import Union, Optional, Dict
PossibleKey = Union["fruits", "cars", "vegetables"]
PossibleType = Dict[PossibleKey, str]
def some_function(target: Optional[PossibleType] = None):
if target:
all_fruits = target["fruits"]
print(f"I have {all_fruits}")
The problem here is that Pyright complains about PossibleKey
. It says:
"fruits is not defined"
I would like to get Pyright/Pylance to work.
I have checked the from enum import Enum
module from another SO answer, but if I try that I end up with more issues since I am actually dealing with a Dict[str, Any]
and not an Enum
.
What is the proper Pythonic way of representing my type?