Skip to content

"'field' in typed_dict_union" discrimination #13802

Closed
@ikonst

Description

@ikonst

Feature

Similar to tagged unions, I wish an in check could discriminate the following:

class A(TypedDict)
   foo: int

class B(TypedDict):
   bar: int

union = Union[A, B]
value: int
if 'foo' in union:
  value = union['foo']
else:
  value = union['bar']

Similarly:

value: int
if foo := union.get('foo'):
  value = foo
else:
  value = union['bar']

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions