Skip to content

TypeGuard is incompatible with exhaustiveness checking #15305

Closed as not planned
Closed as not planned
@antonagestam

Description

@antonagestam

Bug Report

Narrowing with a TypeGuard function doesn't work in conjunction with exhaustiveness checking using assert_never().

To Reproduce

https://mypy-play.net/?mypy=latest&python=3.11&gist=b6bc8609a83ff0538459d658c74f23ea

Expected Behavior

In the example, I expected the TypeGuard function to work no differently from the inlined type checks.

Actual Behavior

When using TypeGuard, the variable seems to be properly narrowed (revealed_type() within the branch is identical), but it seems somehow the result isn't recorded identically as the inlined checks. In the function using TypeGuard, the exhaustiveness check is not passing.

Your Environment

  • Mypy version used: 1.3.0
  • Mypy command-line flags: -
  • Mypy configuration options from mypy.ini (and other config files): -
  • Python version used: 3.11

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrongtopic-typeguardTypeGuard / PEP 647

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions