Skip to content
Advertisement

Differentiate class and object in Python type hints

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:

JavaScript

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.

JavaScript

This answer was posted for the clarity and readability thanks to a comment by @Michael0x2a.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement