I have an existing Django project which uses a custom User
model class that extends AbstractUser
. For various important reasons, we need to redefine the email
field as follows:
class User(AbstractUser):
...
email = models.EmailField(db_index=True, blank=True, null=True, unique=True)
...
Typing checks via mypy have been recently added. However, when I perform the mypy check, I get the following error:
error: Incompatible types in assignment (expression has type "EmailField[str | int | Combinable | None, str | None]", base class "AbstractUser" defined the type as "EmailField[str | int | Combinable, str]") [assignment]
How can I make it so that mypy allows this type reassignment? I don't wish to just use # type: ignore
because I wish to use its type protections.
For context, if I do use # type: ignore
, then I get dozens of instances of the following mypy error instead from all over my codebase:
error: Cannot determine type of "email" [has-type]
Here are details of my setup:
python version: 3.10.5
django version: 3.2.19
mypy version: 1.6.1
django-stubs[compatible-mypy] version: 4.2.6
django-stubs-ext version: 4.2.5
typing-extensions version: 4.8.0
One option is to change the super class from
AbstractUser
toAbstractBaseUser
. This would allow to to more easily over-write the specific properties you want to change and give you more flexibility in the long run, at the cost of some extra boilerplate.Some extra info: https://testdriven.io/blog/django-custom-user-model/