The Wayback Machine - https://web.archive.org/web/20230615005902/https://github.com/python/mypy/issues/9590
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

Returning Any from function declared to return object #9590

Closed
Dreamsorcerer opened this issue Oct 13, 2020 · 6 comments
Closed

Returning Any from function declared to return object #9590

Dreamsorcerer opened this issue Oct 13, 2020 · 6 comments
Labels
bug mypy got something wrong

Comments

@Dreamsorcerer
Copy link
Contributor

Dreamsorcerer commented Oct 13, 2020

Minimum test case:

[case testNoReturnAny]
# flags: --warn-return-any
import itertools
from typing import Any
things: Any = ()
for a, b in itertools.groupby(things, key=lambda e: e):
    pass
[builtins fixtures/tuple.pyi]

Mypy outputs:
error: Returning Any from function declared to return "object" [no-any-return]

But, the error disappears if I change the code to:

k = lambda e: e
for a, b in itertools.groupby(things, key=k):

Despite there being no more or less typing information here.

@Dreamsorcerer Dreamsorcerer added the bug mypy got something wrong label Oct 13, 2020
@alanhdu
Copy link
Contributor

alanhdu commented Oct 27, 2020

We are also running into this when upgrading from mypy 0.782 to 0.790. It seems that lambdas now have no-any-return applied to them, which can lead to a bunch of errors with --check-untyped-defs.

@hauntsaninja
Copy link
Collaborator

The original issue reproes for me on v0.782 (and older), which suggests you're seeing something slightly different. Regressions are often easier to fix (if it is a regression), so would be useful if you could post a minimal repro of what you're seeing.

@alanhdu
Copy link
Contributor

alanhdu commented Oct 28, 2020

Split off into #9656.

@Dreamsorcerer
Copy link
Contributor Author

I had a little dig through the code, and have found that the behaviour forks in visit_lambda_expr():
https://github.com/python/mypy/blob/master/mypy/checkexpr.py#L3487

When it evaluates the lambda in the second form (i.e. assigned to a variable), inferred_type is None, but when used inline it appears as def (<Erased>) -> <Erased>. The warning is then only triggered in the else block, so does not occur in the second form.

@AMDmi3
Copy link

AMDmi3 commented Jan 13, 2022

Another repro case:

from typing import Any, Callable, TypeVar

T = TypeVar('T')

def producer() -> Any:
    return 1

def call(callee: Callable[[], T]) -> T:
    return callee()

call(lambda: producer())
% mypy --strict 1.py
1.py:11: error: Returning Any from function declared to return "object"

Note that this only seems to fire with Any. If I change return type of producer to int the error disappears.

youtux added a commit to pytest-dev/pytest-factoryboy that referenced this issue May 27, 2022
Also add a type ignore because of this mypy bug:
python/mypy#9590
@ilevkivskyi
Copy link
Member

FWIW this works on current master (due to special-casing of object for this error).

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

5 participants