
I've implemented a prototype of `TypeGuard` in the latest version of pyright if you'd like to try it out. I've made a few small changes to my original proposal: 1. `TypeGuard` is now effectively treated as an alias for `bool`, eliminating the need to construct an actual `TypeGuard` object for the return value. ```python from typing_extensions import TypeGuard def is_two_element_tuple(val: Tuple[_T, ...]) -> "TypeGuard[Tuple[_T, _T]]": return len(val) == 2 ``` 2. In my original proposal, I said that type guard functions should be limited to a single parameter. I've found uses for multiple parameters, and I don't see a justification for this limitation. The expression passed as the argument for the first parameter is assumed to be the one that is narrowed. The other arguments are treated just like any others by the type checker. ```python def is_string_list(val: List[Union[str, float]], allow_empty: bool) -> "TypeGuard[List[str]]": if len(val) == 0: return allow_empty return all(isinstance(x, str) for x in val) ``` 3. I'm not enforcing that the output type be strictly narrower than the input type. This would preclude some valuable use cases, such as the previous one. I will write and submit a PEP as you suggested. -- Eric Traut Contributor to Pyright and Pylance Microsoft Corp.