mypy and django models: how to detect errors on nonexistent attributes
问题 Consider this model definition and usage: from django.db import models class User(models.Model): name: str = models.CharField(max_length=100) def do_stuff(user: User) -> None: # accessing existing field print(user.name.strip()) # accessing existing field with a wrong operation: will fail at runtime print(user.name + 1) # acessing nonexistent field: will fail at runtime print(user.name_abc.strip()) While running mypy on this, we will get an error for user.name + 1 : error: Unsupported operand