| 1 |
Type Hints |
| 2 |
========== |
| 3 |
|
| 4 |
.. versionadded:: 4.11 |
| 5 |
|
| 6 |
:pypi:`django-polymorphic` is now fully typed, and ships with type hints for all public |
| 7 |
APIs. Typing is checked with :pypi:`mypy` and :pypi:`pyright` in the CI pipeline. |
| 8 |
|
| 9 |
The utility and power of the Django ORM derives from its dynamism but this makes static typing |
| 10 |
more difficult. There are *no additional runtime dependencies* but **to use the packaged type hint |
| 11 |
classes and descriptors, you must install the following in your type checking context**: |
| 12 |
|
| 13 |
* :pypi:`django-stubs` (required) |
| 14 |
* :pypi:`django-stubs-ext` (optional) |
| 15 |
|
| 16 |
.. tip:: |
| 17 |
|
| 18 |
The most useful type hints to add to your own code will make it so your type checking system |
| 19 |
knows which model types your :class:`~polymorphic.managers.PolymorphicManager` and |
| 20 |
:class:`~polymorphic.managers.PolymorphicQuerySet` objects might return. |
| 21 |
|
| 22 |
Correct type hints for polymorphic managers and querysets cannot be automatically inferred - you |
| 23 |
will have to add them explicitly if you want them: |
| 24 |
|
| 25 |
.. _typing_managers: |
| 26 |
|
| 27 |
Managers |
| 28 |
-------- |
| 29 |
|
| 30 |
You can type your managers like this. It might not always be the case that you can add hints for all |
| 31 |
child model types, especially if they are included in dependent apps. You can alleviate some of this |
| 32 |
complexity with forward type references but strict typing may not always be appropriate. |
| 33 |
|
| 34 |
.. literalinclude:: ../src/polymorphic/tests/examples/type_hints/managers/models.py |
| 35 |
:language: python |
| 36 |
:linenos: |
| 37 |
|
| 38 |
|
| 39 |
.. code-block:: python |
| 40 |
|
| 41 |
ParentModel.objects.all() # type: PolymorphicQuerySet[ParentModel | Child1 | Child2] |
| 42 |
ParentModel.objects.instance_of(Child1) # type: PolymorphicQuerySet[Child1] |
| 43 |
Child1.objects.non_polymorphic() # type: QuerySet[Child1] |
| 44 |
|
| 45 |
.. _typing_foreign_key: |
| 46 |
|
| 47 |
Foreign Key |
| 48 |
----------- |
| 49 |
|
| 50 |
:pypi:`django-polymorphic` includes several :ref:`type hint descriptors <type_hint_descriptors>`. |
| 51 |
You can use them to type your forward and reverse relationship fields. For foreign key relationships |
| 52 |
we provide :class:`~polymorphic.managers.PolymorphicForwardManyToOneDescriptor` and |
| 53 |
:class:`~polymorphic.managers.PolymorphicReverseManyToOneDescriptor`: |
| 54 |
|
| 55 |
.. literalinclude:: ../src/polymorphic/tests/examples/type_hints/fk/models.py |
| 56 |
:language: python |
| 57 |
:linenos: |
| 58 |
|
| 59 |
.. code-block:: python |
| 60 |
|
| 61 |
RelatedModel().parent: Optional[ParentModel | Child1 | Child2] |
| 62 |
RelatedModel().children.all(): PolymorphicQuerySet[ParentModel | Child1 | Child2] |
| 63 |
|
| 64 |
.. _typing_one_to_one: |
| 65 |
|
| 66 |
One to One |
| 67 |
---------- |
| 68 |
For foreign key relationships |
| 69 |
we provide :class:`~polymorphic.managers.PolymorphicForwardOneToOneDescriptor` and |
| 70 |
:class:`~polymorphic.managers.PolymorphicReverseOneToOneDescriptor`: |
| 71 |
|
| 72 |
.. literalinclude:: ../src/polymorphic/tests/examples/type_hints/one2one/models.py |
| 73 |
:language: python |
| 74 |
:linenos: |
| 75 |
|
| 76 |
.. code-block:: python |
| 77 |
|
| 78 |
RelatedModel().parent_forward: ParentModel | Child1 | Child2 | None |
| 79 |
RelatedModel().parent_reverse: ParentModel | Child1 | Child2 |
| 80 |
|
| 81 |
.. _typing_many_to_many: |
| 82 |
|
| 83 |
Many to Many |
| 84 |
------------ |
| 85 |
|
| 86 |
You can use the same :class:`~polymorphic.managers.PolymorphicManyToManyDescriptor` for |
| 87 |
both forward and reverse :class:`~django.db.models.ManyToManyField` relationships. |
| 88 |
|
| 89 |
The following example shows two :class:`~django.db.models.ManyToManyField` relationships: |
| 90 |
|
| 91 |
1. :class:`~polymorphic.models.PolymorphicModel` -> :class:`~django.db.models.Model` |
| 92 |
(with a custom through model) |
| 93 |
2. :class:`~django.db.models.Model` -> :class:`~polymorphic.models.PolymorphicModel` |
| 94 |
(with the default through model) |
| 95 |
|
| 96 |
For the custom through model you will need to annotate using the :ref:`foreign key descriptors |
| 97 |
<typing_foreign_key>` as well. |
| 98 |
|
| 99 |
.. literalinclude:: ../src/polymorphic/tests/examples/type_hints/m2m/models.py |
| 100 |
:language: python |
| 101 |
:linenos: |