Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
Peilonrayz opened this issue Oct 24, 2020 · 4 comments
Closed

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

Peilonrayz opened this issue Oct 24, 2020 · 4 comments
Labels
bug mypy got something wrong

Comments

@Peilonrayz
Copy link
Contributor

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.

@Peilonrayz Peilonrayz added the bug mypy got something wrong label Oct 24, 2020
@JelleZijlstra
Copy link
Member

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.

@Peilonrayz
Copy link
Contributor Author

Mypy also errors when you annotate the implementation.

from typing import overload

@overload
def utf8(value: None) -> None: ...
@overload
def utf8(value: bytes) -> bytes: ...
@overload
def utf8(value: str) -> bytes:
    pass
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)

@JelleZijlstra
Copy link
Member

JelleZijlstra commented Oct 25, 2020

That example has no implementation (as the error message says). It may be better to ask for help at https://gitter.im/python/typing.

@Peilonrayz
Copy link
Contributor Author

Peilonrayz commented Oct 25, 2020

Even if you provide an implementation you get the same error message - playground.

from typing import overload

@overload
def utf8(value: None) -> None: ...
@overload
def utf8(value: bytes) -> bytes: ...
@overload
def utf8(value: str) -> bytes:
    if value is None:
        return None
    return b''
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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

No branches or pull requests

2 participants