Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Current best practices for combining pydantic & mypy #995

Closed
DBCerigo opened this issue Nov 13, 2019 · 14 comments
Closed

Current best practices for combining pydantic & mypy #995

DBCerigo opened this issue Nov 13, 2019 · 14 comments
Labels

Comments

@DBCerigo
Copy link
Contributor

@DBCerigo DBCerigo commented Nov 13, 2019

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 👍)

@samuelcolvin
Copy link
Owner

@samuelcolvin samuelcolvin commented Nov 13, 2019

The solution is to use the mypy plugin with that mypy should work correctly.

yes, #245 is outdated.

@DBCerigo
Copy link
Contributor Author

@DBCerigo DBCerigo commented Nov 13, 2019

@samuelcolvin having just installed pydantic=1.1, with a setup.cfg as:

[mypy]
plugins = pydantic.mypy

I'm getting error: Error importing plugin 'pydantic.mypy'!

@DBCerigo
Copy link
Contributor Author

@DBCerigo DBCerigo commented Nov 13, 2019

Can confirm, installing pydantic=1.1 didn't force an upgrade in mypy version. I was getting the error: Error importing plugin 'pydantic.mypy' with mypy=0.720. I upgraded to mypy=0.740, and now the error is resolved :)

@samuelcolvin
Copy link
Owner

@samuelcolvin samuelcolvin commented Nov 13, 2019

I've just tried this myself and it works fine:

> mkdir scratch
> cd scratch
> touch app.py
[copy python https://pydantic-docs.helpmanual.io/mypy_plugin/]
> touch setup.cfg
[copy the two lines from above]
> pip install -U pydantic mypy
...
> mypy app.py 
app.py:16: error: "Model" has no attribute "middle_name"
app.py:17: error: Missing named argument "age" for "Model"
app.py:17: error: Missing named argument "list_of_ints" for "Model"
Found 3 errors in 1 file (checked 1 source file)

@samuelcolvin
Copy link
Owner

@samuelcolvin samuelcolvin commented Nov 13, 2019

Can confirm, installing pydantic=1.1 didn't force an upgrade in mypy version. I was getting the error: Error importing plugin 'pydantic.mypy' with mypy=0.720. I upgraded to mypy=0.740, and now the error is resolved :)

mypy is not a dependency of pydantic, thus installing pydantic can't force mypy to update.

@DBCerigo
Copy link
Contributor Author

@DBCerigo DBCerigo commented Nov 14, 2019

I guess with the pydantic-mypy plugin introduced in 1.1, mypy is now a dep of pydantic? 🤷‍♂️ It's not biggy to me :) just hopefully to try to help make improvements.

@dmontagu
Copy link
Collaborator

@dmontagu dmontagu commented Nov 14, 2019

@DBCerigo As far as I understand, nothing has changed about pydantic's relationship with mypy. It was a development-only dependency before, and it still is.

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 mypy installed.

If you want to use the mypy plugin while type-checking your code using mypy, then yes, you'd need to have mypy installed 😄. But that was also the case before.

@DBCerigo
Copy link
Contributor Author

@DBCerigo DBCerigo commented Nov 14, 2019

@dmontagu appreciated, sounds completely right :) thanks for responding

@samuelcolvin
Copy link
Owner

@samuelcolvin samuelcolvin commented Nov 14, 2019

@ferndot
Copy link

@ferndot ferndot commented Aug 25, 2020

I am still having this issue with mypy 0.782 and pydantic 1.6.1

@AlexKLWS
Copy link

@AlexKLWS AlexKLWS commented Sep 6, 2020

Also having this issue with mypy 0.782 and pydantic 1.6.1

@Ben2146053
Copy link

@Ben2146053 Ben2146053 commented 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

@AIGeneratedUsername
Copy link

@AIGeneratedUsername AIGeneratedUsername commented Jan 6, 2021

Mypy works from command line, but fails with pre-commit:

mypy.....................................................................Failed
 - hook id: mypy - exit code: 2 setup.cfg:23:1: error: Error importing plugin 'pydantic.mypy': No module named 'pydantic' [misc] 

Any ideas how to fix?

@Reskov
Copy link

@Reskov Reskov commented 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'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

8 participants