Skip to content

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

Open

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

Activity

  1. added
    bug
    on Jun 17, 2025
  2. added
    topic-type-narrowing
    and removed
    bug
    on Jun 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    featuretopic-type-narrowing

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions