Skip to content

Should operator | on TypedDict allow for creating intersection-like dicts? #1445

@bluenote10

Description

@bluenote10

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 for TypedDict.
  • 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 | b

From 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:

Metadata

Metadata

Assignees

No one assigned

    Labels

    topic: featureDiscussions about new features for Python's type annotations

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions