Skip to content

FR: Type narrowing of lists based on any(isinstance) checks #1484

@Kayzels

Description

@Kayzels

Description

It would be awesome if it could narrow the type based on an all isinstance check, if possible. This does a check that all the variables are of a specific type, so the inner block should know that narrowed type.

Code sample in basedpyright playground

from typing import reveal_type

def process_list(values: list[str] | list[list[str]]) -> str:
    if all(isinstance(item, str) for item in values):
        reveal_type(values) # Should be narrowed to list[str]
        return "Should be str"
    if all(isinstance(item, list) and len(item) == 2 for item in values):
        reveal_type(values) # Should be narrowed to list[list[str]]
        return "Should be list[str]"
    return ""

Metadata

Metadata

Assignees

No one assigned

    Labels

    awaiting responsewaiting for more info from the author - will eventually close if they don't respondtype checking / lintingissues relating to existing diagnostic rules or proposals for new diagnostic rules

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions