Skip to content

Mypy not detecting type narrowing in another function #8224

Closed
@colinschoen

Description

@colinschoen
from typing import Optional, List
def is_not_none(lst):
    # type: (Optional[List[str]]) -> bool
    return lst is not None


def get_first_element(lst):
    # type: (Optional[List[str]]) -> str
    if is_not_none(lst):
        return lst[0]
    return 'not found'

mypy invocation: mypy --py2 .

Expected output:

Success: no issues found in 1 source file

Actual output

ex.py:12: error: Value of type "Optional[List[str]]" is not indexable
Found 1 error in 1 file (checked 1 source file)

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