Skip to content

disallow-untyped-defs wants overloaded functions to be annotated #9952

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

Closed
MarkCBell opened this issue Jan 24, 2021 · 3 comments
Closed

disallow-untyped-defs wants overloaded functions to be annotated #9952

MarkCBell opened this issue Jan 24, 2021 · 3 comments
Labels
bug mypy got something wrong

Comments

@MarkCBell
Copy link
Contributor

MarkCBell commented Jan 24, 2021

Bug Report

Setting the disallow-untyped-defs option is set (either through command line flags or mypy.ini) mypy raises errors about requires that an overloaded function's implementation def must also be explicitly annotated. However the documentation / examples for the overload operator in PEP 484 say not to add annotations to this def since its annotation is specified by the overloaded defs (which mypy correctly deduces).

To Reproduce

Run mypy scratch.py --disallow-untyped-defs when scratch.py contains:

from typing import overload

@overload
def foo(value: int) -> int:
    pass

@overload
def foo(value: str) -> str:
    pass

def foo(value):
    pass
# Although this function is unannotated reveal_type(foo) returns:
# note: Revealed type is 'Overload(def (value: builtins.int) -> builtins.int, def (value: builtins.str) -> builtins.str)'
# so mypy is correctly determining the type of foo and so there is no need to annotate it.

Expected Behavior

mypy should reports no errors

Actual Behavior

mypy reports that error: Function is missing a type annotation

Your Environment

  • Mypy version used: 0.800
  • Mypy command-line flags: --disallow-untyped-defs
  • Mypy configuration options from mypy.ini (and other config files):
  • Python version used: 3.8.5
  • Operating system and version: Ubuntu 20.04
@MarkCBell MarkCBell added the bug mypy got something wrong label Jan 24, 2021
@gvanrossum
Copy link
Member

Mypy 0.600 is ancient. Maybe a typo for 0.800?

Annotation the implementation is useful so it’s body can be type-checked.

@hauntsaninja
Copy link
Collaborator

hauntsaninja commented Jan 24, 2021

See #3360

@MarkCBell
Copy link
Contributor Author

Ok, that makes sense and thank you for the link to the other issue. In which case I will submit an issue for updating the examples in the Python overload documentation (for example https://docs.python.org/3/library/typing.html#typing.overload) to reflect the convention of annotating the implementation to allow its body to be type-checked. I think in that example it should be:

@overload
def process(response: None) -> None:
    ...
@overload
def process(response: int) -> tuple[int, str]:
    ...
@overload
def process(response: bytes) -> str:
    ...
def process(response: typing.Union[None, int, bytes]) -> typing.Union[None, tuple[int, str], str]:
    <actual implementation>

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

3 participants