[New-bugs-announce] [issue42169] Apparently all documentation on @typing.overload is wrong

Peilonrayz report at bugs.python.org
Tue Oct 27 06:59:52 EDT 2020


New submission from Peilonrayz <peilonrayz at gmail.com>:

The documentation for `typing.overload` says in a non-stub file the last definition shouldn't be typed. However running that through `mypy --strict` fails. I opened an issue on mypy a couple of days ago, however was told to report this on CPython.

```
>>> import typing
>>> help(typing.overload)
Help on function overload in module typing:

overload(func)
    Decorator for overloaded functions/methods.
    
    In a stub file, place two or more stub definitions for the same
    function in a row, each decorated with @overload.  For example:
    
      @overload
      def utf8(value: None) -> None: ...
      @overload
      def utf8(value: bytes) -> bytes: ...
      @overload
      def utf8(value: str) -> bytes: ...
    
    In a non-stub file (i.e. a regular .py file), do the same but
    follow it with an implementation.  The implementation should *not*
    be decorated with @overload.  For example:
    
      @overload
      def utf8(value: None) -> None: ...
      @overload
      def utf8(value: bytes) -> bytes: ...
      @overload
      def utf8(value: str) -> bytes: ...
      def utf8(value):
          # implementation goes here
```

The typing docs and PEP 484 say similar things.  
typing docs - https://docs.python.org/3/library/typing.html#typing.overload  
PEP 484 - https://www.python.org/dev/peps/pep-0484/#function-method-overloading

Jelle Zijlstra told me to report this here. https://github.com/python/mypy/issues/9633#issuecomment-716201251

> You should annotate the implementation. The example in the typing docs should perhaps also add an annotation, but that's an issue for the CPython repo, not for mypy.

Either way mypy errors which can be seen in the following playgrounds.

docs - https://mypy-play.net/?mypy=latest&python=3.9&flags=strict&gist=cffb94a2de9d5d55142da5e7d960102f
```
main.py:9: error: Function is missing a type annotation
Found 1 error in 1 file (checked 1 source file)
```

proper way? - https://mypy-play.net/?mypy=latest&python=3.9&gist=bfadffe92571b4faad04ea151b2b1c54
```
main.py:3: error: An overloaded function outside a stub file must have an implementation
Found 1 error in 1 file (checked 1 source file)
```

Is all the documentation on `typing.overload` wrong - should the implementation be annotated?

----------
assignee: docs at python
components: Documentation
messages: 379754
nosy: docs at python, peilonrayz
priority: normal
severity: normal
status: open
title: Apparently all documentation on @typing.overload is wrong
versions: Python 3.10, Python 3.6, Python 3.7, Python 3.8, Python 3.9

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue42169>
_______________________________________


More information about the New-bugs-announce mailing list