Skip to content

Commit f529270

Browse files
committed
Add documentation
1 parent 4f79d43 commit f529270

File tree

1 file changed

+107
-0
lines changed

1 file changed

+107
-0
lines changed

docs/source/more_types.rst

+107
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,113 @@ 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+
def g(bool_var: bool) -> None:
679+
if bool_var: # Condition can't be inferred, unable to merge overloads
680+
@overload
681+
def func(var: A) -> A: ...
682+
683+
@overload
684+
def func(var: B) -> B: ...
685+
686+
def func(var: Any) -> Any: ...
687+
688+
reveal_type(func(A())) # Revealed type is "Any"
689+
690+
584691
.. _advanced_self:
585692

586693
Advanced uses of self-types

0 commit comments

Comments
 (0)