Skip to content

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

Closed
@srittau

Description

@srittau

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions