This PEP proposes to allow parentheses surrounding the two-argument form of assert statements. This will cause the interpreter to reinterpret what before would have been an assert with a two-element tuple that will always be True (assert (expression, message)) to an assert statement with a subject and a failure message, equivalent to the statement with the parentheses removed (assert expression, message).
No one thought about it until Raymond Hettinger posted it as a brain teaser a couple of weeks ago. Also, most people are aware that assert is a keyword, so very few have pretended it was a function call.
Also, most people are aware that assert is a keyword, so very few have pretended it was a function call.
This is true, but it downplays the badness of this problem.
I checked through all my code from the last five years or so, and never one time did I make this mistake BUT if I were reading someone else's code and they had written...
assert (condition, message)
Well, looking at it, I would definitely have said something in review. It looks wrong, like print(a, b) used to, and like print a, b does now. :-D
But I can see someone, not even a beginner, reading over this many times and not seeing the issue.
I agree, assert should be a built-in function, rather than a keyword. It was overlooked when print() tore the world apart with 3.0, so I think it's safe to say that it have had very little impact.
I'm all for changing it. It will just have to go through __future__ purgatory for a decade or so, before I'm happy telling people to no longer rely on asserting that their tuple is non-empty.
Well, there is, in that that'd still incur pretty much all the same overhead as when present.
You'd still pay the overhead of the function call, and of evaluating the arguments, so something like
assert(some_expensive_check(), "Something went wrong")
Will still pay the whole price of the check. The only thing you save is the actual "is this condition true" check, which is pretty inconsequential. Really, you might as well not bother changing behaviour.
If you want to make it equivalent to current assert, you'd need to do more: not just make the function a no-op, but also suppress evaluation of the arguments being passed to it, which you can't do as just a normal function. I think that'd be worse: making it look more like a function, but actually having it special cased to perform very differently.
Well, there is, in that that'd still incur pretty much all the same overhead as when present.
You'd still pay the overhead of the function call, and of evaluating the arguments, so something like
You forget that we're discussing relative to a PEP that will require changes to the parser. As you seem comitted to do so, I don't really see a fundamental difference in leaving a statement or a call to a function with a particular name out of the AST, if one of the -O flags are present.
But again, my own preference is not to special case it. People who ignore the warning are definitely asking for the pain.
You forget that we're discussing relative to a PEP that will require changes to the parser
No, but as I mentioned, for this you'd require more than making it a function - you'd also need to suppress evaluation of arguments which removing from the generated code would do, but I do think is worse than leaving it a statement: you're making something that looks like a function, but actually has special-cased behaviour. Even worse, I'm not sure you can really implement it that way only for actual assert: if you're going to make it just another function, then you'd be able to override that name. Do you then special case any function call named assert? Or make it half-way to a keyword and forbid rebinding it.
There are also potential complexities that go beyond just looking though the AST for a function call with the right name. Eg. what will happen with the below:
checker = assert
checker(cond)
[f(cond) for f in [assert, print]]
def assert(*args): pass
assert(cond)
import builtins
builtins.assert(cond)
This isn't just a matter of checking for the AST for function calls with a name being assert - there more complicated issues. Do we unbind the assert function as well, and let these throw NameErrors on optimised builds? Do we go by name only (which seems the only reliable way - it's impossible to be sure what a function is in many cases), meaning checker won't behave like assert, even though it is the assert function, while assert will act like it even though it isn't?
I think if you're going to have assert act differently from other functions - especially if "assert" here means "any function named assert", then I think it'd be better to leave it as a statement, rather than a function whose name triggers magic behaviour in the parser.
39
u/genericlemon24 Jan 21 '22
Still draft.
Abstract: