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
14 changes: 9 additions & 5 deletions flowrep/models/parsers/import_parser.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import ast
import importlib

from flowrep.models.parsers import object_scope
from flowrep.models.parsers.object_scope import ScopeProxy


def build_scope(
imports: list[ast.Import] | None = None,
import_froms: list[ast.ImportFrom] | None = None,
) -> dict:
) -> object_scope.ScopeProxy:
"""
Build a scope dictionary from a list of `import` and `from ... import ...` statements.

Expand All @@ -14,9 +17,10 @@ def build_scope(
import_froms (list | None): A list of `ast.ImportFrom` nodes.

Returns:
dict: A dictionary representing the scope with imported modules and objects.
object_scope.ScopeProxy: A mutable mapping representing the scope with imported
modules and objects.
"""
scope = {}
scope = ScopeProxy()

imports = imports or []
import_froms = import_froms or []
Expand All @@ -26,7 +30,7 @@ def build_scope(
for alias in imp.names:
asname = alias.asname or alias.name
module = importlib.import_module(alias.name)
scope[asname] = module
scope.register(name=asname, obj=module)

# Handle `from ... import ...` statements
for imp_from in import_froms:
Expand All @@ -42,6 +46,6 @@ def build_scope(
name = alias.name
asname = alias.asname or name
obj = getattr(module, name)
scope[asname] = obj
scope.register(name=asname, obj=obj)

return scope
8 changes: 6 additions & 2 deletions flowrep/models/parsers/object_scope.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@ class ScopeProxy(MutableMapping[str, object]):
By default, does not allow re-registration of existing symbols to new values.
"""

def __init__(self, d: MutableMapping[str, object], allow_overwrite: bool = False):
self._d = {k: v for k, v in d.items()}
def __init__(
self,
d: MutableMapping[str, object] | None = None,
allow_overwrite: bool = False,
):
self._d = {} if d is None else {k: v for k, v in d.items()}
self.allow_overwrite = allow_overwrite

def __getitem__(self, name: str):
Expand Down
4 changes: 4 additions & 0 deletions tests/unit/models/parsers/test_object_scope.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ def test_len(self):
proxy = object_scope.ScopeProxy({"a": 1, "b": 2})
self.assertEqual(len(proxy), 2)

def test_empty_construction(self):
proxy = object_scope.ScopeProxy()
self.assertEqual(len(proxy), 0)

def test_str(self):
proxy = object_scope.ScopeProxy({"x": 1})
self.assertEqual(str(proxy), "{'x': 1}")
Expand Down
Loading