Skip to content

Commit 3c260aa

Browse files
authored
Merge pull request #189 from pyiron/copilot/sub-pr-186
Add tests for get_scope with builtin types, classes, and methods
2 parents 092f66c + f9e61bf commit 3c260aa

2 files changed

Lines changed: 35 additions & 0 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ apidoc/
1111
.ipynb_checkpoints/
1212
test_times.dat
1313
core.*
14+
flowrep/_version.py

tests/unit/models/parsers/test_object_scope.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ def add(x: float = 2.0, y: float = 1) -> float:
1111
return x + y
1212

1313

14+
identity = lambda x: x # noqa: E731
15+
16+
1417
class Outer:
1518
class Inner:
1619
@staticmethod
@@ -86,6 +89,37 @@ def test_sys_modules_fallback_when_getmodule_returns_none(self):
8689
finally:
8790
del sys.modules["_test_fallback_mod"]
8891

92+
def test_builtin_type(self):
93+
"""get_scope works for a builtin type such as ``int``."""
94+
scope = object_scope.get_scope(int)
95+
# The builtins module is always merged in, so int and len must be present.
96+
self.assertIs(scope.int, int)
97+
self.assertIs(scope.len, len)
98+
99+
def test_builtin_function(self):
100+
"""get_scope works for a builtin function such as ``len``."""
101+
scope = object_scope.get_scope(len)
102+
self.assertIs(scope.len, len)
103+
self.assertIs(scope.int, int)
104+
105+
def test_user_defined_class(self):
106+
"""get_scope works for a user-defined class object."""
107+
scope = object_scope.get_scope(Outer)
108+
# Module-level names from this test module should be visible.
109+
self.assertIs(scope.Outer, Outer)
110+
self.assertIs(scope.add, add)
111+
112+
def test_static_method(self):
113+
"""get_scope works for a static method."""
114+
scope = object_scope.get_scope(Outer.Inner.nested_func)
115+
self.assertIs(scope.Outer, Outer)
116+
self.assertIs(scope.add, add)
117+
118+
def test_lambda(self):
119+
"""get_scope works for a module-level lambda."""
120+
scope = object_scope.get_scope(identity)
121+
self.assertIs(scope.identity, identity)
122+
89123
def test_no_resolvable_module_raises_value_error(self):
90124
"""When neither inspect.getmodule nor __module__ resolves, raise ValueError."""
91125
func = types.FunctionType(

0 commit comments

Comments
 (0)