|
1 | 1 | import ast |
| 2 | +import sys |
| 3 | +import types |
2 | 4 | import unittest |
3 | 5 |
|
4 | 6 | from flowrep.models.parsers import object_scope |
@@ -42,6 +44,36 @@ def test_includes_builtins(self): |
42 | 44 | self.assertIs(scope.int, int) |
43 | 45 | self.assertIs(scope.ValueError, ValueError) |
44 | 46 |
|
| 47 | + def test_none_module_fallback_via_dunder_module(self): |
| 48 | + """When inspect.getmodule returns None but __module__ is set, fall back.""" |
| 49 | + mod = types.ModuleType("_test_dynamic_mod") |
| 50 | + mod.__dict__["sentinel"] = object() |
| 51 | + sys.modules["_test_dynamic_mod"] = mod |
| 52 | + try: |
| 53 | + func = types.FunctionType( |
| 54 | + (lambda: None).__code__, |
| 55 | + {}, |
| 56 | + "_test_func", |
| 57 | + ) |
| 58 | + # Manually set __module__ but keep the function out of a real module |
| 59 | + # so inspect.getmodule() returns None. |
| 60 | + func.__module__ = "_test_dynamic_mod" |
| 61 | + scope = object_scope.get_scope(func) |
| 62 | + self.assertIs(scope.sentinel, mod.__dict__["sentinel"]) |
| 63 | + finally: |
| 64 | + del sys.modules["_test_dynamic_mod"] |
| 65 | + |
| 66 | + def test_no_resolvable_module_raises_value_error(self): |
| 67 | + """When neither inspect.getmodule nor __module__ resolves, raise ValueError.""" |
| 68 | + func = types.FunctionType( |
| 69 | + (lambda: None).__code__, |
| 70 | + {}, |
| 71 | + "_orphan_func", |
| 72 | + ) |
| 73 | + func.__module__ = None # type: ignore[assignment] |
| 74 | + with self.assertRaises(ValueError): |
| 75 | + object_scope.get_scope(func) |
| 76 | + |
45 | 77 |
|
46 | 78 | class TestResolveSymbolToObject(unittest.TestCase): |
47 | 79 | def test_simple_name(self): |
|
0 commit comments