Skip to content

TypedDict typecheck fails if extra dict keys present #6223

Closed
@zhammer

Description

@zhammer
  • Are you reporting a bug, or opening a feature request?
    Bug (possibly, as per @ilevkivskyi on gitter)
Zach-Hammer:typedict zachhammer$ pipenv graph
mypy==0.660
  - mypy-extensions [required: >=0.4.0,<0.5.0, installed: 0.4.1]
  - typed-ast [required: >=1.2.0,<1.3.0, installed: 1.2.0]

Zach-Hammer:typedict zachhammer$ cat person.py 
from mypy_extensions import TypedDict


class Person(TypedDict):
    name: str
    age: int


def api_fetch_person() -> Person:
    return {
        'name': 'Zach',
        'age': 25,
        'meta': 'other info'
    }

Zach-Hammer:typedict zachhammer$ pipenv run mypy person.py 
person.py:10: error: Extra key 'meta' for TypedDict "Person"
  • What is the actual behavior/output?
    I have a TypedDict Person that has fields name and age
    I have a function that returns a dictionary with name, age, and meta
    mypy throws error: Extra key 'meta' for TypedDict "Person"

  • What is the behavior/output you expect?
    I was testing this out of curiosity, since it's not specified (as far as i can tell) in the TypedDict documentation. I wanted to see if typeddict allowed extra, unspecified fields. (in which case it'd behave like a typescript interface). I posted the outcome in the mypy gitter and @ilevkivskyi suggested I open an issue here.

  • Do you see the same issue after installing mypy from Git master?
    Can't do this at the moment but can try over the weekend

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions