Skip to content

Commit c98217a

Browse files
committed
use caller's module as ID for task in task()
Possible fix for pytask-dev#513 I didn't add any tests yet until I got confirmation this is the right direction.
1 parent 4b613df commit c98217a

File tree

1 file changed

+5
-9
lines changed

1 file changed

+5
-9
lines changed

src/_pytask/task_utils.py

+5-9
Original file line numberDiff line numberDiff line change
@@ -92,18 +92,11 @@ def wrapper(func: Callable[..., Any]) -> Callable[..., Any]:
9292
)
9393
raise ValueError(msg)
9494

95-
unwrapped = inspect.unwrap(func)
96-
97-
raw_path = inspect.getfile(unwrapped)
98-
if "<string>" in raw_path:
99-
path = Path(unwrapped.__globals__["__file__"]).absolute().resolve()
100-
else:
101-
path = Path(raw_path).absolute().resolve()
102-
10395
parsed_kwargs = {} if kwargs is None else kwargs
10496
parsed_name = name if isinstance(name, str) else func.__name__
10597
parsed_after = _parse_after(after)
10698

99+
unwrapped = inspect.unwrap(func)
107100
if hasattr(unwrapped, "pytask_meta"):
108101
unwrapped.pytask_meta.name = parsed_name
109102
unwrapped.pytask_meta.kwargs = parsed_kwargs
@@ -123,7 +116,10 @@ def wrapper(func: Callable[..., Any]) -> Callable[..., Any]:
123116

124117
# Store it in the global variable ``COLLECTED_TASKS`` to avoid garbage
125118
# collection when the function definition is overwritten in a loop.
126-
COLLECTED_TASKS[path].append(unwrapped)
119+
# Based on https://stackoverflow.com/questions/1095543/get-name-of-calling-functions-module-in-python # noqa: E501
120+
frm = inspect.stack()[1]
121+
task_module = inspect.getmodule(frm.frame)
122+
COLLECTED_TASKS[task_module.__file__].append(unwrapped)
127123

128124
return unwrapped
129125

0 commit comments

Comments
 (0)