bazel icon indicating copy to clipboard operation
bazel copied to clipboard

Add `isinstance()` expression

Open brandjon opened this issue 3 weeks ago • 0 comments

(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

if type(x) == "int":

you'd write

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.)

brandjon avatar Dec 03 '25 15:12 brandjon