Skip to content

Commit 74c0fa7

Browse files
author
Paolo Tranquilli
committed
Codegen: allow annotations to add class decorations
1 parent cc5882a commit 74c0fa7

File tree

2 files changed

+43
-5
lines changed

2 files changed

+43
-5
lines changed

misc/codegen/lib/schemadefs.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,9 @@ def modify(self, prop: _schema.Property):
2020
def negate(self) -> _schema.PropertyModifier:
2121
return _ChildModifier(False)
2222

23-
# make ~doc same as doc(None)
24-
2523

2624
class _DocModifierMetaclass(type(_schema.PropertyModifier)):
25+
# make ~doc same as doc(None)
2726
def __invert__(self) -> _schema.PropertyModifier:
2827
return _DocModifier(None)
2928

@@ -41,8 +40,8 @@ def negate(self) -> _schema.PropertyModifier:
4140
return _DocModifier(None)
4241

4342

44-
# make ~desc same as desc(None)
4543
class _DescModifierMetaclass(type(_schema.PropertyModifier)):
44+
# make ~desc same as desc(None)
4645
def __invert__(self) -> _schema.PropertyModifier:
4746
return _DescModifier(None)
4847

@@ -249,7 +248,18 @@ def annotate(annotated_cls: type) -> _Callable[[type], _PropertyAnnotation]:
249248
def decorator(cls: type) -> _PropertyAnnotation:
250249
if cls.__name__ != "_":
251250
raise _schema.Error("Annotation classes must be named _")
252-
annotated_cls.__doc__ = cls.__doc__
251+
if cls.__doc__ is not None:
252+
annotated_cls.__doc__ = cls.__doc__
253+
old_pragmas = getattr(annotated_cls, "_pragmas", None)
254+
new_pragmas = getattr(cls, "_pragmas", [])
255+
if old_pragmas:
256+
old_pragmas.extend(new_pragmas)
257+
else:
258+
annotated_cls._pragmas = new_pragmas
259+
for a, v in cls.__dict__.items():
260+
# transfer annotations
261+
if a.startswith("_") and not a.startswith("__") and a != "_pragmas":
262+
setattr(annotated_cls, a, v)
253263
for p, a in cls.__annotations__.items():
254264
if p in annotated_cls.__annotations__:
255265
annotated_cls.__annotations__[p] |= a

misc/codegen/test/test_schemaloader.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -763,15 +763,43 @@ class data:
763763
class Root:
764764
""" old docstring """
765765

766+
class A(Root):
767+
""" A docstring """
768+
766769
@defs.annotate(Root)
767770
class _:
768771
"""
769772
new
770773
docstring
771774
"""
772775

776+
@defs.annotate(A)
777+
class _:
778+
pass
779+
780+
assert data.classes == {
781+
"Root": schema.Class("Root", doc=["new", "docstring"], derived={"A"}),
782+
"A": schema.Class("A", bases=["Root"], doc=["A docstring"]),
783+
}
784+
785+
786+
def test_annotate_decorations():
787+
@load
788+
class data:
789+
@defs.qltest.skip
790+
class Root:
791+
pass
792+
793+
@defs.annotate(Root)
794+
@defs.qltest.collapse_hierarchy
795+
@defs.ql.hideable
796+
@defs.cpp.skip
797+
class _:
798+
pass
799+
773800
assert data.classes == {
774-
"Root": schema.Class("Root", doc=["new", "docstring"]),
801+
"Root": schema.Class("Root", hideable=True,
802+
pragmas=["qltest_skip", "cpp_skip", "qltest_collapse_hierarchy"]),
775803
}
776804

777805

0 commit comments

Comments
 (0)