Django’s post_save signal sends a model class argument – sender – along with the actual instance being saved – instance.
Is there a way to differentiate between the two in type hints?
Example
We have a model User and would like to create a post_save signal:
# …
@receiver([post_save], sender=User)
def send_activation_email(
    sender: User, 
    instance: User, 
    # …
) -> None:
    # …
As you can see, I have given both sender and instance the same type hint – User. But they are not the same type. The first is a class, and the second is an object. So, is there a way to differentiate the two?
Advertisement
Answer
What you are looking for is typing.Type.
from typing import Type
# …
@receiver([post_save], sender=User)
def send_activation_email(
    sender: Type[User], 
    instance: User, 
    # …
) -> None:
    # …
This answer was posted for the clarity and readability thanks to a comment by @Michael0x2a.