Add `isinstance()` expression
- Dominant language
- Java
- Stars
- 25.8k
- Forks
- 4.6k
- Avg merge
- 2d 16h
- Merged PRs (30d)
- 72
Description
(Forked from #27372, which ended up specifically tracking just reserving `isinstance` as a keyword.)
This is to replace the idiom of comparing the result of `type()` to a string literal. Instead of writing
```python
if type(x) == "int":
```
you'd write
```python
if isinstance(x, int):
```
This is a more robust alternative for discriminating untagged union types.
Originally we wanted to support this in Bazel 9.x but it's unclear whether we'll backport it.
The reason to reserve `isinstance` as a keyword was that we thought we needed to distinguish all type expressions syntactically from ordinary expressions. That is no longer the case in the current design, but it may still come in handy to have isinstance be special syntax. (If we're wrong about that, we can undo the keyword reservation in a later Bazel release.)
Contributor guide
Research direction
No files or tests are named. Start by locating Bazel's Starlark parser and the existing type() string-comparison handling, then trace how reserved keywords are represented. Done means the isinstance(x, int) expression is accepted and provides the described alternative for discriminating untagged union types, with coverage for its syntax and behavior.
Written by the indexing model from the issue text.
Assessment
- Domain
- build-system
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100