[WIP] Remove parenthesis for single argument exceptions in raise statement#69
[WIP] Remove parenthesis for single argument exceptions in raise statement#69dosisod wants to merge 1 commit intodflook:mainfrom
raise statement#69Conversation
There is no semantic difference between `raise X()` and `raise X`, so for the few instances where you are raising an exception with no arguments you can save 2 bytes. The AST comparison is failing now though due to the `Call` node being replaced with a `Name` node, so I don't know what the best way to go about fixing that is.
|
Hi @dosisod, thanks for working on this, this is a good idea. The same thing could be applied to explicit exception chaining e.g. raise MyException() from CauseException()
raise MyException from CauseExceptionThe ModulePrinter should be printing an exact representation of the AST. For changes like this we would need to add a new transformer that visits Raise nodes and replaces its exc Call node with a Name node. |
|
I've been thinking about this, and I don't think we can say that both forms are equivalent. Consider: def create_exception():
return Exception()
raise create_exception()Transforming the final line to |
|
Good catch! Perhaps we should only apply this to built-in exceptions? Of course someone could do: Exception = lambda: __builtins__.Exception()
raise Exception() # ok
raise Exception # not okWould there by any way to detect this in the AST, that is, detect the difference between an |
|
Not from the raw AST, but it gets annotated by parts of the renamer. After the resolve_names() step the root Module node has a The If one of those references is a There is a brain-dump of how the renamer works here |
There is no semantic difference between
raise X()andraise X, so for the few instances where you are raising an exception with no arguments you can save 2 bytes. The AST comparison is failing now due to theCallnode being replaced with aNamenode, so I don't know what the best way to go about fixing that is. I have tested this on Python 3.5-3.11, but not Python 2.Also, one suggestion I have would be adding a
--no-ast-verifyflag to allow for forcing output of unstable minifications for debugging purposes, as opposed to usingprint(self.code)or something similar.