Skip to content

Commit 1d9767a

Browse files
authored
Merge pull request github#17770 from github/redsun82/rust-callable-base
Rust: Add `Callable` as a base class of `Function` and `ClosureExpr`
2 parents 50ec254 + bd08bc7 commit 1d9767a

File tree

24 files changed

+364
-209
lines changed

24 files changed

+364
-209
lines changed

misc/codegen/lib/schemadefs.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from typing import (
22
Callable as _Callable,
33
Dict as _Dict,
4+
Iterable as _Iterable,
45
ClassVar as _ClassVar,
56
)
67
from misc.codegen.lib import schema as _schema
@@ -278,7 +279,7 @@ def __or__(self, other: _schema.PropertyModifier):
278279
drop = object()
279280

280281

281-
def annotate(annotated_cls: type, replace_bases: _Dict[type, type] | None = None) -> _Callable[[type], _PropertyAnnotation]:
282+
def annotate(annotated_cls: type, add_bases: _Iterable[type] | None = None, replace_bases: _Dict[type, type] | None = None) -> _Callable[[type], _PropertyAnnotation]:
282283
"""
283284
Add or modify schema annotations after a class has been defined previously.
284285
@@ -295,6 +296,8 @@ def decorator(cls: type) -> _PropertyAnnotation:
295296
_ClassPragma(p, value=v)(annotated_cls)
296297
if replace_bases:
297298
annotated_cls.__bases__ = tuple(replace_bases.get(b, b) for b in annotated_cls.__bases__)
299+
if add_bases:
300+
annotated_cls.__bases__ += tuple(add_bases)
298301
for a in dir(cls):
299302
if a.startswith(_schema.inheritable_pragma_prefix):
300303
setattr(annotated_cls, a, getattr(cls, a))

misc/codegen/test/test_schemaloader.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -914,6 +914,36 @@ class _:
914914
}
915915

916916

917+
def test_annotate_add_bases():
918+
@load
919+
class data:
920+
class Root:
921+
pass
922+
923+
class A(Root):
924+
pass
925+
926+
class B(Root):
927+
pass
928+
929+
class C(Root):
930+
pass
931+
932+
class Derived(A):
933+
pass
934+
935+
@defs.annotate(Derived, add_bases=(B, C))
936+
class _:
937+
pass
938+
assert data.classes == {
939+
"Root": schema.Class("Root", derived={"A", "B", "C"}),
940+
"A": schema.Class("A", bases=["Root"], derived={"Derived"}),
941+
"B": schema.Class("B", bases=["Root"], derived={"Derived"}),
942+
"C": schema.Class("C", bases=["Root"], derived={"Derived"}),
943+
"Derived": schema.Class("Derived", bases=["A", "B", "C"]),
944+
}
945+
946+
917947
def test_annotate_drop_field():
918948
@load
919949
class data:

rust/extractor/src/generated/.generated.list

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rust/extractor/src/generated/top.rs

Lines changed: 67 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)