I have 2 classes. One that inherits TypedDict class in which I have set of keys that should accept certain type of data:
class UserType(TypeDict):
first_name: str
last_name: str
age: int
In another class Customer
, I am using UserType
dict for storing the information of the customer and have a method to update the information:
class Customer:
def __init__():
self._userInfo: UserType = {
"first_name": "John",
"last_name": "John",
"age": 27
}
def set_info(key: str, val: Any):
self._userInfo[key] = val
In set_info
I want to type hint key
to show only keys that are defined in UserType
like:
customer = Customer()
customer.set_info(SHOULD HINT KEY HERE (first_name, ...), "Somedata")
dict. I tried something like:
def set_info(key: UserType, val: Any):
...
But this didn't work. I guess, I am trying to use it as interfaces as I am coming from strict typed language background. Any idea how this can be achieved? I am flexible to change it something different other than a dict