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.

Mypy finds errors in the first example from the documentation

See original GitHub issue
This example
from datetime import datetime
from typing import List
from pydantic import BaseModel

class User(BaseModel):
    id: int
    name = 'John Doe'
    signup_ts: datetime = None
    friends: List[int] = []

external_data = {
    'id': '123',
    'signup_ts': '2019-06-01 12:22',
    'friends': [1, 2, '3']
}
user = User(**external_data)
print(user.id)
print(repr(user.signup_ts))
print(user.friends)
print(user.dict())
$ mypy --strict pydantic_example.py 
pydantic_example.py:3: error: Module 'pydantic' has no attribute 'BaseModel'
pydantic_example.py:5: error: Class cannot subclass 'BaseModel' (has type 'Any')
pydantic_example.py:8: error: Incompatible types in assignment (expression has type "None", variable has type "datetime")

This is not false positive. Pydantic __init__.py uses implicit reexport (this is an error, and I don’t want to ignore it using --no-implicit-reexport), and this example uses None for a non-optional field (this is also an error).

In general, I can’t trust a library that suggests putting None in a non-optional field in the very first example. This violates the mypy type system.

Issue Analytics

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

github_iconTop GitHub Comments

5reactions
dmontagucommented, Dec 4, 2019

This is a problem.

All right, well once your PEP to change mypy errors to syntax errors is accepted, we’ll get right on converting the behavior of every edge case in the use of this library.

4reactions
dmontagucommented, Dec 4, 2019

I agree that the implicit reexport is a nuisance; I think it is worth addressing. @samuelcolvin any ideas?


While mypy doesn’t like the following: signup_ts: datetime = None, there is an awkward inconsistency with mypy that this would be allowed as function arguments, and would imply the argument was optional. Given that it raises a mypy error, if you want to pass mypy, of course you should “correct” it. It is valid pydantic though if you choose not to care.

For what it’s worth, there may be times when you explicitly choose to override the annotations and have mypy assume the type is str, but allow it to be None for pydantic (eg, if you know a validator will always make it non-None). Obviously you would want to add # type: ignore but this doesn’t necessarily need to be a part of the documentation.

That said, I’m also not opposed to changing the docs here, PR welcome.


In general, I can’t trust a library that suggests putting None in a non-optional field in the very first example.

Well, if you can’t trust the library, you are absolutely free to not use it.

This kind of language is not appreciated by the people who put much of their spare time into this project, and are offering it up to you for free. If you care enough to create an issue, please be courteous and try to phrase your misgivings in a less incendiary way.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Mypy finds errors in the first example from the documentation
This example from datetime import datetime from typing import List from pydantic import BaseModel class User(BaseModel): id: int name ...
Read more >
Common issues and solutions - mypy 0.991 documentation
Common issues and solutions#. This section has examples of cases when you need to update your code to use static typing, and ideas...
Read more >
Mypy Documentation - Read the Docs
Mypy is a static type checker for Python. Type checkers help ensure that you're using variables and functions in your code correctly.
Read more >
Linting Python in Visual Studio Code
Linting highlights syntactical and stylistic problems in your Python source code, which often helps you identify and correct subtle programming errors or ...
Read more >
Ubuntu Manpage: mypy - Optional static typing for Python
Mypy is a static type checker for Python 3 and Python 2.7. If you sprinkle your code with type annotations, mypy can type...
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