Skip to content

Commit e66a462

Browse files
Don't allow parameters with checks/asserts (#1689)
Fix #1578 <!-- ELLIPSIS_HIDDEN --> ---- > [!IMPORTANT] > Disallow types with checks/asserts as function parameters and update client files to reflect this change. > > - **Behavior**: > - Disallow types with checks/asserts as function parameters in `functions.rs` by updating `has_checks_nested()` to handle `TypeAliasId` and `TypeExpId`. > - Add test `check_in_parameter_type_alias.baml` to verify disallowed types with checks as parameters. > - **Client Updates**: > - Update function signatures in `async_client.py`, `sync_client.py`, and `async_client.ts` to remove checks from parameters. > - Modify `openapi.yaml` to reflect changes in parameter types. > - **Misc**: > - Rename `visited` to `visited_classes` and add `visited_type_aliases` in `NestedChecks` struct in `functions.rs`. > > <sup>This description was created by </sup>[<img alt="Ellipsis" src="https://img.shields.io/badge/Ellipsis-blue?color=175173">](https://www.ellipsis.dev?ref=BoundaryML%2Fbaml&utm_source=github&utm_medium=referral)<sup> for 3902b35. It will automatically update as commits are pushed.</sup> <!-- ELLIPSIS_HIDDEN -->
1 parent 44346ae commit e66a462

25 files changed

Lines changed: 107 additions & 104 deletions

File tree

engine/baml-lib/baml-core/src/validate/validation_pipeline/validations/functions.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ use crate::validate::validation_pipeline::context::Context;
55
use internal_baml_diagnostics::{DatamodelError, DatamodelWarning, Span};
66

77
use internal_baml_parser_database::TypeWalker;
8-
use internal_baml_schema_ast::ast::{FieldType, TypeExpId, WithIdentifier, WithName, WithSpan};
8+
use internal_baml_schema_ast::ast::{
9+
FieldType, TypeAliasId, TypeExpId, WithIdentifier, WithName, WithSpan,
10+
};
911

1012
use super::types::validate_type;
1113

@@ -227,14 +229,16 @@ pub(super) fn has_checks_nested(ctx: &Context<'_>, field_type: &FieldType) -> bo
227229

228230
struct NestedChecks<'c> {
229231
ctx: &'c Context<'c>,
230-
visited: HashSet<TypeExpId>,
232+
visited_classes: HashSet<TypeExpId>,
233+
visited_type_aliases: HashSet<TypeAliasId>,
231234
}
232235

233236
impl<'c> NestedChecks<'c> {
234237
fn new(ctx: &'c Context<'c>) -> Self {
235238
Self {
236239
ctx,
237-
visited: HashSet::new(),
240+
visited_classes: HashSet::new(),
241+
visited_type_aliases: HashSet::new(),
238242
}
239243
}
240244

@@ -249,7 +253,7 @@ impl<'c> NestedChecks<'c> {
249253
FieldType::Symbol(_, id, ..) => match self.ctx.db.find_type(id) {
250254
Some(TypeWalker::Class(class_walker)) => {
251255
// Stop recursion when dealing with recursive types.
252-
if !self.visited.insert(class_walker.id) {
256+
if !self.visited_classes.insert(class_walker.id) {
253257
return false;
254258
}
255259

@@ -262,6 +266,14 @@ impl<'c> NestedChecks<'c> {
262266
.map_or(false, |ft| self.has_checks_nested(ft))
263267
})
264268
}
269+
Some(TypeWalker::TypeAlias(type_alias_walker)) => {
270+
if !self.visited_type_aliases.insert(type_alias_walker.id) {
271+
return false;
272+
}
273+
274+
let resolved = type_alias_walker.resolved();
275+
self.has_checks_nested(resolved)
276+
}
265277
_ => false,
266278
},
267279

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// A type alias to a type with a check.
2+
type MultipleAttrs = int @assert({{ this > 0 }}) @check(gt_ten, {{ this > 10 }})
3+
4+
// That alias is used as a parameter.
5+
function AliasWithMultipleAttrs(money: MultipleAttrs) -> MultipleAttrs {
6+
client "openai/gpt-4o"
7+
prompt r#"
8+
Return the given integer without additional context:
9+
10+
{{ money }}
11+
12+
{{ ctx.output_format }}
13+
"#
14+
}
15+
16+
// error: Error validating: Types with checks are not allowed as function parameters.
17+
// --> functions_v2/check_in_parameter_type_alias.baml:5
18+
// |
19+
// 4 | // That alias is used as a parameter.
20+
// 5 | function AliasWithMultipleAttrs(money: MultipleAttrs) -> MultipleAttrs {
21+
// |

integ-tests/baml_src/test-files/functions/output/type-aliases.baml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ function MergeAliasAttributes(money: int) -> MergeAttrs {
5757
"#
5858
}
5959

60-
function ReturnAliasWithMergedAttributes(money: Amount) -> Amount {
60+
function ReturnAliasWithMergedAttributes(money: int) -> Amount {
6161
client "openai/gpt-4o"
6262
prompt r#"
6363
Return the given integer without additional context:
@@ -68,7 +68,7 @@ function ReturnAliasWithMergedAttributes(money: Amount) -> Amount {
6868
"#
6969
}
7070

71-
function AliasWithMultipleAttrs(money: MultipleAttrs) -> MultipleAttrs {
71+
function AliasWithMultipleAttrs(money: int) -> MultipleAttrs {
7272
client "openai/gpt-4o"
7373
prompt r#"
7474
Return the given integer without additional context:

integ-tests/openapi/baml_client/openapi.yaml

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

integ-tests/python/baml_client/async_client.py

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

integ-tests/python/baml_client/async_request.py

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

integ-tests/python/baml_client/inlinedbaml.py

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

0 commit comments

Comments
 (0)