Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions tests/unit/models/parsers/test_object_scope.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import sys
import types
import unittest
from unittest.mock import patch

from flowrep.models.parsers import object_scope

Expand Down Expand Up @@ -63,6 +64,28 @@ def test_none_module_fallback_via_dunder_module(self):
finally:
del sys.modules["_test_dynamic_mod"]

def test_sys_modules_fallback_when_getmodule_returns_none(self):
"""Cover line 53: sys.modules.get(module_name) is reached when inspect.getmodule
returns None but the object's __module__ is registered in sys.modules."""
mod = types.ModuleType("_test_fallback_mod")
mod.__dict__["marker"] = object()
sys.modules["_test_fallback_mod"] = mod
try:
func = types.FunctionType(
(lambda: None).__code__,
{},
"_test_func",
)
func.__module__ = "_test_fallback_mod"
# Patch inspect.getmodule to return None, simulating objects (e.g.
# C-extension types) where the module cannot be determined from the
# object directly, so the fallback via sys.modules is exercised.
with patch.object(object_scope.inspect, "getmodule", return_value=None):
scope = object_scope.get_scope(func)
self.assertIs(scope.marker, mod.__dict__["marker"])
finally:
del sys.modules["_test_fallback_mod"]

def test_no_resolvable_module_raises_value_error(self):
"""When neither inspect.getmodule nor __module__ resolves, raise ValueError."""
func = types.FunctionType(
Expand Down
Loading