[issue42895] Return multi-line list concatenation without parentheses returns only first operand

2021-01-11 Thread JD-Veiga

JD-Veiga  added the comment:

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.⁶

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue42895] Return multi-line list concatenation without parentheses returns only first operand

2021-01-11 Thread Steven D'Aprano


Steven D'Aprano  added the comment:

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.

--
nosy: +steven.daprano

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue42895] Return multi-line list concatenation without parentheses returns only first operand

2021-01-11 Thread Guido van Rossum


Guido van Rossum  added the comment:

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.

--
nosy: +gvanrossum
resolution:  -> not a bug
stage:  -> resolved
status: open -> closed

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue42895] Return multi-line list concatenation without parentheses returns only first operand

2021-01-11 Thread JD-Veiga


New submission from JD-Veiga :

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 `]+ [` 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.

--
components: Interpreter Core
messages: 384854
nosy: JD-Veiga
priority: normal
severity: normal
status: open
title: Return multi-line list concatenation without parentheses returns only 
first operand
type: behavior
versions: Python 3.8, Python 3.9

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com