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

Imports from __future__ fail #1306

Open
Hugovdberg opened this issue Dec 16, 2021 · 7 comments
Open

Imports from __future__ fail #1306

Hugovdberg opened this issue Dec 16, 2021 · 7 comments
Labels
bug Something isn't working

Comments

@Hugovdberg
Copy link
Contributor

Snakemake version
6.9.1

Describe the bug
When a script contains a line from __future__ import {some future behaviour} python raises a syntaxerror as snakemake inserts a few lines of code before the first line of user code. Perhaps a guard should be built in to check whether future behaviour is imported, and if so, inject the snakemake code after that line.

Logs

Minimal example

from __future__ import annotations

Additional context

@Hugovdberg Hugovdberg added the bug Something isn't working label Dec 16, 2021
@Hugovdberg
Copy link
Contributor Author

This bug is still present in recent versions of snakemake, is there a reason why the current manipulation of the script is used instead of the runpy module in the standard library? That includes a run_path function that allows you to run a script and inject variables into their globals through the use of the init_globals argument.

@pvandyken
Copy link
Contributor

I doubt there's any specific reason; that looks like a much better solution. Are you able to make a PR correcting this?

@Hugovdberg
Copy link
Contributor Author

I have been digging around a bit, and I thing one of the reasons it is done this way is that it is similar to the way R and Julia scripts are handled. Interestingly there is a regex defined in snakemake.script that would potentially help to prevent this, but I guess it isn't used (correctly).
I think I can make a PR that would implement the solution using runpy for python, but I'm still trying to wrap my head around how the code is copied and run exactly so I can inject the new behaviour at the correct location.

@pvandyken
Copy link
Contributor

I don't know anything about R or Julia, but it seems to me a language-specific method that's properly supported is much preferable to a language-agnostic method that breaks. Regex workaround seem hacky to me.

I'm not actually super familiar with snakemake.script, but let me know if there's any way I can help.

@Hugovdberg
Copy link
Contributor Author

I have some experience in R, and none in Julia, but if you would be ok with a specific method for running python scripts that makes it a lot easier I guess. Still might take some time as I'm busy finishing my thesis.

@pvandyken
Copy link
Contributor

Yeah, no worries. Not my decision actually, that's up to @johanneskoester, but it makes sense to me

@pvandyken
Copy link
Contributor

Was thinking about this a bit more, one thing to be aware of is that snakemake needs to be able to run scripts from any arbitrary python interpreter in order to properly use conda envs. So to implement your solution properly, the best way to go about it is probably to have a dedicated script that consumes all the pickled args and does all the prepwork, finishing by calling the user script using runpy. So on snakemake, you would always call this dedicated script, passing data via arguments in the same way it currently does, and you would additionally pass the path to the user script to be run.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants