From c98217a84e061f47aaeff9cee1f451697ae77eb1 Mon Sep 17 00:00:00 2001
From: Nick Crews <nicholas.b.crews@gmail.com>
Date: Tue, 5 Dec 2023 12:56:12 -0900
Subject: [PATCH 1/4] use caller's module as ID for task in `task()`

Possible fix for https://github.com/pytask-dev/pytask/issues/513

I didn't add any tests yet until I got confirmation this
is the right direction.
---
 src/_pytask/task_utils.py | 14 +++++---------
 1 file changed, 5 insertions(+), 9 deletions(-)

diff --git a/src/_pytask/task_utils.py b/src/_pytask/task_utils.py
index 59af53e3..d815321c 100644
--- a/src/_pytask/task_utils.py
+++ b/src/_pytask/task_utils.py
@@ -92,18 +92,11 @@ def wrapper(func: Callable[..., Any]) -> Callable[..., Any]:
                 )
                 raise ValueError(msg)
 
-        unwrapped = inspect.unwrap(func)
-
-        raw_path = inspect.getfile(unwrapped)
-        if "<string>" in raw_path:
-            path = Path(unwrapped.__globals__["__file__"]).absolute().resolve()
-        else:
-            path = Path(raw_path).absolute().resolve()
-
         parsed_kwargs = {} if kwargs is None else kwargs
         parsed_name = name if isinstance(name, str) else func.__name__
         parsed_after = _parse_after(after)
 
+        unwrapped = inspect.unwrap(func)
         if hasattr(unwrapped, "pytask_meta"):
             unwrapped.pytask_meta.name = parsed_name
             unwrapped.pytask_meta.kwargs = parsed_kwargs
@@ -123,7 +116,10 @@ def wrapper(func: Callable[..., Any]) -> Callable[..., Any]:
 
         # Store it in the global variable ``COLLECTED_TASKS`` to avoid garbage
         # collection when the function definition is overwritten in a loop.
-        COLLECTED_TASKS[path].append(unwrapped)
+        # Based on https://stackoverflow.com/questions/1095543/get-name-of-calling-functions-module-in-python  # noqa: E501
+        frm = inspect.stack()[1]
+        task_module = inspect.getmodule(frm.frame)
+        COLLECTED_TASKS[task_module.__file__].append(unwrapped)
 
         return unwrapped
 

From e6433668cb47f1d213d707231d46774fda60214a Mon Sep 17 00:00:00 2001
From: "pre-commit-ci[bot]"
 <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Date: Tue, 5 Dec 2023 21:57:26 +0000
Subject: [PATCH 2/4] [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci
---
 src/_pytask/task_utils.py | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/src/_pytask/task_utils.py b/src/_pytask/task_utils.py
index d815321c..0359084e 100644
--- a/src/_pytask/task_utils.py
+++ b/src/_pytask/task_utils.py
@@ -3,9 +3,8 @@
 
 import inspect
 from collections import defaultdict
-from pathlib import Path
 from typing import Any
-from typing import Callable
+from typing import Callable, TYPE_CHECKING
 
 import attrs
 from _pytask.mark import Mark
@@ -13,6 +12,9 @@
 from _pytask.shared import find_duplicates
 from _pytask.typing import is_task_function
 
+if TYPE_CHECKING:
+    from pathlib import Path
+
 
 __all__ = [
     "COLLECTED_TASKS",

From b33aa4babf969abc5cb62d793c151c6cc8c6d031 Mon Sep 17 00:00:00 2001
From: Nick Crews <nicholas.b.crews@gmail.com>
Date: Tue, 5 Dec 2023 13:01:36 -0900
Subject: [PATCH 3/4] fixup: use Path, not str for path of task

---
 src/_pytask/task_utils.py | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/src/_pytask/task_utils.py b/src/_pytask/task_utils.py
index 0359084e..53659a3f 100644
--- a/src/_pytask/task_utils.py
+++ b/src/_pytask/task_utils.py
@@ -121,7 +121,8 @@ def wrapper(func: Callable[..., Any]) -> Callable[..., Any]:
         # Based on https://stackoverflow.com/questions/1095543/get-name-of-calling-functions-module-in-python  # noqa: E501
         frm = inspect.stack()[1]
         task_module = inspect.getmodule(frm.frame)
-        COLLECTED_TASKS[task_module.__file__].append(unwrapped)
+        task_path = Path(task_module.__file__)
+        COLLECTED_TASKS[task_path].append(unwrapped)
 
         return unwrapped
 

From a813ebe494ce5986e4eec40462fa65973fe12449 Mon Sep 17 00:00:00 2001
From: "pre-commit-ci[bot]"
 <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Date: Tue, 5 Dec 2023 22:02:09 +0000
Subject: [PATCH 4/4] [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci
---
 src/_pytask/task_utils.py | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)

diff --git a/src/_pytask/task_utils.py b/src/_pytask/task_utils.py
index 53659a3f..586f4eca 100644
--- a/src/_pytask/task_utils.py
+++ b/src/_pytask/task_utils.py
@@ -4,16 +4,15 @@
 import inspect
 from collections import defaultdict
 from typing import Any
-from typing import Callable, TYPE_CHECKING
+from typing import Callable
 
 import attrs
 from _pytask.mark import Mark
 from _pytask.models import CollectionMetadata
 from _pytask.shared import find_duplicates
 from _pytask.typing import is_task_function
+from pathlib import Path
 
-if TYPE_CHECKING:
-    from pathlib import Path
 
 
 __all__ = [