Skip to content
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

Closed
stevendaprano opened this issue Apr 18, 2018 · 7 comments
Closed

Improve syntax error for numbers with leading zero #77486

stevendaprano opened this issue Apr 18, 2018 · 7 comments
Labels
3.8 (EOL) end of life interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement

Comments

@stevendaprano
Copy link
Member

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']

    @stevendaprano
    Copy link
    Member Author

    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)

    @stevendaprano stevendaprano added type-bug An unexpected behavior, bug, or error 3.8 (EOL) end of life type-feature A feature request or enhancement and removed type-bug An unexpected behavior, bug, or error labels Apr 18, 2018
    @mdickinson
    Copy link
    Member

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

    @mdickinson
    Copy link
    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"

    @serhiy-storchaka
    Copy link
    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.

    @serhiy-storchaka
    Copy link
    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.

    @serhiy-storchaka serhiy-storchaka added the interpreter-core (Objects, Python, Grammar, and Parser dirs) label Apr 18, 2018
    @serhiy-storchaka
    Copy link
    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?

    @serhiy-storchaka
    Copy link
    Member

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

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.8 (EOL) end of life interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants