Skip to content

mypy: Add create_model overload#2748

Merged
PrettyWood merged 4 commits intopydantic:masterfrom
uriyyo:mypy-create-model-default
Sep 4, 2021
Merged

mypy: Add create_model overload#2748
PrettyWood merged 4 commits intopydantic:masterfrom
uriyyo:mypy-create-model-default

Conversation

@uriyyo
Copy link
Copy Markdown
Contributor

@uriyyo uriyyo commented May 5, 2021

Add create_model overload

Currently mypy will fail on this code:

from pydantic import create_model

Foo = create_model("Foo")
error: Need type annotation for 'Foo'  [var-annotated]

With this PR mypy will correctly resolve return value type of create_model:

from pydantic import create_model

Foo = create_model("Foo")
reveal_type(Foo)
note: Revealed type is 'Type[pydantic.main.BaseModel]'

And it will still work for a cases when __base__ is specified:

from pydantic import create_model, BaseModel


class Bar(BaseModel):
    a: int


Foo = create_model("Foo", __base__=Bar)
reveal_type(Foo)
note: Revealed type is 'Type[temp.Bar*]'

Checklist

  • Unit tests for the changes exist
  • Tests pass on CI and coverage remains at 100%
  • Documentation reflects the changes where applicable
  • changes/<pull request or issue id>-<github username>.md file added describing change
    (see changes/README.md for details)
  • My PR is ready to review, please add a comment including the phrase "please review" to assign reviewers

@uriyyo
Copy link
Copy Markdown
Contributor Author

uriyyo commented May 5, 2021

please review

Comment thread pydantic/main.py
Copy link
Copy Markdown
Contributor

@PrettyWood PrettyWood left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great! Thank you so much

@PrettyWood PrettyWood merged commit b26d6f9 into pydantic:master Sep 4, 2021
jpribyl pushed a commit to liquet-ai/pydantic that referenced this pull request Oct 7, 2021
* Add create_model overload

* Add changes

* fix: import

Co-authored-by: PrettyWood <em.jolibois@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants