I am trying to create a TypedDict for better code completion and am running into an issue.
I want to have a fixed set of keys (an Enum) and the values to match a specific list of objects depending on the key.
For example:
from enum import Enum
class OneObject:
pass
class TwoObject:
pass
class MyEnum(Enum):
ONE: 1
TWO: 2
I am looking to have something like this:
from typing import TypedDict
class CustomDict(TypedDict):
MyEnum.ONE: list[OneObject]
MyEnum.TWO: list[TwoObject]
However, I am getting Non-self attribute could not be type hinted and it doesn't really work. What are my options?
This is not compatible with the
TypedDictspecification as laid out in PEP 589. Let me quote: (emphasis mine)So using arbitrary enum members for defining
TypedDictkeys is invalid.While
TypedDictdoes also support an alternative, functional definition syntax and you could theoretically make your enum have thestrdata type by doingclass MyEnum(str, Enum): ..., you would still probably not be able to define aTypedDictwith those enum members in a way that your type checker understands.That is because only actual string literals are officially accepted as keys as mentioned in the section on the Use of Final Values and Literal Types. Quote: (again, emphasis mine)
In other words, whether something like the following is supported depends entirely on any given type checker:
Mypy (currently) does not and gives the output:
error: Invalid TypedDict() field name. (By the way, I tested it withFinalvariables as keys and those are also rejected.)So depending on what your use case is, you will probably have to bite the bullet and explicitly type out the enum/key names again or just not use an enum for that in the first place, as suggested by @artem in his answer.