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.

Author JD-Veiga
Recipients JD-Veiga
Date 2021-01-11.18:38:58
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1610390338.44.0.382360444411.issue42895@roundup.psfhosted.org>
In-reply-to
Content
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.
History
Date User Action Args
2021-01-11 18:38:58JD-Veigasetrecipients: + JD-Veiga
2021-01-11 18:38:58JD-Veigasetmessageid: <1610390338.44.0.382360444411.issue42895@roundup.psfhosted.org>
2021-01-11 18:38:58JD-Veigalinkissue42895 messages
2021-01-11 18:38:58JD-Veigacreate