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

Error should be flagged if Walrus operator is used for multiple assigment #86461

Closed
JohnPie mannequin opened this issue Nov 9, 2020 · 2 comments
Closed

Error should be flagged if Walrus operator is used for multiple assigment #86461

JohnPie mannequin opened this issue Nov 9, 2020 · 2 comments
Labels
3.9 only security fixes build The build process and cross-build

Comments

@JohnPie
Copy link
Mannequin

JohnPie mannequin commented Nov 9, 2020

BPO 42295
Nosy @isidentical, @johnpie
Files
  • walrustest.py: Python code
  • 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 2020-11-09.12:44:05.710>
    created_at = <Date 2020-11-09.10:48:14.577>
    labels = ['build', 'invalid', '3.9']
    title = 'Error should be flagged if Walrus operator is used for multiple assigment'
    updated_at = <Date 2020-11-09.12:44:05.710>
    user = 'https://github.com/JohnPie'

    bugs.python.org fields:

    activity = <Date 2020-11-09.12:44:05.710>
    actor = 'BTaskaya'
    assignee = 'none'
    closed = True
    closed_date = <Date 2020-11-09.12:44:05.710>
    closer = 'BTaskaya'
    components = []
    creation = <Date 2020-11-09.10:48:14.577>
    creator = 'JohnPie'
    dependencies = []
    files = ['49583']
    hgrepos = []
    issue_num = 42295
    keywords = []
    message_count = 2.0
    messages = ['380580', '380581']
    nosy_count = 2.0
    nosy_names = ['BTaskaya', 'JohnPie']
    pr_nums = []
    priority = 'normal'
    resolution = 'not a bug'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'compile error'
    url = 'https://bugs.python.org/issue42295'
    versions = ['Python 3.9']

    @JohnPie
    Copy link
    Mannequin Author

    JohnPie mannequin commented Nov 9, 2020

    #The 'Walrus' operator does not support multiple assignment but does not #flag an attempt to make a multiple assigment as an error
    #This results in unexpected behavior at execution time:

    a, b = 100, 200

    print (a, b)
    #result is
    #100 200

    if (a, b := 3, 4): #this should be flagged as an error but is not

    print ("found true")
    else:
    print ("found false")

    print (a, b)
    #result is
    #100 3 but if multiple assigment were allowed this would be '3, 4'

    ----------------------------------------------------------------------

    Python 3.9.0 (tags/v3.9.0:9cf6752, Oct  5 2020, 15:34:40) [MSC v.1927 64 bit (AMD64)] on win32
    Type "help", "copyright", "credits" or "license()" for more information.
    >>> 
    = RESTART: C:\Users\John PC 2017\AppData\Local\Programs\Python\Python38-32\walrustest.py
    100 200
    found true
    100 3
    >>>

    @JohnPie JohnPie mannequin added 3.9 only security fixes build The build process and cross-build labels Nov 9, 2020
    @isidentical
    Copy link
    Sponsor Member

    Due to the precedence of the walrus operator, it is not actually a multiple assignment but rather a tuple of 3 elements with one being the value of the assignment expression.

     $ python -m ast  
    (a, b := 3, 4)
    Module(
       body=[
          Expr(
             value=Tuple(
                elts=[
                   Name(id='a', ctx=Load()),
                   NamedExpr(
                      target=Name(id='b', ctx=Store()),
                      value=Constant(value=3)),
                   Constant(value=4)],
                ctx=Load()))],
       type_ignores=[])

    In this case, it creates a tuple with loading the name a from the current scope, using the value of the 3 and also assigning 3 to the b, and loading constant 4.

    So basically (a, b := 3, 4) is actually (a, (b := 3), 4)

    @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.9 only security fixes build The build process and cross-build
    Projects
    None yet
    Development

    No branches or pull requests

    1 participant