mypy icon indicating copy to clipboard operation
mypy copied to clipboard

[1.18 regression] Negative type narrowing of `TypeIs` of Union with `TypeVar` not working

Open FancyNeuron opened this issue 1 month ago • 2 comments

Bug Report

When I use a TypeIs on a union of a TypeVar and something else (for example int), it seems the negative type narrowing does not work. mypy accepts it as safe in version 1.17.1, but fails since 1.18.1. In my stack overflow question about this, a commenter said this was changed in #18193 and they suspect it to be a bug. I don't know if it is a bug, however the code below sure looks safe to me.

To Reproduce

Check the following code with mypy, at least version 1.18.1 mypy playground

from typing import TypeVar, Callable, Any
from typing_extensions import TypeIs

_T = TypeVar('_T')

def foo(x: _T | int, checker: Callable[[Any], TypeIs[_T]], default: int) -> int:
    if checker(x):
        return default
    else:
        return x

Expected Behavior Succeeds

Actual Behavior Fails

main.py:12: error: Incompatible return value type (got "_T | int", expected "int")  [return-value]
Found 1 error in 1 file (checked 1 source file)

-> Error is raised on the return line in the else branch

Your Environment

  • Mypy version used: 1.18.1
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files): unchanged
  • Python version used: 1.12

FancyNeuron avatar Nov 29 '25 16:11 FancyNeuron

I can confirm that I still think this code is safe - please enlighten me if I misunderstand the TypeIs semantics, but this looks exactly like the intended use case of TypeIs. The TypeIs PEP does not provide any generic examples, and neither does the spec.

Btw, my initial guess that we can't synthesise the intersection due to missing upper bound was wrong, the problem persists even with _T = TypeVar('_T', bound=str).

Bisects to #18193, cc @brianschubert - please confirm that this issue is valid, I might be missing some unsafe behavior here!

sterliakov avatar Nov 29 '25 16:11 sterliakov

@sterliakov thanks for the ping! I don't have the time to think about this too deeply now, but at a glance I'd say this seems valid. It wasn't something I thought about in https://github.com/python/mypy/pull/18193

brianschubert avatar Nov 29 '25 17:11 brianschubert