Skip to content

Commit 1b356b3

Browse files
committed
Add deprecation warning for camelCase Alias
1 parent 0bb5563 commit 1b356b3

File tree

1 file changed

+28
-3
lines changed

1 file changed

+28
-3
lines changed

src/a2a/_base.py

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import warnings
2+
13
from typing import Any, ClassVar
24

35
from pydantic import BaseModel, ConfigDict
@@ -27,13 +29,13 @@ class A2ABaseModel(BaseModel):
2729
serves as the foundation for future extensions or shared utilities.
2830
2931
This implementation provides backward compatibility for camelCase aliases
30-
by lazy-loading an alias map upon first use.
32+
by lazy-loading an alias map upon first use. Accessing or setting
33+
attributes via their camelCase alias will raise a DeprecationWarning.
3134
"""
3235

3336
model_config = ConfigDict(
3437
# SEE: https://docs.pydantic.dev/latest/api/config/#pydantic.config.ConfigDict.populate_by_name
35-
validate_by_name=True,
36-
validate_by_alias=True,
38+
populate_by_name=True, # Recommended over validate_by_name/validate_by_alias
3739
serialize_by_alias=True,
3840
alias_generator=to_camel_custom,
3941
)
@@ -60,14 +62,37 @@ def __setattr__(self, name: str, value: Any) -> None:
6062
"""Allow setting attributes via their camelCase alias."""
6163
# Get the map and find the corresponding snake_case field name.
6264
field_name = type(self)._get_alias_map().get(name) # noqa: SLF001
65+
66+
if field_name:
67+
# An alias was used, issue a warning.
68+
warnings.warn(
69+
(
70+
f"Setting field '{name}' via its camelCase alias is deprecated and will be removed in version 0.3.0 "
71+
f"Use the snake_case name '{field_name}' instead."
72+
),
73+
DeprecationWarning,
74+
stacklevel=2,
75+
)
76+
6377
# If an alias was used, field_name will be set; otherwise, use the original name.
6478
super().__setattr__(field_name or name, value)
6579

6680
def __getattr__(self, name: str) -> Any:
6781
"""Allow getting attributes via their camelCase alias."""
6882
# Get the map and find the corresponding snake_case field name.
6983
field_name = type(self)._get_alias_map().get(name) # noqa: SLF001
84+
7085
if field_name:
86+
# An alias was used, issue a warning.
87+
warnings.warn(
88+
(
89+
f"Accessing field '{name}' via its camelCase alias is deprecated and will be removed in version 0.3.0 "
90+
f"Use the snake_case name '{field_name}' instead."
91+
),
92+
DeprecationWarning,
93+
stacklevel=2,
94+
)
95+
7196
# If an alias was used, retrieve the actual snake_case attribute.
7297
return getattr(self, field_name)
7398

0 commit comments

Comments
 (0)