Skip to content

Overloaded functions fail when checked with --disallow-untyped-defs #3360

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
tyehle opened this issue May 12, 2017 · 3 comments
Closed

Overloaded functions fail when checked with --disallow-untyped-defs #3360

tyehle opened this issue May 12, 2017 · 3 comments

Comments

@tyehle
Copy link
Contributor

tyehle commented May 12, 2017

For this minimal example

@overload
def test(val: int) -> int: ...
@overload
def test(val: str) -> str: ...
def test(val):
    return val

with --disallow-untyped-defs mypy fails with error: Function is missing a type annotation
but without it it succeeds.

@ilevkivskyi
Copy link
Member

This is actually expected, although somewhat unintuitive. The reason is following: currently mypy does not check the body of the function against all overloads (this is very difficult in a general case involving *args, **kwargs), overloads only provide the type/API for "external users" of a function. At the same time, mypy checks the body of a function against an (optional) type signature given in the last definition. The --disallow-untyped-defs flag prohibits unchecked function bodies, so that it errors in your case. You can fix this by writing something like this:

@overload
def test(val: int) -> int: ...
@overload
def test(val: str) -> str: ...
def test(val: Union[int, str]) -> Union[int, str]:
    return val

I just checked the documentation, and I think this is not well documented. Would you like to make a PR?

As a side comment, it is not necessary to use overloads for such simple signatures, you can just write:

T = TypeVar('T', int, str)
def test(val: T) -> T:
    return val

@tyehle
Copy link
Contributor Author

tyehle commented May 12, 2017

Yeah, that was just the smallest example I could get to fail.
I'll work on a PR.

@gvanrossum
Copy link
Member

Fixed by #3369. (If you use the exact words "Fixes #3369" in a commit message it will auto-close the issue when the PR lands.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants