Skip to content

Pyright fails to resolve type(None) to the None singleton, preventing standard type narrowing #11407

@rob3c

Description

@rob3c

Describe the bug

When type(None) is assigned to a variable and used in a Union type hint, Pyright fails to resolve it to the None singleton. This prevents standard type narrowing (e.g., if not obj.my_list) from removing the None branch, causing elements to be inferred as Unknown.

Code Sample

import typing
none_type = type(None) # Alias for the None singleton

class MyClass:
    # Pyright treats this Union as list[str] | Unknown
    my_list: typing.Union[list[str], none_type]

def fn(o: MyClass):
    if not o.my_list:
        return
    
    # Expected: list[str]
    # Actual: list[str | Unknown]
    elements = [e for e in o.my_list]

Environment

Python 3.13, Pyright/Pylance v2026.2.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions