Open
Description
Bug report
We are implementing pre-save of some models and there usually we check if the instance is being created or not by doing
if instance.id is None:
... # creating the instance logic
What's wrong
import typing as t
from django.db import models as django_models
from django import dispatch as django_dispatch
@django_dispatch.receiver(django_models.signals.pre_save, sender=my_app.MyModel)
def project_pre_save(sender: type[my_app.MyModel], instance: my_app.MyModel, **kwargs: t.Any) -> None:
if instance.id is None:
add_something_to_instance(instance) # <-- Statement is unreachable [unreachable]
...
In this case mypy flagging this with the following error indicated above
How is that should be
On pre-save instance.id
should be either the Field or None as it can be None in case of creation of the object.
Do not know how to properly implement it though...
System information
- OS: MacOS / Linux
python
version: 3.11.5django
version: 3.2.23mypy
version: 1.9.0django-stubs
version: 4.2.7django-stubs-ext
version: 4.2.7