Skip to content

Commit

Permalink
more detail
Browse files Browse the repository at this point in the history
  • Loading branch information
hauntsaninja committed Sep 9, 2023
1 parent 68c6e30 commit 83eaf66
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
11 changes: 10 additions & 1 deletion docs/source/error_code_list.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1137,7 +1137,16 @@ Warn if multiple ``@overload`` variants overlap in unsafe ways.
def takes_a(a: A) -> str:
return foo(a)
assert isinstance(takes_a(B()), str) # may fail when run
a: A = B()
value = takes_a(a)
# mypy will think that value is a str, but it could actually be an int
reveal_type(value) # Revealed type is "builtins.str"
Note that in cases you ignore this error, mypy will usually still infer the
types you expect.

See :ref:`overloading <function-overloading> for more explanation.

.. _code-annotation-unchecked:

Expand Down
5 changes: 4 additions & 1 deletion docs/source/more_types.rst
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,7 @@ To prevent these kinds of issues, mypy will detect and prohibit inherently unsaf
overlapping overloads on a best-effort basis. Two variants are considered unsafely
overlapping when both of the following are true:

1. All of the arguments of the first variant are compatible with the second.
1. All of the arguments of the first variant are potentially compatible with the second.
2. The return type of the first variant is *not* compatible with (e.g. is not a
subtype of) the second.

Expand All @@ -510,6 +510,9 @@ the ``object`` argument in the second, yet the ``int`` return type is not a subt
``str``. Both conditions are true, so mypy will correctly flag ``unsafe_func`` as
being unsafe.

Note that in cases you ignore the overlapping overload error, mypy will usually
still infer the types you expect at callsites.

However, mypy will not detect *all* unsafe uses of overloads. For example,
suppose we modify the above snippet so it calls ``summarize`` instead of
``unsafe_func``:
Expand Down

0 comments on commit 83eaf66

Please sign in to comment.