Skip to content

isinstance/issubclass don't properly handle tuple of tuples #2994

Description

@pkch

This code:

def f(x: Union[int, str, List, Tuple]) -> None:
    if isinstance(x, (str, (int, tuple))):
        print(x[1])

results in error: Argument 2 to "isinstance" has incompatible type "Tuple[str, Tuple[List[_T], Tuple[_T_co, ...]]]"; expected "Union[type, Tuple[type, ...]]".

Python 2 and 3 allow to put nested tuples as the second argument to isinstance, but neither typeshed nor mypy are aware of it. The typeshed needs to be fixed first (python/typeshed#997), but then still some logic needs to be updated inside mypy (otherwise, it results in internal error since it doesn't expect nested tuples; it should instead result in an type validation error).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions