Skip to content

TypeIs for isinstance #15844

@OutSquareCapital

Description

@OutSquareCapital

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

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions