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 ๐)
The solution is to use the mypy plugin with that mypy should work correctly.
yes, #245 is outdated.
@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'!
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 :)
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)
Can confirm, installing
pydantic=1.1didn't force an upgrade inmypyversion. I was getting theerror: Error importing plugin 'pydantic.mypy'withmypy=0.720. I upgraded tomypy=0.740, and now the error is resolved :)
mypy is not a dependency of pydantic, thus installing pydantic can't force mypy to update.
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.
@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.
@dmontagu appreciated, sounds completely right :) thanks for responding
No, mypy is definitely not a dependency of pydantic because you can use
pydantic without mypy.
In theory mypy could become an optional dependency on pydantic, but without
reading the docs that wouldn't have helped you.
We could however add a note to the docs pointing out the minimum version of
mypy required to use plugins.
On Thu, Nov 14, 2019, 08:41 Daniel Burkhardt Cerigo <
[email protected]> wrote:
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.โ
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/samuelcolvin/pydantic/issues/995?email_source=notifications&email_token=AA62GGI6LSQCE6UXGKQCGBDQTUFLDA5CNFSM4JM7ZONKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEEBBIMY#issuecomment-553784371,
or unsubscribe
https://github.com/notifications/unsubscribe-auth/AA62GGMMWLIFSL6HHRXK36TQTUFLDANCNFSM4JM7ZONA
.
I am still having this issue with mypy 0.782 and pydantic 1.6.1
Also having this issue with mypy 0.782 and pydantic 1.6.1
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
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?