Skip to content

TypeGuard does not narrow when statement compares for identity (is True) #17483

Open
@flaeppe

Description

@flaeppe

Bug Report

In the example below f is a TypeGuard that mypy fails to narrow the type with when the statement compares with identity (is True)

To Reproduce

https://mypy-play.net/?mypy=master&python=3.12&flags=strict&gist=688d7597c968ed90ecdd8d4bf2b3a8a3

from typing import TypeGuard

def f(value: str) -> TypeGuard[int]:
    return True

# This should narrow to "builtins.int"
by_identity = "123"
assert f(by_identity) is True
reveal_type(by_identity)  # Revealed type is "builtins.str"

Expected Behavior

Revealed type is "builtins.int"

Actual Behavior

Revealed type is "builtins.str"

Your Environment

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

Metadata

Metadata

Assignees

Labels

bugmypy got something wrongtopic-type-narrowingConditional type narrowing / bindertopic-typeguard-typeisTypeGuard / TypeIs / PEP 647 / PEP 742

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions