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

parsing default argument for _env does not work for dict literals spanning multiple lines #636

Closed
jimustafa opened this issue Jan 5, 2023 · 3 comments

Comments

@jimustafa
Copy link

The following

import sh


def test1():
    sh2 = sh(_env=dict(
        PATH='/usr/bin'
        ))
    output = sh2.env()
    print(f'test1 env: {output.stdout}')


def test2():
    sh2 = sh(_env={'PATH': '/usr/bin'})
    output = sh2.env()
    print(f'test2 env: {output.stdout}')


def test3():
    sh2 = sh(_env={
        'PATH': '/usr/bin'
        })
    output = sh2.env()
    print(f'test3 env: {output.stdout}')


test1()
test2()
test3()

gives

test1 env: b'PATH=/usr/bin\n'
test2 env: b'PATH=/usr/bin\n'
Traceback (most recent call last):
  File "test.py", line 28, in <module>
    test3()
  File "test.py", line 20, in test3
    'PATH': '/usr/bin'
  File "/home/jimustafa/.pyenv/versions/cookiecutter-voila-app/lib/python3.7/site-packages/sh.py", line 3581, in __call__
    parsed = ast.parse(code)
  File "/home/jimustafa/.pyenv/versions/3.7.13/lib/python3.7/ast.py", line 35, in parse
    return compile(source, filename, mode, PyCF_ONLY_AST)
  File "<unknown>", line 1
SyntaxError: illegal target for annotation
@ecederstrand
Copy link
Collaborator

ecederstrand commented Jan 6, 2023

Thanks for reporting!

This is a bug in sh. It's also a really hacky part of sh, as the comment in https://github.com/amoffat/sh/blob/develop/sh.py#L3564 indicates. Basically, the problem is that inspect.stack() only returns the line containing sh2 = sh(...\n, not the entire statement in the following lines. This leads us to evaluate a non-complete code snippet.

@jimustafa
Copy link
Author

Thanks @ecederstrand! Yeah, thought it might have to do with newlines in the dict literal. sh is great! And this bug is not a big issue, as we can work around it with the dict constructor if we wanted to pass in a large environment split over many lines. Thanks again!

@amoffat
Copy link
Owner

amoffat commented Feb 9, 2023

This has been remedied in 2.0.0. Note however that the sh2 = sh( syntax has changed to sh2 = sh.bake(. See more details here

@amoffat amoffat closed this as completed Feb 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants