Skip to content

TypeIs for isinstance #15844

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions