示例工程 11-protocols-generics-overloads

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

text · 0.2 KB

from typing import TypeGuard
 
 
def is_str_list(value: int | list[str]) -> TypeGuard[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)