You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Improve syntax error for numbers with leading zero #77486
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:
assignee=Noneclosed_at=<Date2018-07-09.12:30:50.485>created_at=<Date2018-04-18.01:47:11.863>labels= ['interpreter-core', 'type-feature', '3.8']
title='Improve syntax error for numbers with leading zero'updated_at=<Date2018-07-09.12:30:50.484>user='https://github.com/stevendaprano'
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.
Activity
stevendaprano commentedon Apr 18, 2018
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)
mdickinson commentedon Apr 18, 2018
Maybe once Python 2.7 officially reaches EOL, we can remove the syntax error altogether and allow leading zeros on decimal integer literals.
mdickinson commentedon Apr 18, 2018
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 tokenCould we cover both cases in a single message? "leading zeros in decimal integer literals are not permitted; use an 0o prefix for octal integers"
serhiy-storchaka commentedon Apr 18, 2018
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.
serhiy-storchaka commentedon Apr 18, 2018
PR 6517 improves syntax error messages for invalid numerical literals.
"SyntaxError: invalid token" was emitted before.
No tests yet. Suggestions about error messages are welcome.
serhiy-storchaka commentedon Apr 25, 2018
In Python 2.5
0or[]is valid syntax, but it is not valid in newer versions.What is the good error message for this case?
serhiy-storchaka commentedon Jul 9, 2018
New changeset cf7303e by Serhiy Storchaka in branch 'master':
bpo-33305: Improve SyntaxError for invalid numerical literals. (GH-6517)
cf7303e