![](https://secure.gravatar.com/avatar/01aa7d6d4db83982a2f6dd363d0ee0f3.jpg?s=120&d=mm&r=g)
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.
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.
What's wrong with having to look up an unfamiliar exception by searching the interwebs? The first few times I hit KeyError, I had no idea what a "key" was (I was a beginner) and if I had internet access, I would have looked it up on the web. I didn't, so I looked it up in the book I was reading.
But remember that assignment expressions are a relatively advanced topic. Once you learned what a key was — and you have to do that pretty early on in your Python experience — KeyError is perfectly descriptive. 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. Is that valuable in the error output? Is it still valuable when you have useful descriptive text?
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. -Barry