Skip to content

TypedDict doesn't respect subtypes or promoted types. #2610

Closed
@rowillia

Description

@rowillia

Repro:

test_typed_dict_return_value.py

from mypy_extensions import TypedDict

TaggedPoint = TypedDict('TaggedPoint', {'type': str, 'x': float, 'y': float})

def create_tagged_point():
    # type: () -> TaggedPoint
    return TaggedPoint(type='2d', x=42, y=1337)
$ mypy test_typed_dict_return_value.py
test_typed_dict_return_value.py:9: error: Incompatible return value type (got "TypedDict(type=str, x=int, y=int)", expected "TaggedPoint")

As an aside, the error message above is fairly hard to decode. It took me a few hours to realize what was going on.

The problem also exists with Any types:

from mypy_extensions import TypedDict

TaggedPoint = TypedDict('TaggedPoint', {'type': str, 'x': int, 'y': int})

def create_tagged_point(some_value):
    # type: (Any) -> TaggedPoint
    return TaggedPoint(type=some_value.type, x=some_value.x, y=some_value.y)

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions