New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[docs] Document that SyntaxError's offsets are 1-indexed #87871
Comments
As pointed out by Guido in https://bugs.python.org/issue43555, we switched to making the column offsets for SyntaxError be 1-indexed consistently in https://bugs.python.org/issue34683 The rationale is explained by Guido and expanded upon in follow up comments here: #9338 (review) This property however was not actually ever added to SyntaxError's documentation: https://docs.python.org/3/library/exceptions.html#SyntaxError |
Should we mention that the 4 attributes are also available as a 4-tuple that is the 2nd item of the args tuple, after the message? Doing so will help when illustrating the following. For syntax errors in f-string fields, the expanded doc is still incomplete/erroneous. The text attribute is the replacement expression wrapped in parentheses (or with {} replaced by ()), with \n added; the column is the (1-based) offset within that constructed source. >>> try:
compile("f'Bad {a b} field'", '', 'exec')
except SyntaxError as e:
print(e.args) ('f-string: invalid syntax', ('', 1, 4, '(a b)\n')) This was brought to my attention a week or so ago because IDLE still thinks that 4 refers to the 'a' of 'Bad' and marks it is the error location. I had to deduce the rule from examples. Whether for this issue or a new one, I am thinking of something like the following. For syntax error instances, the args attribute is (error-message, details-tuple). The str returns only the error message. For convenience, the four details are also available as separate attributes. <new list as is> For errors in f-string fields, the message is prefixed by "f-string:" and the offset is the offset in a text constructed from the replacement expression. For example, compiling f'Bad {a b} field' results in the following args attribute: ('f-string: invalid syntax', ('', 1, 4, '(a b)\n')). I think it okay to require the reader to match '{a b}' and '(a b)\n' to deduce the transformation rule. Messages other than 'invalid syntax', like 'closing parenthesis ...' get the same prefix. Anything Python specific can be labelled as such. |
Aah thanks for pointing that out Terry, I didn't realize f-strings have different semantics for SyntaxError. Would you mind making a separate issue for that, I'll look into making the documentation for it clearer after I get a chance to investigate. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: