ITADN

TypeIs for isinstance

#15844OpenOutSquareCapital 创建于 2026-05-29
Apologies if I'm missing something obvious, but I checked issues and PR's and haven't seen this mentioned explicitly. Could `isinstance` use `TypeIs`? With multiples checks it could return an union, with one type `T` a `TypeIs[T]`. I often find myself writing one-liner helpers functions that just call isinstance, see this script for example: https://github.com/OutSquareCapital/pyochain/blob/master/scripts/check_docstrings.py This both add boilerplate and a performance tax due to a double function call overhead. I'm very often using lambdas with fluent interfaces, e.g ```python res = ( my_iterator .map(lambda x: ...) .filter(lambda x: isinstance(x, int) ) ``` Assuming `my_iterator` is an `Iterator[Any]`, it would be fantastic if it could be narrowed to `Iterator[int]` without having to define a separate TypeIs function.
10 条评论