mypy icon indicating copy to clipboard operation
mypy copied to clipboard

Type narrowing fails on type(None) or NoneType

Open Jonathan-Landeed opened this issue 7 months ago • 0 comments

Bug Report

Type narrowing can't remove type(None) from a union.

To Reproduce

def get_class(x: int) -> type[str | None]:
    if x > 0:
        return str
    else:
        return type(None)


x = get_class(1)
if x is type(None):
    print("hi")
else:
    x("test") # Cannot instantiate type "Type[None]"

Expected Behavior

Type narrowing should eliminate that possibility. It works with None, but not NoneType.

Actual Behavior

Cannot instantiate type "Type[None]"

Your Environment

mypy 1.16.0 Python 3.12.10

Jonathan-Landeed avatar Jun 17 '25 21:06 Jonathan-Landeed