示例工程 11-protocols-generics-overloads

11-protocols-generics-overloads/05-typeis-narrowing/typeis_case.py.txt

text · 0.2 KB

from typing import TypeIs
 
 
def is_str_list(value: int | list[str]) -> TypeIs[list[str]]:
    return isinstance(value, list)
 
 
def use(value: int | list[str]) -> int:
    if not is_str_list(value):
        return value + 1
    return len(value)