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

Move future statements at the top #32

Open
maneandrea opened this issue Nov 6, 2023 · 2 comments
Open

Move future statements at the top #32

maneandrea opened this issue Nov 6, 2023 · 2 comments

Comments

@maneandrea
Copy link

Description:
Trying to stickytape a file containing a future statement results in an error:

SyntaxError: from __future__ imports must occur at the beginning of the file

Steps to reproduce:

  1. Write file a.py
from __future__ import annotations

def f(x: str | int) -> str | int:
    return 2 * x
  1. Run stickytape a.py > b.py
  2. Run python b.py

Expected result:
Nothing

Actual result:

  File "b.py", line 37
    from __future__ import annotations
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
SyntaxError: from __future__ imports must occur at the beginning of the file

Possible fix:
Detect such statements, for instance with a regex pattern, and move them at the beginning of the file.

re.match('from __future__ import (.*)$', line)
@mwilliamson
Copy link
Owner

Adding future imports changes the meaning of the code, so changing them to apply to everything sounds fragile to me.

In general, I'd suggest using something like zipapp instead of this pile of hacks.

@maneandrea
Copy link
Author

That's a good point. I did a PR anyway because I find it useful for my particular use case. If you are against it, feel free to close it.

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

2 participants