[issue39516] ++ does not throw a SyntaxError

Marco Sulla report at bugs.python.org
Tue Feb 4 14:49:31 EST 2020


Marco Sulla <launchpad.net at marco.sulla.e4ward.com> added the comment:

> this is the sort of thing that is usually best suited to be reported by linters, not the Python runtime.

TL;DR: if you write something like `a -- b`, it's quite extraordinary that you really wanted to write this. You probably wanted to write `a - -b` or, more probably, `a -= b`. So the parser is **masking a potential subtle bug**, that can cause hours of stressing debugging, because probably the program will run without problem but gives you a wrong result, or will throw an exception but in a completely different point.

Long version:

Normally I agree, but not in this case.

PEP 8 defines line guides for writing a more readable code. They are not mandatory because:

1. there are cases in which is more readable if you not follow PEP 8 (for example, using `\` with long `with` statements)
2. there are cases in which the rule is not followed because of using it in fast tests (as for from module import *)
3. sometimes is simply not possible to follow PEP 8 (for example, many classes can easily implement __eq__, but implementing all the other comparison operators many times is simply not possible)
4. sometimes the recommendation can't be followed, because it's not what you want to achive (for example, sometimes you need to check the exact class of an object and use `type(a) == SomeClass` instead of `isinstance(a, SomeClass)`)
5. there are cases that PEP 8 does not work. For example, bool(numpy.ndarray) does not work, you must do len(numpy.ndarray)
6. sometimes, it's simply a matter of style. One prefers a style, another one prefer another style

That said, none of these valid border cases can be applied to this case:

1. `a+-b` can be NEVER more readable than `a + -b`
2. `a++b` is clearly faster because you have to write... 2 spaces less. Well, I think that you'll never write a ton of binary operators followed by a unary one, so I suppose two little blank spaces does not slow down you too much :-D
3. it's always possible to separate `a * -b`, for example
4. if you write something like `a -- b`, it's quite extraordinary that you really wanted to write this. You probably wanted to write `a - -b` or, more probably, `a -= b`. So the parser is **masking a potential subtle bug**, that can cause hours of stressing debugging, because probably the program will run without problem but gives you a wrong result, or will throw an exception but in a completely different point.
5. See 3
6. this is IMHO not a matter of style. Writing `a ++ b` is simply ugly, **much** unreadable and prone to errors.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue39516>
_______________________________________


More information about the Python-bugs-list mailing list