-
Notifications
You must be signed in to change notification settings - Fork 290
Should operator | on TypedDict allow for creating intersection-like dicts? #1445
Description
I'm wondering how is the relation between the following PEPs:
- PEP 584 which introduces operator
|for dicts, but doesn't mention how it should behave forTypedDict. - PEP 589 which introduces
TypedDict, but doesn't mention the operator|.
According to PEP 589, multiple inheritance can be used to create a combined (or "intersection like") dict, and operator | provides the corresponding behavior at runtime. Thus it would be nice if the type system could handle the combination properly (similar to how it is possible in TypeScript):
from typing import TypedDict
class HasFoo(TypedDict):
foo: int
class HasBar(TypedDict):
bar: int
class HasFooAndBar(HasFoo, HasBar):
...
def f(a: HasFoo, b: HasBar) -> HasFooAndBar:
return a | bFrom a runtime and type-checking perspective this code looks valid, but currently mypy does not accept it (playground):
main.py:15: error: Incompatible return value type (got "HasFoo", expected "HasFooAndBar") [return-value]
main.py:15: error: Unsupported operand types for | ("HasFoo" and "HasBar") [operator]
Pyright seems to have the same behavior.
Apparently operator | can only be used for two instances of the same typed dict, which as far as I can see has limited use cases, because using the operator | on two dicts that already have the same fields is kind of pointless (perhaps it mostly makes sense if the type used total=False).
Possible related discussions and issues I've found:
- Introduce an Intersection #213 (since this feature for
TypedDictprobably falls into the "intersection type" category as well?) - Should augmented assignment operator on
TypedDictfail if type is compatible? microsoft/pylance-release#2300 - Support
|and|=operators forTypedDictmicrosoft/pyright#2951