Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Type narrowing fails on type(None) or NoneType #19308

Open
@Jonathan-Landeed

Description

@Jonathan-Landeed

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

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions