Skip to content

Commit

Permalink
Fix dynamic generation in python (#638)
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronvg authored Jun 3, 2024
1 parent be20ecf commit a6aed34
Show file tree
Hide file tree
Showing 13 changed files with 373 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,22 @@ class TypeBuilder(_TypeBuilder):
[{% for enum in enums %}"{{enum.name}}",{% endfor %}]
))

{%- for cls in classes %}{% if cls.dynamic %}
{% for cls in classes %}{% if cls.dynamic %}

@property
def {{cls.name}}(self) -> "{{cls.name}}Builder":
return {{cls.name}}Builder(self)
{%- endif %}{% endfor %}
{% endif %}{% endfor %}

{%- for enum in enums %}{% if enum.dynamic %}
{% for enum in enums %}{% if enum.dynamic %}

@property
def {{enum.name}}(self) -> "{{enum.name}}Builder":
return {{enum.name}}Builder(self)
{% endif %}{% endfor %}

{% for cls in classes %}{% if cls.dynamic -%}

class {{cls.name}}Builder:
def __init__(self, tb: _TypeBuilder):
self.__bldr = tb._tb.class_("{{cls.name}}")
Expand All @@ -50,7 +51,7 @@ class {{cls.name}}Properties:
self.__bldr = cls_bldr
self.__properties = properties

{%- for (name, type) in cls.fields %}
{% for (name, type) in cls.fields %}

@property
def {{name}}(self) -> ClassPropertyBuilder:
Expand All @@ -61,7 +62,7 @@ class {{cls.name}}Properties:
if name not in self.__properties:
raise AttributeError(f"Property {name} not found.")
return ClassPropertyBuilder(self.__bldr.property(name))
{%- endif %}{% endfor %}
{% endif %}{% endfor %}

{% for enum in enums %}{% if enum.dynamic %}
class {{enum.name}}Builder:
Expand Down Expand Up @@ -91,17 +92,17 @@ class {{enum.name}}Values:
self.__bldr = enum_bldr
self.__values = values

{%- for value in enum.values %}
{% for value in enum.values %}

@property
def {{value}}(self) -> EnumValueBuilder:
return self.__bldr.value("{{value}}")
{%- endfor %}
{% endfor %}

def __getattr__(self, name: str) -> EnumValueBuilder:
if name not in self.__values:
raise AttributeError(f"Value {name} not found.")
return self.__bldr.value(name)
{%- endif %}{% endfor %}
{% endif %}{% endfor %}

__all__ = ["TypeBuilder"]
2 changes: 1 addition & 1 deletion engine/language_client_python/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "baml-py"
version = "0.33.0"
version = "0.33.1"
description = "BAML python bindings (pyproject.toml)"
readme = "README.md"
authors = [["Boundary", "[email protected]"]]
Expand Down
25 changes: 25 additions & 0 deletions integ-tests/baml_src/test-files/dynamic/dynamic.baml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class DynamicClassOne {
hi string
@@dynamic
}

enum DynEnumOne {
@@dynamic
}

enum DynEnumTwo {
@@dynamic
}

class SomeClassNestedDynamic {
hi string
@@dynamic

}

class DynamicClassTwo {
hi string
some_class SomeClassNestedDynamic
status DynEnumOne
@@dynamic
}
2 changes: 1 addition & 1 deletion integ-tests/baml_src/test-files/functions/v2/basic.baml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@


function ExtractResume2(resume: string) -> Resume {
client Resilient_ComplexSyntax
client GPT4
prompt #"
{{ _.role('system') }}

Expand Down
5 changes: 3 additions & 2 deletions integ-tests/python/baml_client/inlinedbaml.py

Large diffs are not rendered by default.

11 changes: 11 additions & 0 deletions integ-tests/python/baml_client/partial_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ class ClassWithImage(BaseModel):
param2: Optional[str] = None
fake_image: "FakeImage"

class DynamicClassOne(BaseModel):
hi: Optional[str] = None

class DynamicClassTwo(BaseModel):
hi: Optional[str] = None
some_class: "SomeClassNestedDynamic"
status: Optional[Union[types.DynEnumOne, str]] = None

class Education(BaseModel):
institution: Optional[str] = None
location: Optional[str] = None
Expand Down Expand Up @@ -109,6 +117,9 @@ class SearchParams(BaseModel):
description: List["WithReasoning"]
tags: List[Optional[Union[Optional[types.Tag], Optional[str]]]]

class SomeClassNestedDynamic(BaseModel):
hi: Optional[str] = None

class TestClassAlias(BaseModel):
key: Optional[str] = None
key2: Optional[str] = None
Expand Down
Loading

0 comments on commit a6aed34

Please sign in to comment.