Skip to content

Lambda with exponentiation return any warning #6210

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
alanhdu opened this issue Jan 17, 2019 · 3 comments
Closed

Lambda with exponentiation return any warning #6210

alanhdu opened this issue Jan 17, 2019 · 3 comments

Comments

@alanhdu
Copy link
Contributor

alanhdu commented Jan 17, 2019

Using mypy 0.660 and Python 3.7, running mypy --warn-return-any on:

output = filter(lambda x: 2**x < 6, [1, 2, 3, 4])

returns

lambda.py:1: warning: Returning Any from function declared to return "object"

This only occurs when there's the exponentiation (**) in the lambda: if I change it to a + or *.

@alanhdu
Copy link
Contributor Author

alanhdu commented Jan 17, 2019

Looking at typeshed, it looks like the line responsible for the Any is:

https://github.com/python/typeshed/blob/6f09ccb63dfce0924e34fa04644ea77b63747310/stdlib/2and3/builtins.pyi#L170-L171

    def __pow__(self, x: int) -> Any: ...  # Return type can be int or float, depending on x.
    def __rpow__(self, x: int) -> Any: ...

But I'm not sure where "return object" comes into play. Should that Any just be a float?

@JukkaL
Copy link
Collaborator

JukkaL commented Jan 17, 2019

float return type would be technically true, but that would cause a lot of issues with code that (correctly) wants to assume that x ** y is an integer. The convention in typeshed is to use Any as the return type if the return type depends on the value (not the type) of an argument. If you want a precise type, you can create a helper function (or alias pow) and explicitly annotate it.

@jonasrauber
Copy link

This also happens for map(lambda x: x.shape, xs) (where xs is a list of numpy arrays).

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

No branches or pull requests

3 participants