Skip to content

Commit c94363e

Browse files
author
Oleksandr Bazarnov
committed
updated the BaseModelWithDeprecations class
1 parent 9651c8e commit c94363e

File tree

1 file changed

+31
-22
lines changed

1 file changed

+31
-22
lines changed

airbyte_cdk/sources/declarative/models/base_model_with_deprecations.py

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -46,48 +46,57 @@ class Config:
4646

4747
extra = "allow"
4848

49-
def __init__(self, **model_data: Any) -> None:
49+
def __init__(self, **model_fields: Any) -> None:
5050
"""
5151
Show warnings for deprecated fields during component initialization.
5252
"""
53-
5453
# call the parent constructor first to initialize Pydantic internals
55-
super().__init__(**model_data)
56-
54+
super().__init__(**model_fields)
5755
# set the placeholder for the deprecation logs
5856
self._deprecation_logs: List[AirbyteLogMessage] = []
59-
6057
# process deprecated fields, if present
61-
self._process_fields(model_data)
62-
58+
self._process_fields(model_fields)
6359
# set the deprecation logs attribute to the model
6460
self._set_deprecation_logs_attr_to_model()
6561

66-
def _process_fields(self, model_data: Any) -> None:
62+
def _is_deprecated_field(self, field_name: str) -> bool:
63+
return (
64+
self.__fields__[field_name].field_info.extra.get(DEPRECATED, False)
65+
if field_name in self.__fields__.keys()
66+
else False
67+
)
68+
69+
def _get_deprecation_message(self, field_name: str) -> str:
70+
return (
71+
self.__fields__[field_name].field_info.extra.get(
72+
DEPRECATION_MESSAGE, "<missing_deprecation_message>"
73+
)
74+
if field_name in self.__fields__.keys()
75+
else "<missing_deprecation_message>"
76+
)
77+
78+
def _process_fields(self, model_fields: Any) -> None:
6779
"""
6880
Processes the fields in the provided model data, checking for deprecated fields.
6981
70-
For each field in the input `model_data`, this method checks if the field exists in the model's defined fields.
82+
For each field in the input `model_fields`, this method checks if the field exists in the model's defined fields.
7183
If the field is marked as deprecated (using the `DEPRECATED` flag in its metadata), it triggers a deprecation warning
72-
by calling the `_deprecated_warning` method with the field name and an optional deprecation message.
84+
by calling the `_create_warning` method with the field name and an optional deprecation message.
7385
7486
Args:
75-
model_data (Any): The data containing fields to be processed.
87+
model_fields (Any): The data containing fields to be processed.
7688
7789
Returns:
7890
None
7991
"""
80-
model_fields = self.__fields__
81-
for field_name in model_data.keys():
82-
if field_name in model_fields:
83-
is_deprecated_field = model_fields[field_name].field_info.extra.get(
84-
DEPRECATED, False
85-
)
86-
if is_deprecated_field:
87-
deprecation_message = model_fields[field_name].field_info.extra.get(
88-
DEPRECATION_MESSAGE, ""
92+
93+
if hasattr(self, FIELDS_TAG):
94+
for field_name in model_fields.keys():
95+
if self._is_deprecated_field(field_name):
96+
self._create_warning(
97+
field_name,
98+
self._get_deprecation_message(field_name),
8999
)
90-
self._deprecated_warning(field_name, deprecation_message)
91100

92101
def _set_deprecation_logs_attr_to_model(self) -> None:
93102
"""
@@ -102,7 +111,7 @@ def _set_deprecation_logs_attr_to_model(self) -> None:
102111
"""
103112
setattr(self, DEPRECATION_LOGS_TAG, self._deprecation_logs)
104113

105-
def _deprecated_warning(self, field_name: str, message: str) -> None:
114+
def _create_warning(self, field_name: str, message: str) -> None:
106115
"""
107116
Show a warning message for deprecated fields (to stdout).
108117
Args:

0 commit comments

Comments
 (0)