Skip to content

Commit 4776070

Browse files
committed
Add documentation
1 parent 4f79d43 commit 4776070

File tree

1 file changed

+108
-0
lines changed

1 file changed

+108
-0
lines changed

docs/source/more_types.rst

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,114 @@ with ``Union[int, slice]`` and ``Union[T, Sequence]``.
581581
to returning ``Any`` only if the input arguments also contain ``Any``.
582582

583583

584+
Conditional overloads
585+
---------------------
586+
587+
Sometimes it's useful to define overloads conditionally.
588+
Common use cases are types which aren't available at runtime or only in
589+
a certain Python version. All existing overload rules still apply.
590+
E.g. if overloads are defined, at least 2 are required.
591+
592+
.. note::
593+
594+
Mypy can only infer a limited number of conditions.
595+
Supported ones currently include :py:data:`~typing.TYPE_CHECKING`, ``MYPY``,
596+
:ref:`version_and_platform_checks`, and :option:`--always-true <mypy --always-true>` / :option:`--always-false <mypy --always-false>` values.
597+
It's thus recommended to keep these conditions as simple as possible.
598+
599+
.. code-block:: python
600+
601+
from typing import TYPE_CHECKING, Any, overload
602+
603+
if TYPE_CHECKING:
604+
class A: ...
605+
class B: ...
606+
607+
608+
if TYPE_CHECKING:
609+
@overload
610+
def func(var: A) -> A: ...
611+
612+
@overload
613+
def func(var: B) -> B: ...
614+
615+
def func(var: Any) -> Any:
616+
return var
617+
618+
619+
reveal_type(func(A())) # Revealed type is "A"
620+
621+
.. code-block:: python
622+
623+
# flags: --python-version 3.10
624+
import sys
625+
from typing import Any, overload
626+
627+
class A: ...
628+
class B: ...
629+
class C: ...
630+
class D: ...
631+
632+
633+
if sys.version_info < (3, 7):
634+
@overload
635+
def func(var: A) -> A: ...
636+
637+
elif sys.version_info >= (3, 10):
638+
@overload
639+
def func(var: B) -> B: ...
640+
641+
else:
642+
@overload
643+
def func(var: C) -> C: ...
644+
645+
@overload
646+
def func(var: D) -> D: ...
647+
648+
def func(var: Any) -> Any:
649+
return var
650+
651+
652+
reveal_type(func(B())) # Revealed type is "B"
653+
reveal_type(func(C())) # No overload variant of "func" matches argument type "C"
654+
# Possible overload variants:
655+
# def func(var: B) -> B
656+
# def func(var: D) -> D
657+
# Revealed type is "Any"
658+
659+
660+
.. note::
661+
662+
In the last example Mypy is executed with
663+
:option:`--python-version 3.10 <mypy --python-version>`.
664+
Because of that the condition ``sys.version_info >= (3, 10)`` will match and
665+
the overload for ``B`` will be added.
666+
The overloads for ``A`` and ``C`` are ignored!
667+
The overload for ``D`` isn't defined conditionally and thus also added.
668+
669+
In case Mypy can't infer a condition to be always True or always False, an error will be emitted.
670+
671+
.. code-block:: python
672+
673+
from typing import Any, overload
674+
675+
class A: ...
676+
class B: ...
677+
678+
679+
def g(bool_var: bool) -> None:
680+
if bool_var: # Condition can't be inferred, unable to merge overloads
681+
@overload
682+
def func(var: A) -> A: ...
683+
684+
@overload
685+
def func(var: B) -> B: ...
686+
687+
def func(var: Any) -> Any: ...
688+
689+
reveal_type(func(A())) # Revealed type is "Any"
690+
691+
584692
.. _advanced_self:
585693

586694
Advanced uses of self-types

0 commit comments

Comments
 (0)