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

False positive with list comprehensions and --warn-return-any #4345

Closed
srittau opened this issue Dec 12, 2017 · 1 comment
Closed

False positive with list comprehensions and --warn-return-any #4345

srittau opened this issue Dec 12, 2017 · 1 comment

Comments

@srittau
Copy link
Contributor

srittau commented Dec 12, 2017

Given the following code in test.py:

def bar() -> "FooClass":
    list1 = [FooClass()]
    list2 = [m for m in list1 if m.matches]
    return list2[0]


class FooClass:

    def __init__(self) -> None:
        self.matches = True

Testing it with mypy --warn-return-any test.py (mypy 0.550, Python 3.6.3) gives the following error:

test.py:4: warning: Returning Any from function declared to return "FooClass"

Any of the following "fixes" this error:

  • Move FooClass declaration in front of the declaration of bar()
  • Remove the m.matches check inside the list comprehension
  • Remove --warn-return-any from the command line
  • Adding assert isinstance(list2[0], FooClass) before the return
@srittau srittau changed the title Wrong positive with list comprehensions Wrong positive with list comprehensions and --warn-return-any Dec 12, 2017
@elazarg
Copy link
Contributor

elazarg commented Dec 12, 2017

Duplicate of #4296, fixed on master (#4295).

@gvanrossum gvanrossum changed the title Wrong positive with list comprehensions and --warn-return-any False positive with list comprehensions and --warn-return-any Dec 12, 2017
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