[proxy] bugs.python.org← back | site home | direct (HTTPS) ↗ | proxy home | ◑ dark◐ light

Issue 33305: Improve syntax error for numbers with leading zero

Issue33305

classification
Title: Improve syntax error for numbers with leading zero
Type: enhancement Stage: resolved
Components: Interpreter Core Versions: Python 3.8
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: mark.dickinson, serhiy.storchaka, steven.daprano
Priority: normal Keywords: patch

Created on 2018-04-18 01:47 by steven.daprano, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 6517 merged serhiy.storchaka, 2018-04-18 11:19
Messages (7)
msg315431 - (view) Author: Steven D'Aprano (steven.daprano) * Date: 2018-04-18 01:47
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 #33304)
msg315437 - (view) Author: Mark Dickinson (mark.dickinson) * Date: 2018-04-18 07:11
Maybe once Python 2.7 officially reaches EOL, we can remove the syntax error altogether and allow leading zeros on decimal integer literals.
msg315439 - (view) Author: Mark Dickinson (mark.dickinson) * Date: 2018-04-18 07:30
For the message:

> invalid token, use 0o prefix for octal integers

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"
msg315442 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * Date: 2018-04-18 09:35
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.
msg315445 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * Date: 2018-04-18 11:27
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.
msg315724 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * Date: 2018-04-25 11:08
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?
msg321312 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * Date: 2018-07-09 12:09
New changeset cf7303ed2aa19fb48687d7140dbc86fc23c9fca4 by Serhiy Storchaka in branch 'master':
bpo-33305: Improve SyntaxError for invalid numerical literals. (GH-6517)
https://github.com/python/cpython/commit/cf7303ed2aa19fb48687d7140dbc86fc23c9fca4
History
Date User Action Args
2022-04-11 14:58:59adminsetgithub: 77486
2018-07-09 12:30:50serhiy.storchakasetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2018-07-09 12:09:38serhiy.storchakasetmessages: + msg321312
2018-04-25 11:08:13serhiy.storchakasetmessages: + msg315724
2018-04-18 11:27:43serhiy.storchakasetmessages: + msg315445
components: + Interpreter Core
2018-04-18 11:19:48serhiy.storchakasetkeywords: + patch
stage: patch review
pull_requests: + pull_request6210
2018-04-18 09:35:43serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg315442
2018-04-18 07:30:42mark.dickinsonsetmessages: + msg315439
2018-04-18 07:11:28mark.dickinsonsetnosy: + mark.dickinson
messages: + msg315437
2018-04-18 01:47:49steven.dapranosettype: behavior -> enhancement
2018-04-18 01:47:11steven.dapranocreate