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 · 9 comments
Closed

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

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

Comments

@DBCerigo
Copy link

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

@DBCerigo DBCerigo added the question label Nov 13, 2019
@samuelcolvin

This comment has been minimized.

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

This comment has been minimized.

Copy link
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

This comment has been minimized.

Copy link
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

This comment has been minimized.

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

This comment has been minimized.

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

This comment has been minimized.

Copy link
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

This comment has been minimized.

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

This comment has been minimized.

Copy link
Author

@DBCerigo DBCerigo commented Nov 14, 2019

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

@samuelcolvin

This comment has been minimized.

Copy link
Owner

@samuelcolvin samuelcolvin commented Nov 14, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants
You can’t perform that action at this time.