Skip to content

[1.20 regression] when same object is narrowed multiple times #21181

@ktbarrett

Description

@ktbarrett

Bug Report

There was a regression in narrowing with isinstance checks from 1.19 to 1.20. This seems to only happen when the same object is narrowed multiple times. For example, this works correct if removing the outer is not None guard and the | None from the argument.

To Reproduce

mypy playground link

from __future__ import annotations


def _score_test(
    exc: BaseException | None, expected_excs: set[type[BaseException]]
) -> None:
    if exc is not None:
        reveal_type(exc)
        reveal_type(expected_excs)  # Revealed type is "BaseException"
        reveal_type(tuple(expected_excs))  # Revealed type is "tuple[type[BaseException], ...]"
        if isinstance(exc, tuple(expected_excs)):
            reveal_type(exc)  # Revealed type is "object"
            return logexc(exc)  # Argument 1 to "logexc" has incompatible type "object"; expected "BaseException | None"

def logexc(
    exc: BaseException | None
) -> None:
    ...

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrongtopic-type-narrowingConditional type narrowing / binder

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions