Skip to content

Commit b91a02a

Browse files
committed
test for dynamic module import without as parameter
1 parent 3ceca32 commit b91a02a

File tree

8 files changed

+118
-5
lines changed

8 files changed

+118
-5
lines changed

snakemake/workflow.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2278,19 +2278,20 @@ def userule(
22782278
def decorate(maybe_ruleinfo):
22792279
if from_module is not None:
22802280
try:
2281-
module = self.modules[from_module]
22822281
modifier = name_modifier
2282+
module = self.modules[from_module]
22832283
except KeyError:
22842284
from inspect import currentframe
22852285

22862286
if from_module in currentframe().f_back.f_globals:
22872287
module = self.modules[
22882288
currentframe().f_back.f_globals[from_module]
22892289
]
2290-
if name_modifier.endswith("*"):
2291-
modifier = f"{currentframe().f_back.f_globals[name_modifier[:-1]]}*"
2292-
else:
2293-
modifier = currentframe().f_back.f_globals[name_modifier]
2290+
if name_modifier is not None:
2291+
if name_modifier.endswith("*"):
2292+
modifier = f"{currentframe().f_back.f_globals[name_modifier[:-1]]}*"
2293+
else:
2294+
modifier = currentframe().f_back.f_globals[name_modifier]
22942295
else:
22952296
raise WorkflowError(
22962297
"Module {} has not been registered with 'module' statement before using it in 'use rule' statement.".format(
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
shell.executable("bash")
2+
3+
4+
configfile: "config/config.yaml"
5+
6+
for module_name in ["module1", "module2"]:
7+
module:
8+
name: module_name
9+
snakefile:
10+
f"{module_name}/Snakefile"
11+
config:
12+
config
13+
replace_prefix:
14+
{"results/": "results/testmodule/"}
15+
16+
use rule * from module_name
17+
18+
rule all:
19+
input:
20+
multiext(expand("results/testmodule/c1/{name}.", name="test")[0], "tsv", "txt"),
21+
multiext(expand("results/testmodule/c2/{name}.", name="test")[0], "tsv", "txt"),
22+
23+
24+
assert module1.some_func() == 15
25+
assert module2.some_func() == 25
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
test: 1
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
configfile: "config.yaml" # does not exist, but this statement should be ignored on module import
2+
3+
4+
def some_func():
5+
return 15
6+
7+
8+
rule module1_a:
9+
output:
10+
temp("results/a1/{name}.out"),
11+
shell:
12+
"echo {config[test]} > {output}"
13+
14+
15+
rule module1_b:
16+
input:
17+
expand(rules.module1_a.output, name="test"),
18+
output:
19+
"results/b1/{name}.out",
20+
shell:
21+
"cat {input} > {output}"
22+
23+
24+
rule module1_c_tsv:
25+
input:
26+
expand(rules.module1_b.output, name="test"),
27+
output:
28+
"results/c1/{name}.tsv",
29+
shell:
30+
"cat {input} > {output}"
31+
32+
33+
use rule module1_c_tsv as module1_c_txt with:
34+
output:
35+
"results/c1/{name}.txt",
36+
37+
38+
rule module1_all:
39+
input:
40+
expand(rules.module1_c_tsv.output, name="test"),
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
configfile: "config.yaml" # does not exist, but this statement should be ignored on module import
2+
3+
4+
def some_func():
5+
return 25
6+
7+
8+
rule module2_a:
9+
output:
10+
temp("results/a2/{name}.out"),
11+
shell:
12+
"echo {config[test]} > {output}"
13+
14+
15+
rule module2_b:
16+
input:
17+
expand(rules.module2_a.output, name="test"),
18+
output:
19+
"results/b2/{name}.out",
20+
shell:
21+
"cat {input} > {output}"
22+
23+
24+
rule module2_c_tsv:
25+
input:
26+
expand(rules.module2_b.output, name="test"),
27+
output:
28+
"results/c2/{name}.tsv",
29+
shell:
30+
"cat {input} > {output}"
31+
32+
33+
use rule module2_c_tsv as module2_c_txt with:
34+
output:
35+
"results/c2/{name}.txt",
36+
37+
38+
rule module2_all:
39+
input:
40+
expand(rules.module2_c_tsv.output, name="test"),

tests/tests.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1464,6 +1464,10 @@ def test_modules_dynamic():
14641464
run(dpath("test_modules_dynamic"), targets=["all"])
14651465

14661466

1467+
def test_modules_dynamic_no_as():
1468+
run(dpath("test_modules_dynamic_no_as"), targets=["all"])
1469+
1470+
14671471
def test_module_nested():
14681472
run(dpath("test_module_nested"))
14691473

0 commit comments

Comments
 (0)