Skip to content

Improve syntax error for numbers with leading zero #77486

Closed

Description

@stevendaprano
BPO 33305
Nosy @mdickinson, @stevendaprano, @serhiy-storchaka
PRs
  • bpo-33305: Improve SyntaxError for invalid numerical literals. #6517
  • 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 = None
    closed_at = <Date 2018-07-09.12:30:50.485>
    created_at = <Date 2018-04-18.01:47:11.863>
    labels = ['interpreter-core', 'type-feature', '3.8']
    title = 'Improve syntax error for numbers with leading zero'
    updated_at = <Date 2018-07-09.12:30:50.484>
    user = 'https://github.com/stevendaprano'

    bugs.python.org fields:

    activity = <Date 2018-07-09.12:30:50.484>
    actor = 'serhiy.storchaka'
    assignee = 'none'
    closed = True
    closed_date = <Date 2018-07-09.12:30:50.485>
    closer = 'serhiy.storchaka'
    components = ['Interpreter Core']
    creation = <Date 2018-04-18.01:47:11.863>
    creator = 'steven.daprano'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 33305
    keywords = ['patch']
    message_count = 7.0
    messages = ['315431', '315437', '315439', '315442', '315445', '315724', '321312']
    nosy_count = 3.0
    nosy_names = ['mark.dickinson', 'steven.daprano', 'serhiy.storchaka']
    pr_nums = ['6517']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue33305'
    versions = ['Python 3.8']

    Activity

    1. stevendaprano commented on Apr 18, 2018

      @stevendaprano
      MemberAuthor

      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)

    2. added
      type-bug
      3.8 (EOL)
      type-feature
      and removed
      type-bug
      on Apr 18, 2018
    3. mdickinson commented on Apr 18, 2018

      @mdickinson
      Member

      Maybe once Python 2.7 officially reaches EOL, we can remove the syntax error altogether and allow leading zeros on decimal integer literals.

    4. mdickinson commented on Apr 18, 2018

      @mdickinson
      Member

      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"

    5. serhiy-storchaka commented on Apr 18, 2018

      @serhiy-storchaka
      Member

      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.

    6. serhiy-storchaka commented on Apr 18, 2018

      @serhiy-storchaka
      Member

      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.

    7. added
      interpreter-core
      on Apr 18, 2018
    8. serhiy-storchaka commented on Apr 25, 2018

      @serhiy-storchaka
      Member

      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?

    9. serhiy-storchaka commented on Jul 9, 2018

      @serhiy-storchaka
      Member

      New changeset cf7303e by Serhiy Storchaka in branch 'master':
      bpo-33305: Improve SyntaxError for invalid numerical literals. (GH-6517)
      cf7303e

    10. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      3.8 (EOL)interpreter-coretype-feature

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions