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

mypy type checking doesn't seem to work #366

Closed
gjcarneiro opened this issue Jan 21, 2019 · 4 comments · Fixed by #373
Closed

mypy type checking doesn't seem to work #366

gjcarneiro opened this issue Jan 21, 2019 · 4 comments · Fixed by #373
Labels
bug V1 Bug related to Pydantic V1.X

Comments

@gjcarneiro
Copy link

Bug

For bugs/questions:

  • OS: ubuntu 18.10
  • Python version import sys; print(sys.version): 3.7
  • Pydantic version import pydantic; print(pydantic.VERSION): master

Where possible please include a self contained code snippet describing your
bug, question, or where applicable feature request:

from datetime import datetime
from typing import List, Optional
from pydantic import BaseModel, NoneStr

class Model(BaseModel):
    age: int
    first_name = 'John'
    last_name: NoneStr = None
    signup_ts: Optional[datetime] = None
    list_of_ints: List[int]

m = Model(age=42, list_of_ints=[1, '2', b'3'])
print(m.age)
# > 42

Model()
# will raise a validation error for age and list_of_ints

The above is the example from the docs: https://pydantic-docs.helpmanual.io/#usage-with-mypy

With mypy 0.650, doesn't raise any validation error.

Moreover, changing print(m.age) to print(m.agexxx), it still doesn't give any error saying that the field name is wrong.

@samuelcolvin
Copy link
Member

We should make the docs clearer, but while mypy won't give a false negative when it parses a pydantic model, it obviously can't check if model validation is going to pass or fail.

Also as per #245 mypy can't check whether an attribute exists.

This is a (completely understandable) limitation of mypy. You might want to use dataclasses which mypy should support better.

@gjcarneiro
Copy link
Author

Yes, please make the docs clearer. Essentially I can't use pydantic if I want static type checking. The docs say:

because pydantic data structures are just instances of classes you define; auto-completion, linting, mypy and your intuition should all work properly with your validated data.

Well, I would claim that, no, mypy does not work correctly with pydantic. The docs are misleading.

@samuelcolvin
Copy link
Member

That's fair, I'll update docs.

Some things do work however

from pydantic import BaseModel

class Test(BaseModel):
    test_int: int
    test_str: str

m = Test(test_int=42, test_str='foobar')
print(123 + m.test_str)

Returns:

(env) pydantic 0 1.19s ➤  mypy --ignore-missing-imports --follow-imports=skip --strict-optional test.py 
test.py:11: error: Unsupported operand types for + ("int" and "str")

While the same with print(123 + m.test_int) would pass.

So I would say mypy and pydantic work together partially, "Essentially I can't use pydantic if I want static type checking." is a bit too far the other way.

@samuelcolvin
Copy link
Member

see #372.

@samuelcolvin samuelcolvin mentioned this issue Jan 26, 2019
4 tasks
alexdrydew pushed a commit to alexdrydew/pydantic that referenced this issue Dec 23, 2023
* Test on other OSes, remove benchmarks

* fix pytest-speed for 3.7

* remove windows xfail, revert pyproject changes

* uprev pytest, pytest-pretty

* add print to windows

* more eprints

* remove test-os py cache

* remove extra prints

* revert xfail

* remove pytest-speed on codspeed ci

* install ptyest-benchmark with codspeed

* clear codspeed cache
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug V1 Bug related to Pydantic V1.X
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants