-
-
Notifications
You must be signed in to change notification settings - Fork 30.7k
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
Improve syntax error for numbers with leading zero #77486
Comments
The Python 2.x syntax for octal integers is a syntax error in 3.x, but the error message is very uninformative: SyntaxError: invalid token Can this be improved? Perhaps to something like: invalid token, use 0o prefix for octal integers (see also bpo-33304) |
Maybe once Python 2.7 officially reaches EOL, we can remove the syntax error altogether and allow leading zeros on decimal integer literals. |
For the message:
I'd expect (without having any evidence to back this up) that the majority of people who encounter this error would be those who intended a decimal literal rather than an octal one, in which case an error message that talks about octal might be confusing. >>> import datetime
>>> birthday = datetime.datetime(1912, 06, 23)
File "<stdin>", line 1
birthday = datetime.datetime(1912, 06, 23)
^
SyntaxError: invalid token Could we cover both cases in a single message? "leading zeros in decimal integer literals are not permitted; use an 0o prefix for octal integers" |
It still can cause when copy octal constants from other languages or from old Python 2 books and articles. Perhaps it should emit SyntaxWarning if all digits are in range 0-7 and the number is larger than 7. |
PR 6517 improves syntax error messages for invalid numerical literals. >>> 012
File "<stdin>", line 1
SyntaxError: leading zeros in decimal integer literals are not permitted; use an 0o prefix for octal integers
>>> 0o129
File "<stdin>", line 1
SyntaxError: invalid digit '9' in octal literal
>>> 0o
File "<stdin>", line 1
SyntaxError: invalid octal literal
>>> 1_2_
File "<stdin>", line 1
SyntaxError: invalid decimal literal
>>> 0.1_2_
File "<stdin>", line 1
SyntaxError: invalid decimal literal
>>> 12e+
File "<stdin>", line 1
SyntaxError: invalid decimal literal
>>> 12e+1_
File "<stdin>", line 1
SyntaxError: invalid decimal literal "SyntaxError: invalid token" was emitted before. No tests yet. Suggestions about error messages are welcome. |
In Python 2.5 What is the good error message for this case? |
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: