Skip to content

@overload declarations in stubs - required to be directly adjacent? #10120

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

Open
michi42 opened this issue Feb 20, 2021 · 0 comments
Open

@overload declarations in stubs - required to be directly adjacent? #10120

michi42 opened this issue Feb 20, 2021 · 0 comments
Labels
bug mypy got something wrong topic-overloads

Comments

@michi42
Copy link

michi42 commented Feb 20, 2021

Bug Report

Currently, mypy requires @overload declarations in stubs to be directly adjacent, with no declarations (e.g. type vars) in between. However, I don't find that requirement in PEP 484 [1], nor in the typing module documentation [2].

[1] https://www.python.org/dev/peps/pep-0484/#function-method-overloading
[2] https://docs.python.org/3/library/typing.html#typing.overload

To Reproduce
Consider the following type stub:

import typing

@typing.overload
def foo() -> None: ...

K = typing.TypeVar('K')
@typing.overload
def foo(k: K) -> K: ...

Then run stubtest on it.

Expected Behavior
According to the documentation, I would expect this stub to be valid.

Actual Behavior

$ python -m mypy.stubtest teststub
error: failed mypy build.
teststub.pyi:4: error: Single overload definition, multiple required
teststub.pyi:8: error: Name 'foo' already defined on line 4
teststub.pyi:8: error: Single overload definition, multiple required

Pulling the type var declaration up fixes it:

import typing

K = typing.TypeVar('K')

@typing.overload
def foo() -> None: ...

@typing.overload
def foo(k: K) -> K: ...

Your Environment
Python 3.7.1 (anaconda) on Linux, latest master of mypy.
I also tested with mypy 0.790 on Windows, with the same result.

$ python --version
Python 3.7.1
$ python -m mypy --version
mypy 0.820+dev.497556f466dcda90d850b23e86c55ec4082be3f5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-overloads
Projects
None yet
Development

No branches or pull requests

2 participants