TranscryptOrg / Transcrypt

Python 3.9 to JavaScript compiler - Lean, fast, open!
https://www.transcrypt.org
Apache License 2.0
2.85k stars 214 forks source link

List concatenation not working #844

Closed chrisb-fico closed 1 year ago

chrisb-fico commented 1 year ago

Of these three functions which concatenate two lists, only the third one works properly:

def concat1():
    a = ['a', 'b']
    b = ['c', 'd']
    return a + b

def concat2():
    a = ['a', 'b']
    b = ['c', 'd']
    return [*a, *b]

def concat3():
    a = ['a', 'b']
    b = ['c', 'd']
    r = []
    r.extend(a)
    r.extend(b)
    return r

The first returns a string, 'a,bc,d', and the second returns a list containing two nested lists, [ [ 'a', 'b' ], [ 'c', 'd' ] ]

I have tested this in the latest PyPI version, 3.9.0, which is also the latest Git master revision at the time of writing this.

chrisb-fico commented 1 year ago

Related: [1] * 4 does not produce [1, 1, 1, 1] as expected

JennaSys commented 1 year ago

Are these not working even with operator overloads enabled using the opov pragma?

chrisb-fico commented 1 year ago

Thanks! This fixes all cases except for [*a, *b], which still produces [['a', 'b'], ['c', 'd']].

IMO this feature should be made more prominent in the docs, given that it silently produces incorrect behaviour. Even more helpful would be to emit a warning when it is obvious that non-numeric objects are being added (in my case it I was adding list literals, which should be easy to detect).