Skip to content

list.append seems to break return type checker #8734

@AugPro

Description

@AugPro

Hello, concerning the function below:

from typing import List
def foo() -> List[int]:
    listings = None
    if 2 != 2:
        listings = list()
        listings.append(1)
    return listings

I expected:
Incompatible return value type (got "Optional[List[int]]", expected "List[int]")

I got no error

This one does throw the expected error (same config):

from typing import List
def foo() -> List[int]:
    listings = None
    if 2 != 2:
        listings = [1]
    return listings

running mypy 0.770 on python 3.8

I would appreciate it if you could tell me if I am doing something wrong :)

Have a good day !

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions