Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "Codegen: allow to include .py files in schema.py" #17524

Merged
merged 1 commit into from
Sep 19, 2024
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
13 changes: 3 additions & 10 deletions misc/codegen/lib/schemadefs.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,9 @@ def modify(self, prop: _schema.Property):


def include(source: str):
scope = _inspect.currentframe().f_back.f_locals
if source.endswith(".dbscheme"):
# add to `includes` variable in calling context
scope.setdefault("__includes", []).append(source)
elif source.endswith(".py"):
# just load the contents
with open(source) as input:
exec(input.read(), scope)
else:
raise _schema.Error(f"Unsupported file for inclusion: {source}")
# add to `includes` variable in calling context
_inspect.currentframe().f_back.f_locals.setdefault(
"__includes", []).append(source)


class _Namespace:
Expand Down
2 changes: 1 addition & 1 deletion misc/codegen/loaders/schemaloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ def _check_test_with(classes: typing.Dict[str, schema.Class]):


def load(m: types.ModuleType) -> schema.Schema:
includes = []
includes = set()
classes = {}
known = {"int", "string", "boolean"}
known.update(n for n in m.__dict__ if not n.startswith("__"))
Expand Down
48 changes: 1 addition & 47 deletions misc/codegen/test/test_schemaloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class data:
pass

assert data.classes == {}
assert data.includes == []
assert data.includes == set()
assert data.null is None
assert data.null_class is None

Expand Down Expand Up @@ -838,51 +838,5 @@ class C(Root):
pass


def test_include_dbscheme():
@load
class data:
defs.include("foo.dbscheme")
defs.include("bar.dbscheme")

assert data.includes == ["foo.dbscheme", "bar.dbscheme"]


def test_include_source(tmp_path):
(tmp_path / "foo.py").write_text("""
class A(Root):
pass
""")
(tmp_path / "bar.py").write_text("""
class C(Root):
pass
""")

@load
class data:
class Root:
pass

defs.include(str(tmp_path / "foo.py"))

class B(Root):
pass

defs.include(str(tmp_path / "bar.py"))

assert data.classes == {
"Root": schema.Class("Root", derived=set("ABC")),
"A": schema.Class("A", bases=["Root"]),
"B": schema.Class("B", bases=["Root"]),
"C": schema.Class("C", bases=["Root"]),
}


def test_include_not_supported(tmp_path):
with pytest.raises(schema.Error):
@load
class data:
defs.include("foo.bar")


if __name__ == '__main__':
sys.exit(pytest.main([__file__] + sys.argv[1:]))
Loading