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

Dictionary unpacking causes a transpiling error #837

Open
tomkcook opened this issue Dec 5, 2022 · 2 comments
Open

Dictionary unpacking causes a transpiling error #837

tomkcook opened this issue Dec 5, 2022 · 2 comments
Labels

Comments

@tomkcook
Copy link

tomkcook commented Dec 5, 2022

Example:

#__pragma__('skip')
def el(name, attrs, *children):
    pass
#__pragma__('noskip')
def App():
    return el("div", {
        **{'class': 'my-class'}
    })

Transcrypt output:

$ python3 -m transcrypt test.py

Transcrypt (TM) Python to JavaScript Small Sane Subset Transpiler Version 3.9.0
Copyright (C) Geatec Engineering. License: Apache 2.0


Saving target code in: /home/tkcook/git/test/__target__/org.transcrypt.__runtime__.js
Saving minified target code in: /home/tkcook/git/test/__target__/org.transcrypt.__runtime__.js

Error while compiling (offending file last):
	File 'test', line 6, namely:
	
	Error while compiling (offending file last):
	File 'test', line 6, namely:
	

Aborted

The dictionary unpack is obviously redundant in this case but is useful in more complex cases.

@JennaSys
Copy link
Collaborator

JennaSys commented Dec 5, 2022

There must be an edge case somewhere in the compiler. With a slightly simpler example, this works:

def test(args):
    print(list(args.keys()))

test(dict(**{'class': 'my-class'}))

But this does not:

def test(args):
    print(list(args.keys()))

test({**{'class': 'my-class'}})

Though both versions work in CPython.

@mentalisttraceur
Copy link

mentalisttraceur commented Dec 6, 2022

Those last two examples are actually significantly different at the technical/language level, and the latter is actually a lot newer in CPython.

  1. dict(**a_dictionary) is a splatting a dictionary into arguments of a function call. In Python since 2.2 (over 20 years ago).
  2. {**a_dictionary} is splatting a dictionary into a dictionary literal "display". Added in 3.5 by PEP-448.

If I had to guess, Transcrypt has different code paths for "translating the stuff inside a function call" and "translating the stuff inside a dictionary display", and the former is expecting the possibility of a ** while the latter isn't.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants