Skip to content

TypedDict.get returns an object if a dictionary has more than one field and the types on those fields differ. #2612

Closed
@rowillia

Description

@rowillia

This appears to be due to TypedDict relying on a fallback.

from typing import List, Optional
from mypy_extensions import TypedDict

TaggedPoint = TypedDict('TaggedPoint', {'type': str, 'x': int, 'y': int})
Point3D = TypedDict('Point3D', {'x': int, 'y': int, 'z': int})
BadPointSet = TypedDict('BadPointSet', {
    'total_length': int,
    'points': List[TaggedPoint]
})
OKPointSet = TypedDict('OKPointSet', {
    'points': List[TaggedPoint]
})

def foo(point_3d: Point3D,
        tagged_point: TaggedPoint,
        bad_points: BadPointSet,
        ok_points: OKPointSet) -> None:
    reveal_type(point_3d.get('x'))  # error: Revealed type is 'builtins.int*
    reveal_type(tagged_point.get('x'))  # error: Revealed type is 'builtins.object*
    reveal_type(bad_points.get('points'))  # error: Revealed type is 'builtins.object*'
    reveal_type(ok_points.get('points'))  # Revealed type is 'builtins.list*[TypedDict(x=builtins.int, y=builtins.int, _fallback=test_typed_dict_get.TaggedPoint)]'

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions