Skip to content

Commit cf5d56a

Browse files
authored
Merge pull request #17524 from github/revert-17514-redsun82/codegen-include
Revert "Codegen: allow to include `.py` files in `schema.py`"
2 parents a065434 + 97cca76 commit cf5d56a

File tree

3 files changed

+5
-58
lines changed

3 files changed

+5
-58
lines changed

misc/codegen/lib/schemadefs.py

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,9 @@ def modify(self, prop: _schema.Property):
3232

3333

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

4639

4740
class _Namespace:

misc/codegen/loaders/schemaloader.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ def _check_test_with(classes: typing.Dict[str, schema.Class]):
127127

128128

129129
def load(m: types.ModuleType) -> schema.Schema:
130-
includes = []
130+
includes = set()
131131
classes = {}
132132
known = {"int", "string", "boolean"}
133133
known.update(n for n in m.__dict__ if not n.startswith("__"))

misc/codegen/test/test_schemaloader.py

Lines changed: 1 addition & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class data:
1313
pass
1414

1515
assert data.classes == {}
16-
assert data.includes == []
16+
assert data.includes == set()
1717
assert data.null is None
1818
assert data.null_class is None
1919

@@ -838,51 +838,5 @@ class C(Root):
838838
pass
839839

840840

841-
def test_include_dbscheme():
842-
@load
843-
class data:
844-
defs.include("foo.dbscheme")
845-
defs.include("bar.dbscheme")
846-
847-
assert data.includes == ["foo.dbscheme", "bar.dbscheme"]
848-
849-
850-
def test_include_source(tmp_path):
851-
(tmp_path / "foo.py").write_text("""
852-
class A(Root):
853-
pass
854-
""")
855-
(tmp_path / "bar.py").write_text("""
856-
class C(Root):
857-
pass
858-
""")
859-
860-
@load
861-
class data:
862-
class Root:
863-
pass
864-
865-
defs.include(str(tmp_path / "foo.py"))
866-
867-
class B(Root):
868-
pass
869-
870-
defs.include(str(tmp_path / "bar.py"))
871-
872-
assert data.classes == {
873-
"Root": schema.Class("Root", derived=set("ABC")),
874-
"A": schema.Class("A", bases=["Root"]),
875-
"B": schema.Class("B", bases=["Root"]),
876-
"C": schema.Class("C", bases=["Root"]),
877-
}
878-
879-
880-
def test_include_not_supported(tmp_path):
881-
with pytest.raises(schema.Error):
882-
@load
883-
class data:
884-
defs.include("foo.bar")
885-
886-
887841
if __name__ == '__main__':
888842
sys.exit(pytest.main([__file__] + sys.argv[1:]))

0 commit comments

Comments
 (0)