Skip to content

Commit

Permalink
lf.structured.class_dependencies to support cyclic dependencies.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 566806521
  • Loading branch information
daiyip authored and langfun authors committed Sep 22, 2023
1 parent 8e2d7a4 commit 9475b12
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
5 changes: 3 additions & 2 deletions langfun/core/structured/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,13 +188,14 @@ def _add_dependency(cls_or_classes):
if isinstance(cls_or_classes, type):
cls_or_classes = [cls_or_classes]
for cls in cls_or_classes:
if cls not in seen:
if cls not in dependencies:
dependencies.append(cls)
seen.add(cls)

def _fill_dependencies(vs: pg.typing.ValueSpec, include_subclasses: bool):
if isinstance(vs, pg.typing.Object):
if issubclass(vs.cls, pg.Object) and vs.cls not in seen:
seen.add(vs.cls)

# Add base classes as dependencies.
for base_cls in vs.cls.__bases__:
# We only keep track of user-defined symbolic classes.
Expand Down
10 changes: 10 additions & 0 deletions langfun/core/structured/schema_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ class Itinerary(pg.Object):
hotel: pg.typing.Str['.*Hotel'] | None


class Node(pg.Object):
children: list['Node']


class SchemaTest(unittest.TestCase):

def assert_schema(self, annotation, spec):
Expand Down Expand Up @@ -201,6 +205,12 @@ class B(A):
with self.assertRaisesRegex(TypeError, 'Unsupported spec type'):
schema_lib.class_dependencies((Foo, 1))

def test_class_dependencies_recursive(self):
self.assertEqual(
schema_lib.class_dependencies(Node),
[Node]
)

def test_class_dependencies_from_value(self):
class Foo(pg.Object):
x: int
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
jinja2>=3.1.2
openai>=0.18.1
pyglove>=0.4.3
pyglove>=0.4.4.dev20230922
termcolor==1.1.0

0 comments on commit 9475b12

Please sign in to comment.