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

Make "global after use" a SyntaxError #72186

Closed
ilevkivskyi opened this issue Sep 7, 2016 · 10 comments
Closed

Make "global after use" a SyntaxError #72186

ilevkivskyi opened this issue Sep 7, 2016 · 10 comments
Assignees
Labels
docs Documentation in the Doc dir interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error

Comments

@ilevkivskyi
Copy link
Member

BPO 27999
Nosy @gvanrossum, @1st1, @ilevkivskyi
Files
  • patch-v1.diff
  • patch-v2.diff
  • 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 = 'https://github.com/gvanrossum'
    closed_at = <Date 2016-09-09.16:53:54.889>
    created_at = <Date 2016-09-07.14:08:38.915>
    labels = ['interpreter-core', 'type-bug', 'docs']
    title = 'Make "global after use" a SyntaxError'
    updated_at = <Date 2016-09-09.16:53:54.888>
    user = 'https://github.com/ilevkivskyi'

    bugs.python.org fields:

    activity = <Date 2016-09-09.16:53:54.888>
    actor = 'gvanrossum'
    assignee = 'gvanrossum'
    closed = True
    closed_date = <Date 2016-09-09.16:53:54.889>
    closer = 'gvanrossum'
    components = ['Documentation', 'Interpreter Core']
    creation = <Date 2016-09-07.14:08:38.915>
    creator = 'levkivskyi'
    dependencies = []
    files = ['44501', '44503']
    hgrepos = []
    issue_num = 27999
    keywords = ['patch']
    message_count = 10.0
    messages = ['274813', '274836', '274888', '274894', '274895', '275314', '275318', '275332', '275344', '275346']
    nosy_count = 5.0
    nosy_names = ['gvanrossum', 'docs@python', 'python-dev', 'yselivanov', 'levkivskyi']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = None
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue27999'
    versions = ['Python 3.6']

    @ilevkivskyi
    Copy link
    Member Author

    The documentation at https://docs.python.org/3/reference/simple_stmts.html says that:

    "Names listed in a global statement must not be used in the same code block textually preceding that global statement"

    But then later:

    "CPython implementation detail: The current implementation does not enforce the two restrictions,
    but programs should not abuse this freedom, as future implementations may enforce them..."

    Code like this

    def f():
        x = 1
        global x

    gives SyntaxWarning for several releases, maybe it is time to make it a SyntaxError?

    @ilevkivskyi ilevkivskyi added docs Documentation in the Doc dir interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error labels Sep 7, 2016
    @gvanrossum
    Copy link
    Member

    I like fixing this. I suppose it'll be fixed for nonlocal too?

    @ilevkivskyi
    Copy link
    Member Author

    Yes, nonlocal will be fixed too. This will be a relatively small patch, but I am a bit afraid there could be merge conflicts (the code affected could overlap with changes for PEP-526).

    Easiest way would be to simply make this a single patch with PEP-526 implementation (this will actually even slightly simplify the implementation).

    Alternatively we could fix this right after the implementation patch is applied. What do you prefer?

    @gvanrossum
    Copy link
    Member

    I strongly prefer to have it as a separate patch (either before or
    after) to avoid the appearance of piggy-backing this in with a much
    larger change.

    @ilevkivskyi
    Copy link
    Member Author

    OK, then I think after will be safer. Let us came back to this right after PEP-526.

    @ilevkivskyi
    Copy link
    Member Author

    I am attaching the patch.

    Yury, I also added you here, just in case you have time for this.

    Btw, while working on this I have found that the second restriction:

    """
    Names listed in a global statement must not be ... in a for loop control target, class definition, function definition, or import statement.
    """

    is not enforced at all, it does not give even a SyntaxWarning.
    Is it something worth fixing?

    @ilevkivskyi
    Copy link
    Member Author

    Added few tests to the patch

    @gvanrossum
    Copy link
    Member

    I'll commit this.

    @gvanrossum gvanrossum assigned gvanrossum and unassigned docspython Sep 9, 2016
    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Sep 9, 2016

    New changeset 804b71d43c85 by Guido van Rossum in branch 'default':
    Issue bpo-27999: Make "global after use" a SyntaxError, and ditto for nonlocal.
    https://hg.python.org/cpython/rev/804b71d43c85

    @gvanrossum
    Copy link
    Member

    Thanks again Ivan!

    @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
    docs Documentation in the Doc dir interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants