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

Codegen: allow to attach docstrings after the definition #17516

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
16 changes: 16 additions & 0 deletions misc/codegen/lib/schemadefs.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,3 +170,19 @@ def group(name: str = "") -> _ClassDecorator:
from_class=_schema.get_type_name(ref)))
synth.on_arguments = lambda **kwargs: _annotate(
synth=_schema.SynthInfo(on_arguments={k: _schema.get_type_name(t) for k, t in kwargs.items()}))


def annotate(annotated_cls: type) -> _Callable[[type], None]:
"""
Add or modify schema annotations after a class has been defined
For the moment, only docstring annotation is supported. In the future, any kind of
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be great to also support the QL test function signature annotation.

modification will be allowed.

The name of the class used for annotation must be `_`
"""
def decorator(cls: type) -> None:
if cls.__name__ != "_":
raise _schema.Error("Annotation classes must be named _")
annotated_cls.__doc__ = cls.__doc__
return None
return decorator
2 changes: 1 addition & 1 deletion misc/codegen/loaders/schemaloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ def load(m: types.ModuleType) -> schema.Schema:
if name == "__includes":
includes = data
continue
if name.startswith("__"):
if name.startswith("__") or name == "_":
continue
cls = _get_class(data)
if classes and not cls.bases:
Expand Down
33 changes: 33 additions & 0 deletions misc/codegen/test/test_schemaloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -756,6 +756,39 @@ class D(Root):
}


def test_annotate_docstring():
@load
class data:
class Root:
""" old docstring """

@defs.annotate(Root)
class _:
"""
new
docstring
"""

assert data.classes == {
"Root": schema.Class("Root", doc=["new", "docstring"]),
}


def test_annotate_not_underscore():
with pytest.raises(schema.Error):
@load
class data:
class Root:
pass

@defs.annotate(Root)
class Something:
"""
new
docstring
"""


def test_test_with_unknown_string():
with pytest.raises(schema.Error):
@load
Expand Down
Loading