Skip to content
Merged
Show file tree
Hide file tree
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
3 changes: 2 additions & 1 deletion src/flowrep/parsers/object_scope.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ def get_scope(func: Callable[..., Any] | type[Any]) -> ScopeProxy:
"inspect.getmodule() returned None and no resolvable __module__ "
"attribute was found."
)
return ScopeProxy(module.__dict__ | vars(builtins))
scope = module.__dict__ | getattr(func, "__globals__", {})
return ScopeProxy(scope | vars(builtins))


def resolve_attribute_to_object(attribute: str, scope: ScopeProxy | object) -> object:
Expand Down
18 changes: 18 additions & 0 deletions tests/unit/parsers/test_object_scope.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,24 @@ def test_returns_module_globals(self):
self.assertIs(scope.Outer, Outer)
self.assertIs(scope.object_scope, object_scope)

def test_prefers_function_globals_when_module_differs(self):
"""Resolve names from a function's globals when its module is unrelated."""
module = types.ModuleType("_test_unrelated_mod")
sys.modules[module.__name__] = module
marker = object()
try:
func = types.FunctionType(
(lambda: None).__code__,
{"marker": marker},
"_test_func",
)
func.__module__ = module.__name__
with patch.object(object_scope.inspect, "getmodule", return_value=module):
scope = object_scope.get_scope(func)
self.assertIs(scope.marker, marker)
finally:
del sys.modules[module.__name__]

def test_includes_builtins(self):
scope = object_scope.get_scope(add)
self.assertIs(scope.len, len)
Expand Down
Loading