On Thu, Aug 08, 2019 at 04:28:28PM -0700, Barry Warsaw wrote:
On Aug 8, 2019, at 14:58, Steven D'Aprano <steve@pearwood.info> wrote:
It's not a syntax error. There's nothing wrong with the syntax per-say: we still have ``target := expression``. There's a problem with the *semantics* not the syntax.
I’m not sure that distinction is meaningful though. What you wrote is disallowed, so you have to change your code (i.e. syntax) to avoid the error.
I find it difficult to imagine a more meaningful distinction than that between syntax and semantics. I can write something with invalid syntax that you can nevertheless predict the intended semantics: ipmort sys and write something with correct syntax that is almost impossible to determine the semantics without a huge amount of effort: https://github.com/davekch/b/blob/master/b.py (and they say you can't write one-liners in Python...) Should x/0 be a SyntaxError? It too is "disallowed", and we have to change our code. Should None[x] be a SyntaxError? It too is "disallowed", and we have to change our code. I cannot think of a single programming error that doesn't require us to "change your code (i.e. syntax) to avoid the error".
The name is perfectly self-descriptive: TargetScopeError means that there's an error with the scope of the target. What else could it mean?
What’s the scope? What’s *a* scope (in this context)? What’s the target? Nothing about the exception name tells you that it’s a problem with the assignment expression you wrote.
It isn't a requirement for the exception *type* _alone_ to give a complete and detailed explanation of the error and all the background knowledge you need to make sense of it. No other exception lives up to that high standard. Not even ValueError. (If you think "value" is easy enough, you ought to read some of the debates on comp.lang.python.) What's "syntax" mean in the context of programming? (If you can assume the reader doesn't know what scope and target mean, I can assume the reader doesn't know what syntax is.) What's wrong with the syntax? (Nothing. The syntax is fine. Its the semantics we are prohibiting.) [...]
Once you learned what a key was — and you have to do that pretty early on in your Python experience — KeyError is perfectly descriptive.
You can cross out "key" and replace it with "(assignment) target" and "scope", and your comment still applies. These are basic, simple terms in common use, like assignment, global and class. "Scope" is used in the tutorial's fourth chapter: https://docs.python.org/3/tutorial/controlflow.html "Key" in the sense of a dict not until the fifth: https://docs.python.org/3/tutorial/datastructures.html We're not talking about obscure terminology known only to a tiny handful of specialists here.
So let’s imagine what the user experience would be like with a better named exception to see whether a subclass is worth it. For now let’s call it AssignmentExpressionDisallowedError. If you saw that it would definitely be more descriptive.
It's certainly *longer*, but I'm not so sure it describes the problem better. The assignment expression is disallowed. That's implied by the fact that we have an exception in the first place. The important question is why it has been disallowed? - Because it's Tuesday, and they are never allowed on Tuesdays? - Because the assignment target is a read-only name binding? (Python doesn't have those, but a beginner might not know that.) - Because access controls have prevented the assignment? - Or because there's a problem with the scope of the assignment target?
Is that valuable in the error output?
Its certainly more valuable than SyntaxError. I wouldn't choose "Assignment Disallowed" over "Target Scope", but let's go with it. Which conveys more information about the error? "I'm getting a syntax error." "I'm getting an assignment disallowed error." I'm a beginner, my google-fu is not strong, and I get this exception. If I google for SyntaxError without the error message, will I get good quality results? How about if I google for AssignmentDisallowedError?
Please let's not use SyntaxError for something that's not an error with the syntax.
PEP 572 does propose TargetScopeError as a subclass of SyntaxError, so you’re getting a SyntaxError anyway.
I know, and I'm not super happy about that, but at least it's a subclass with a descriptive name. -- Steven