Skip to content

Commit fe8beff

Browse files
committed
Use custom camelCase alias generator
1 parent e3d5057 commit fe8beff

File tree

3 files changed

+69
-82
lines changed

3 files changed

+69
-82
lines changed

scripts/generate_types.sh

100644100755
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ uv run datamodel-codegen \
3535
--use-subclass-enum \
3636
--base-class a2a._base.A2ABaseModel \
3737
--field-constraints \
38-
--snake-case-field
38+
--snake-case-field \
39+
--no-alias
3940

4041
echo "Formatting generated file with ruff..."
4142
uv run ruff format "$GENERATED_FILE"

src/a2a/_base.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,21 @@
11
from pydantic import BaseModel, ConfigDict
2+
from pydantic.alias_generators import to_camel
3+
4+
5+
def to_camel_custom(snake: str) -> str:
6+
"""Convert a snake_case string to camelCase.
7+
8+
Args:
9+
snake: The string to convert.
10+
11+
Returns:
12+
The converted camelCase string.
13+
"""
14+
# First, remove any trailing underscores. This is common for names that
15+
# conflict with Python keywords, like 'in_' or 'from_'.
16+
if snake.endswith('_'):
17+
snake = snake.rstrip('_')
18+
return to_camel(snake)
219

320

421
class A2ABaseModel(BaseModel):
@@ -13,4 +30,5 @@ class A2ABaseModel(BaseModel):
1330
validate_by_name=True,
1431
validate_by_alias=True,
1532
serialize_by_alias=True,
33+
alias_generator=to_camel_custom,
1634
)

0 commit comments

Comments
 (0)