starlark icon indicating copy to clipboard operation
starlark copied to clipboard

Elevate `None`, `True`, and `False` to keyword status

Open brandjon opened this issue 8 months ago • 2 comments

In #282, it was observed that None, True, and False are merely predeclared names, not keywords, so it's legal to shadow them:

>>> None, True, False = 1, 2, 3
>>> None + True + False
6

I remember this used to be true in older versions of Python, but current Python bans assignment to any of these names. The current Python grammar makes them part of the syntax (the literal_expr node).

Assigning to these names hurts readability, since everyone expects them to be universal constants. Banning assignment is technically a backwards incompatible change, but a quick search of google3 indicates it's unlikely anyone is relying on this.

This FR does not propose any change to the present ability to shadow other predeclareds like len.

@alandonovan any objections?

brandjon avatar Mar 28 '25 15:03 brandjon

Fine by me, but only because Python did it, and limited itself to these three names. In general, retroactively reserving words is (as you point out) a breaking change.

adonovan avatar Mar 28 '25 17:03 adonovan

I think we didn't bother with this because no one cares about this (mostly theoretical) edge case.

People are more likely to redefine e.g. str by accident (as it could be a useful variable name), but this problem can be handled by a linter. So you could also have the linter detect redefinitions of True, False, None, if you think it can happen.

Either way, feel free to make them keywords or not; no one will notice. :)

laurentlb avatar Mar 28 '25 19:03 laurentlb