Confusing error message when expressing an integer

From eBNF notation of integer in python reference:

decinteger ::= nonzerodigit (["_"] digit)* | "0"+ (["_"] "0")*

I pick up 2nd alternative “0”+ ([“_”] “0”)* to test positive and negative cases respectively:

>>> c = 0_0_00000_0
>>> c = 0_0_00000_9
  File "<stdin>", line 1
    c = 0_0_00000_9
        ^^^^^^^^^^
SyntaxError: leading zeros in decimal integer literals are not permitted; use an
 0o prefix for octal integers

I do expect an error in 2nd statement, but how could it be presumed an octal integers?
Literally, if there is a leading zero to present a *non-zero integer, it could be any of bininteger, octinteger or hexinteger. This error message sounds a little odd as 9 is not even a valid octdigit!

Hereby, I would suggest the error message to be more instructive like:
SyntaxError: leading zeros in a non-zero decimal integer literals are not permitted; use 0b, 0o, 0x for bininteger, octinteger or hexinteger integers, respectively.

Please comment.

P.S. test env:
Python 3.10.4 (tags/v3.10.4:9d38120, Mar 23 2022, 23:13:41) [MSC v.1929 64 bit (
AMD64)] on win32

The reason for the message is that historically in Python 2, leading 0s indicated an octal integer:

Python 2.7.18 (default, Oct  5 2023, 10:23:23) 
[GCC Apple LLVM 14.0.0 (clang-1400.0.29.202)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 010
8

However, now that Python 2 is mostly gone, the “use 0o prefix for octal integers” may cause more confusion than it prevents. Perhaps we should simply remove that part of the error.

1 Like

Agree. And… to be more precise, better add below bolded content:
leading zeros in a non-zero decimal integer literals are not permitted

Aha, I found it is also documented in PLR:

Note that leading zeros in a non-zero decimal number are not allowed.