Skip to content

create_model_from_typeddict reports a type error when given the correct input type #3008

@lewiswolf

Description

@lewiswolf

Checks

  • [x ] I added a descriptive title to this issue
  • [x ] I have searched (google, github) for similar issues and couldn't find anything
  • [x ] I have read and followed the docs and still think this is a bug

Bug

When running mypy using create_model_from_typeddict() I get a type error for the input - the code however works as expected. When I run the code below, using mypy myscript.py, I get the following error.

error: Argument 1 to "create_model_from_typeddict" has incompatible type "Type[SomeDict]"; expected "Type[TypedDict]"

Output of python -c "import pydantic.utils; print(pydantic.utils.version_info())":

pydantic version: 1.8.2
pydantic compiled: True
install path: /Users/lewiswolstanholme/.local/share/virtualenvs/drum-shape- 
predictor-YZkKhtr_/lib/python3.9/site-packages/pydantic
python version: 3.9.6 (default, Jun 29 2021, 06:20:32)  [Clang 12.0.0 (clang-1200.0.32.29)]
platform: macOS-10.15.7-x86_64-i386-64bit
optional deps. installed: ['typing-extensions']
from typing import TypedDict 
import pydantic


class SomeDict(TypedDict):
    val: int
    name: str

# this could be a valid/invalid declaration
obj: SomeDict = {
    'val': 12,
    'name': 'John',
}

# validate with pydantic
try:
    pydantic.create_model_from_typeddict(SomeDict)(**obj)

except pydantic.ValidationError as exc: 
    print(f"ERROR: Invalid schema: {exc}")

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug V1Bug related to Pydantic V1.X

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions