python / python/typeshed

TypeIs for isinstance

Open
#15,844 14 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
5.1k
Forks
2.1k
Avg merge
1d 19h
Merged PRs (30d)
82

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linked scripts/check_docstrings.py example and the requested isinstance/TypeIs behavior. Clarify how single and multiple checks should narrow types, then identify the typeshed stubs and tests needed to validate that behavior; done means the proposal has a defined typing result and corresponding coverage.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Quiet
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.