Skip to content

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

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__ importannotationsdef_score_test(
exc: BaseException|None, expected_excs: set[type[BaseException]]
) ->None:
ifexcisnotNone:
reveal_type(exc)
reveal_type(expected_excs) # Revealed type is "BaseException"reveal_type(tuple(expected_excs)) # Revealed type is "tuple[type[BaseException], ...]"ifisinstance(exc, tuple(expected_excs)):
reveal_type(exc) # Revealed type is "object"returnlogexc(exc) # Argument 1 to "logexc" has incompatible type "object"; expected "BaseException | None"deflogexc(
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