This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Return multi-line list concatenation without parentheses returns only first operand
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 3.9, Python 3.8
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: JD-Veiga, gvanrossum, steven.daprano
Priority: normal Keywords:

Created on 2021-01-11 18:38 by JD-Veiga, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (4)
msg384854 - (view) Author: JD-Veiga (JD-Veiga) Date: 2021-01-11 18:38
I was trying to return the concatenation of several lists from a function but, inadvertently, forgot to surround the multi-line concatenation expression with parentheses. 

As a result, the function returns just the first operand and does not perform the concatenation at all. 


Basically, I am doing something like this:

```
def non_parens():
    return [
        1, 2, 3
    ]
    + [
        4, 5
    ]

print(non_parens())

```

which outputs: `[1, 2, 3]`


On the contrary, parenthesised version such as:

```
def with_parens():
    return (
        [
            1, 2, 3
        ]
        + [
            4, 5
        ]
    )

print(with_parens())

```

will output the "expected" result: `[1, 2, 3, 4, 5]`.


Even not breaking the line before the '+' operator:

```
def no_parens_without_newline_before_operator():
    return [
        1, 2, 3
    ] + [
        4, 5
    ]

print(no_parens_without_newline_before_operator())

```

prints `[1, 2, 3, 4, 5]`.



My hypothesis is that first function `non_parens()` does not work because the expression has some kind of error after

```
    return [
        1, 2, 3
    ]
```

and 

```
    + [
        4, 5
    ]
```

is never executed.


However, why does not the parser or the interpreter raise any error or warning about this malformed expression?


"Similar" expressions cause an error:

```
assert [
        1, 2, 3
    ]
    + [
        4, 5
    ]
```

raises: `IndentationError: unexpected indent`

and

```
[
    1, 2, 3
]
+ [
    4, 5
]
```

raises: `TypeError: bad operand type for unary +: 'list'`


What perplexes me the most about breaking lines in this case is that `] + [` works and `]<newline>+ [` does not (against PEP8 --of course, PEP8 is not the parser).


I am sure that I am missing something about expressions, line breaks, or lexical parsing.


I am using Python 3.8.7 and Python 3.9.1



Thank you a lot.
msg384861 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2021-01-11 20:11
This is not a bug -- it is how it's supposed to work (for better or for worse).

Please see a use forum to help you understand what's going on here.
msg384863 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2021-01-11 20:37
As Guido says, a full explanation will have to to a user-forum such as https://discuss.python.org/c/users/7 but consider this example and see if it gives you insight:

def demo():
    x = 1
    return (
        x - 1
        )
    print("This is not executed.")
    - (5 * x)

and remember that `+` can also be a unary operator.
msg384889 - (view) Author: JD-Veiga (JD-Veiga) Date: 2021-01-12 05:26
What a shame. I forget that + is also a unary operator. That produces a new logical line with correct identation though never executed. Sorry for the inconvenience.⁶
History
Date User Action Args
2022-04-11 14:59:40adminsetgithub: 87061
2021-01-12 05:26:19JD-Veigasetmessages: + msg384889
2021-01-11 20:37:16steven.dapranosetnosy: + steven.daprano
messages: + msg384863
2021-01-11 20:11:04gvanrossumsetstatus: open -> closed

nosy: + gvanrossum
messages: + msg384861

resolution: not a bug
stage: resolved
2021-01-11 18:38:58JD-Veigacreate