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

Possibly spurious SyntaxError: annotated name can't be global #79120

Closed
rohanpadhye mannequin opened this issue Oct 9, 2018 · 8 comments
Closed

Possibly spurious SyntaxError: annotated name can't be global #79120

rohanpadhye mannequin opened this issue Oct 9, 2018 · 8 comments
Labels
3.7 (EOL) end of life interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error

Comments

@rohanpadhye
Copy link
Mannequin

rohanpadhye mannequin commented Oct 9, 2018

BPO 34939
Nosy @gvanrossum, @serhiy-storchaka, @ilevkivskyi, @pablogsal, @tirkarthi, @rohanpadhye
PRs
  • bpo-34939: Allow annotated global names in module namespace #9844
  • 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-10-14.17:07:08.454>
    created_at = <Date 2018-10-09.01:58:38.509>
    labels = ['interpreter-core', 'type-bug', '3.7']
    title = "Possibly spurious SyntaxError: annotated name can't be global"
    updated_at = <Date 2018-10-14.17:07:08.453>
    user = 'https://github.com/rohanpadhye'

    bugs.python.org fields:

    activity = <Date 2018-10-14.17:07:08.453>
    actor = 'pablogsal'
    assignee = 'none'
    closed = True
    closed_date = <Date 2018-10-14.17:07:08.454>
    closer = 'pablogsal'
    components = ['Interpreter Core']
    creation = <Date 2018-10-09.01:58:38.509>
    creator = 'rohanpadhye'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 34939
    keywords = ['patch']
    message_count = 8.0
    messages = ['327378', '327384', '327386', '327420', '327421', '327424', '327554', '327716']
    nosy_count = 6.0
    nosy_names = ['gvanrossum', 'serhiy.storchaka', 'levkivskyi', 'pablogsal', 'xtreak', 'rohanpadhye']
    pr_nums = ['9844']
    priority = 'normal'
    resolution = None
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue34939'
    versions = ['Python 3.7']

    @rohanpadhye
    Copy link
    Mannequin Author

    rohanpadhye mannequin commented Oct 9, 2018

    The following code when run as a script file gives syntax error:

    def set_x():
        global x
        x = 1
    
    x:int = 0   # SyntaxError: annotated name 'x' can't be global
    

    PEP-526 does not seem to forbid this. The error message "annotated name [...] can't be global" is usually seen when using the global x declaration in the same scope as an annotated assignment. In the above case, the annotated assignment is outside the function scope, yet Python 3.7 gives a syntax error.

    Is this a bug in CPython? Or should the PEP-526 document say something about forward references?

    Interestingly, if the above program is run in interactive mode, there is no syntax error.

    In interactive mode:

    >>> def set_x():
    ...     global x
    ...     x = 1
    ... 
    >>> x:int = 0
    >>> set_x()
    >>> print(x)
    1
    

    Further, forward references work fine with nonlocal. For example, the following works fine both as a script file and in interactive mode:

    def outer():
        def inner():
            nonlocal y
            y = 1
        y:int = 0
    

    I don't see why a forward reference in global is a problem.

    @rohanpadhye rohanpadhye mannequin added 3.7 (EOL) end of life interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error labels Oct 9, 2018
    @tirkarthi
    Copy link
    Member

    The error message was added with 6cff874 and bpo-27999. The docs were also changed as below with the commit :

    https://docs.python.org/3.8/reference/simple_stmts.html#the-global-statement

    Names listed in a global statement must not be defined as formal parameters or in a for loop control target, class definition, function definition, import statement, or variable annotation.

    Thanks

    @ilevkivskyi
    Copy link
    Member

    Hm, I think this should be allowed. The formulation in the docs is not very clear, but the wording in the PEP clarifies the intention. Indeed, only annotations at the same scope with global declarations should be prohibited.

    So I think this is a bug.

    @gvanrossum
    Copy link
    Member

    I agree with Ivan.

    @serhiy-storchaka
    Copy link
    Member

    This looks similar to say

        x = 1
        global x

    It is an error in a file, but is not an error in interactive mode.

    @rohanpadhye
    Copy link
    Mannequin Author

    rohanpadhye mannequin commented Oct 9, 2018

    Another point I'd like to make is that there is no error in either file or interactive if the annotated assignment appears before the global declaration like so:

    x:int = 0
    
    def set_x():
        global x
        x = 1
    
    # Works fine!
    

    The syntax error specifically occurs when the annotated assignment occurs after a global declaration in the program, even if the assignment is in a different scope.

    Neither the docs nor the PEP say anything about such an ordering constraint.

    @ilevkivskyi
    Copy link
    Member

    I think we can just go ahead and allow this. If there is a volunteer, please go ahead, otherwise I will try to find time for this myself.

    @pablogsal
    Copy link
    Member

    New changeset de2aea0 by Pablo Galindo in branch 'master':
    bpo-34939: Allow annotated global names in module namespace (GH-9844)
    de2aea0

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    ilia1243 added a commit to Netcracker/KubeMarine that referenced this issue Apr 17, 2023
    ilia1243 added a commit to Netcracker/KubeMarine that referenced this issue Apr 17, 2023
    ilia1243 added a commit to Netcracker/KubeMarine that referenced this issue Apr 18, 2023
    koryaga pushed a commit to Netcracker/KubeMarine that referenced this issue Apr 25, 2023
    * Split compatibility map into few YAML configurations
    
    * Refactoring in thirdparties enrichment
    
    * Implementation of thirdparties management automation
    
    * Haproxy and Keepalived no longer depend in Kubernetes version
    
    * Add synchronization of packages
    
    * Add synchronization of kubernetes images.
    
    * Rename plugins
    
    * Add synchronization of plugins
    
    * Move from globals.yaml and auto synchronize supported kubernetes_versions
    
    * Make manifest enrichment independent on cluster and run test enrichment
    
    * Workaround python/cpython#79120
    
    * Implement simple test of thirdparties sync
    
    * Stage all changed and added files
    
    * Print links to software requirements
    
    * Take package versions from previous Kubernetes version
    
    * Changes to support PR #416
    
    * Add more integration tests
    
    * Refactoring for unit tests
    
    * Implement unit tests
    
    * Remove unnecessary option
    
    * Auto-update license header
    
    * Fix upgrade of pause and reconfigure containerd
    
    ---------
    
    Co-authored-by: ilia1243 <ilia1243@users.noreply.github.com>
    commonism added a commit to commonism/cryptography that referenced this issue Mar 21, 2024
    alex added a commit to pyca/cryptography that referenced this issue Mar 22, 2024
    * Adding support for OpenSSH ecdsa-sk & ed25519-sk public keys
    
    fixes #10604
    
    * Revert changing the keygen
    
    * Add application string to sk key generation
    
    * Typing - fix load_application return value annotation
    
    * fix sk keys skipping loading in the tests
    
    * fix ruff E509
    
    * Fix ruff …
    
    * comment wording
    
    Co-authored-by: Alex Gaynor <alex.gaynor@gmail.com>
    
    * requested changes
    
    * no subclassing
    
    * fix SyntaxError: annotated name '_KEY_FORMATS' can't be global
    
    in python 3.7
    
    c.f. python/cpython#79120
    
    * typo
    
    * Update src/cryptography/hazmat/primitives/serialization/ssh.py
    
    Co-authored-by: Alex Gaynor <alex.gaynor@gmail.com>
    
    * Update src/cryptography/hazmat/primitives/serialization/ssh.py
    
    Co-authored-by: Alex Gaynor <alex.gaynor@gmail.com>
    
    ---------
    
    Co-authored-by: Alex Gaynor <alex.gaynor@gmail.com>
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.7 (EOL) end of life 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

    5 participants