question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Current best practices for combining `pydantic` & `mypy`

See original GitHub issue

Part 1: there seems to be an inconsistency between the conclusion from this issue #245 being “Solution here is to use dataclasses which I think mypy should work with properly.”, and then this pydantic docs page https://pydantic-docs.helpmanual.io/usage/mypy/ Is the conclusion of that issue now defunct, and inheriting from BaseModel is supposed to work correctly with mypy, or are the docs incorrect? I raise this as in our current usage of BaseModel we get the (“mypy not working”) behaviour from #245 and not the (“mypy working”) behaviour from docs.

Part 2: given we really like pydantic (a thanks and congrats to all contributors to it!), what is currently advised by core pydantic devs as the best practice to use pydantic while still get type-checking with mypy. The options I found are: a. Use pydantic.dataclasses as per https://pydantic-docs.helpmanual.io/usage/dataclasses/#nested-dataclasses b. Use the pydantic.mypy plugin* as per https://pydantic-docs.helpmanual.io/mypy_plugin/ The hope with this question is that it can provide advice to others out there looking to combine mypy & pydantic on how best to do so currently 😸

Other refs for mypy + pydantic usage: #366 #957 #156 #238

*Note, in this same docs page it states that mypy works with pydantic (assuming implying with BaseModel also) out of the box, which is again not what #245 concludes 😬

(I personally think mypy is pretty 👎, so I appreciate the presumed headache it causes you all too, but that optional type-hinting in general is very 👍)

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:14 (8 by maintainers)

github_iconTop GitHub Comments

25reactions
Reskovcommented, Feb 9, 2021

I was able to solve issue using additional_dependencies


- repo: https://github.com/pre-commit/mirrors-mypy
  rev: v0.800
  hooks:
  - id: mypy
    additional_dependencies:
      - 'pydantic'
4reactions
Ben2146053commented, Jan 4, 2021

If your using the mypy plugin on VS Code you could be getting the error error: Error importing plugin 'pydantic.mypy' what you need to do is go into the plugins virtual machine and pip install pydantic because its not installed yet So assumed you have installed the vs code plugin already, navigate to your terminal

%USERPROFILE%\.mypyls\Scripts\activate pip install pydantic

Read more comments on GitHub >

github_iconTop Results From Across the Web

Current best practices for combining pydantic & mypy #995
If you never import pydantic.mypy (and it should only be imported when mypy is running using the plugin), you don't need to have...
Read more >
Usage with mypy - pydantic
Usage with mypy. pydantic models work with mypy provided you use the annotation-only version of required fields: Python 3.7 and above Python 3.9...
Read more >
Conflict between pydantic constr and mypy checking
There are a few ways to achieve this: Inheriting from pydantic.ConstrainedStr. Instead of using constr to specify the regex constraint ...
Read more >
Pydantic types - The Blue Book
Mypy accepts this happily and pydantic does correct validation. The type of data.regex[i] is Regex , but as pydantic.ConstrainedStr itself inherits from str...
Read more >
Python Best Practices for a New Project in 2021 - Alex Mitelman
I think, with Mypy we can. Mypy is a static code analysis tool that makes sure that the code is type safe. The...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found