Skip to content

Commit 67f11e6

Browse files
committed
get_calling_module_name() now works through import
1 parent 4178686 commit 67f11e6

File tree

4 files changed

+18
-10
lines changed

4 files changed

+18
-10
lines changed

src/omnipy/util/helpers.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,8 +242,10 @@ def recursive_module_import(module: ModuleType, imported_modules: list[ModuleTyp
242242

243243
def get_calling_module_name() -> str | None:
244244
stack = inspect.stack()
245-
if len(stack) >= 3:
246-
grandparent_frame = inspect.stack()[2][0]
245+
start_frame_index = 2
246+
while len(stack) > start_frame_index:
247+
grandparent_frame = stack[start_frame_index][0]
247248
module = inspect.getmodule(grandparent_frame)
248249
if module is not None:
249250
return module.__name__
251+
start_frame_index += 1

tests/util/helpers/__init__.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +0,0 @@
1-
from omnipy.util.helpers import get_calling_module_name
2-
3-
4-
# For test_helpers::test_get_calling_module_name
5-
def other_module_call_get_calling_module_name() -> str:
6-
return get_calling_module_name()

tests/util/helpers/other_module.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from omnipy.util.helpers import get_calling_module_name
2+
3+
4+
# For test_helpers::test_get_calling_module_name
5+
def other_module_call_get_calling_module_name() -> str:
6+
return get_calling_module_name()
7+
8+
9+
calling_module_name_when_importing_other_module = get_calling_module_name()

tests/util/test_helpers.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@
2323
RestorableContents,
2424
transfer_generic_args_to_cls)
2525

26-
from .helpers import other_module_call_get_calling_module_name
27-
2826
T = TypeVar('T')
2927
U = TypeVar('U')
3028

@@ -409,5 +407,10 @@ def test_get_calling_module_name() -> None:
409407
def local_call_get_calling_module_name() -> str:
410408
return get_calling_module_name()
411409

410+
from .helpers.other_module import (calling_module_name_when_importing_other_module,
411+
other_module_call_get_calling_module_name)
412+
412413
assert local_call_get_calling_module_name() == 'tests.util.test_helpers'
413414
assert other_module_call_get_calling_module_name() == 'tests.util.test_helpers'
415+
assert calling_module_name_when_importing_other_module == 'tests.util.test_helpers'
416+

0 commit comments

Comments
 (0)