Open
Description
Bug Report
In the example below, is_str
is a type guard, but when used mypy fails to narrow the type. I'm pretty sure this happens because of the assignment expression.
To Reproduce
https://mypy-play.net/?mypy=latest&python=3.12&gist=42da00a72ace0a51a5d2335721877591
import typing
s: str | int
def is_str(x: str | int) -> typing.TypeGuard[str]:
return bool(
isinstance(x, str)
and (y if len(y := '123'.split('/')) > 1 else '')
)
if is_str(s):
s + ''
Expected Behavior
No mypy errors.
Actual Behavior
main.py:14: error: Unsupported operand types for + ("int" and "str") [operator]
main.py:14: note: Left operand is of type "str | int"
Found 1 error in 1 file (checked 1 source file)
Your Environment
- Mypy version used: 1.19
- Mypy command-line flags: non
- Mypy configuration options from
mypy.ini
(and other config files): non - Python version used: 3.12