Skip to content

Documented @overload failes in non-stub file with --strict #9633

@Peilonrayz

Description

@Peilonrayz

Bug Report

The documentation for typing.overload describes how to use overloads. However when used with --strict the final, untyped, function errors. Support for typing.overload in a non-stub file has been discussed in #72 and #1136, and it looks like the documentation should work fine.

>>> 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

To Reproduce

Here is a playground for easy viewing.

$ mkdir example
$ cd example
$ python -m virtualenv venv
$ . venv/bin/activate.fish
$ pip install mypy
Collecting mypy
  Using cached mypy-0.790-cp38-cp38-manylinux1_x86_64.whl (22.0 MB)
Collecting typed-ast<1.5.0,>=1.4.0
  Using cached typed_ast-1.4.1-cp38-cp38-manylinux1_x86_64.whl (768 kB)
Collecting typing-extensions>=3.7.4
  Using cached typing_extensions-3.7.4.3-py3-none-any.whl (22 kB)
Collecting mypy-extensions<0.5.0,>=0.4.3
  Using cached mypy_extensions-0.4.3-py2.py3-none-any.whl (4.5 kB)
Installing collected packages: typed-ast, typing-extensions, mypy-extensions, mypy
Successfully installed mypy-0.790 mypy-extensions-0.4.3 typed-ast-1.4.1 typing-extensions-3.7.4.3
$ vim example.py
$ mypy example.py
Success: no issues found in 1 source file
$ mypy --strict example.py
example.py:9: error: Function is missing a type annotation
Found 1 error in 1 file (checked 1 source file)

example.py

from typing import overload

@overload
def utf8(value: None) -> None: ...
@overload
def utf8(value: bytes) -> bytes: ...
@overload
def utf8(value: str) -> bytes: ...
def utf8(value):
    pass

Overall

I don't think any harm will come if I silence the error with # type: ignore. However I've been really confused why it's not been working and I don't like using # type: ignore if possible. This has meant that I have opted to not use it in the past.

I fear erroring on correct usage may cause others to also not use overload.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions