Skip to content

Wrong type in list after isinstance check #406

@AlexElvers

Description

@AlexElvers

The following code appends a str to a list of ints.

from typing import Iterable, List

def modify(l: Iterable[object]) -> None:
    if isinstance(l, List[object]):
        l.append("abc")

l = List[int]()
modify(l)
print(l)  # ['abc']
1 + l[0]  # TypeError: unsupported operand type(s) for +: 'int' and 'str'

(without mypy errors/warnings)

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions