Skip to content

Commit

Permalink
🐛 model validate
Browse files Browse the repository at this point in the history
  • Loading branch information
juftin committed Dec 16, 2023
1 parent 164d972 commit 348da1d
Show file tree
Hide file tree
Showing 12 changed files with 26 additions and 22 deletions.
6 changes: 3 additions & 3 deletions lunchable/models/assets.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ def get_assets(self) -> List[AssetsObject]:
method=self.Methods.GET, url_path=[APIConfig.LUNCHMONEY_ASSETS]
)
assets = response_data.get(APIConfig.LUNCHMONEY_ASSETS)
asset_objects = [AssetsObject(**item) for item in assets]
asset_objects = [AssetsObject.model_validate(item) for item in assets]
return asset_objects

def update_asset(
Expand Down Expand Up @@ -198,7 +198,7 @@ def update_asset(
url_path=[APIConfig.LUNCHMONEY_ASSETS, asset_id],
payload=payload,
)
asset = AssetsObject(**response_data)
asset = AssetsObject.model_validate(response_data)
return asset

def insert_asset(
Expand Down Expand Up @@ -266,5 +266,5 @@ def insert_asset(
url_path=[APIConfig.LUNCHMONEY_ASSETS],
payload=payload,
)
asset = AssetsObject(**response_data)
asset = AssetsObject.model_validate(response_data)
return asset
2 changes: 1 addition & 1 deletion lunchable/models/budgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ def get_budgets(
response_data = self.make_request(
method="GET", url_path=[APIConfig.LUNCHMONEY_BUDGET], params=params
)
budget_objects = [BudgetObject(**item) for item in response_data]
budget_objects = [BudgetObject.model_validate(item) for item in response_data]
return budget_objects

def upsert_budget(
Expand Down
6 changes: 4 additions & 2 deletions lunchable/models/categories.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,9 @@ def get_categories(self) -> List[CategoriesObject]:
method=self.Methods.GET, url_path=APIConfig.LUNCHMONEY_CATEGORIES
)
categories = response_data["categories"]
category_objects = [CategoriesObject(**item) for item in categories]
category_objects = [
CategoriesObject.model_validate(item) for item in categories
]
return category_objects

def insert_category(
Expand Down Expand Up @@ -232,7 +234,7 @@ def get_category(self, category_id: int) -> CategoriesObject:
method=self.Methods.GET,
url_path=[APIConfig.LUNCHMONEY_CATEGORIES, category_id],
)
return CategoriesObject(**response_data)
return CategoriesObject.model_validate(response_data)

def remove_category(self, category_id: int) -> bool:
"""
Expand Down
4 changes: 2 additions & 2 deletions lunchable/models/crypto.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def get_crypto(self) -> List[CryptoObject]:
method=self.Methods.GET, url_path=APIConfig.LUNCHMONEY_CRYPTO
)
crypto_data = response_data["crypto"]
crypto_objects = [CryptoObject(**item) for item in crypto_data]
crypto_objects = [CryptoObject.model_validate(item) for item in crypto_data]
return crypto_objects

def update_crypto(
Expand Down Expand Up @@ -159,5 +159,5 @@ def update_crypto(
],
payload=crypto_body,
)
crypto = CryptoObject(**response_data)
crypto = CryptoObject.model_validate(response_data)
return crypto
2 changes: 1 addition & 1 deletion lunchable/models/plaid_accounts.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,5 +120,5 @@ def get_plaid_accounts(self) -> List[PlaidAccountObject]:
method=self.Methods.GET, url_path=APIConfig.LUNCHMONEY_PLAID_ACCOUNTS
)
accounts = response_data.get(APIConfig.LUNCHMONEY_PLAID_ACCOUNTS)
account_objects = [PlaidAccountObject(**item) for item in accounts]
account_objects = [PlaidAccountObject.model_validate(item) for item in accounts]
return account_objects
2 changes: 1 addition & 1 deletion lunchable/models/recurring_expenses.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ def get_recurring_expenses(
)
recurring_expenses = response_data.get(APIConfig.LUNCH_MONEY_RECURRING_EXPENSES)
recurring_expenses_objects = [
RecurringExpensesObject(**item) for item in recurring_expenses
RecurringExpensesObject.model_validate(item) for item in recurring_expenses
]
logger.debug(
"%s RecurringExpensesObjects retrieved", len(recurring_expenses_objects)
Expand Down
2 changes: 1 addition & 1 deletion lunchable/models/tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,5 @@ def get_tags(self) -> List[TagsObject]:
response_data = self.make_request(
method=self.Methods.GET, url_path=APIConfig.LUNCHMONEY_TAGS
)
tag_objects = [TagsObject(**item) for item in response_data]
tag_objects = [TagsObject.model_validate(item) for item in response_data]
return tag_objects
10 changes: 6 additions & 4 deletions lunchable/models/transactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ def get_update_object(self) -> TransactionUpdateObject:
TransactionUpdateObject.StatusEnum(self.status)
except ValueError:
update_dict["status"] = None
update_object = TransactionUpdateObject(**update_dict)
update_object = TransactionUpdateObject.model_validate(update_dict)
if update_object.tags is not None:
tags = [] if self.tags is None else self.tags
update_object.tags = [tag.name for tag in tags]
Expand All @@ -359,7 +359,7 @@ def get_insert_object(self) -> TransactionInsertObject:
TransactionInsertObject.StatusEnum(self.status)
except ValueError:
insert_dict["status"] = None
insert_object = TransactionInsertObject(**insert_dict)
insert_object = TransactionInsertObject.model_validate(insert_dict)
if insert_object.tags is not None:
tags = [] if self.tags is None else self.tags
insert_object.tags = [tag.name for tag in tags]
Expand Down Expand Up @@ -543,7 +543,9 @@ def get_transactions(
params=search_params,
)
transactions = response_data[APIConfig.LUNCHMONEY_TRANSACTIONS]
transaction_objects = [TransactionObject(**item) for item in transactions]
transaction_objects = [
TransactionObject.model_validate(item) for item in transactions
]
return transaction_objects

def get_transaction(self, transaction_id: int) -> TransactionObject:
Expand Down Expand Up @@ -575,7 +577,7 @@ def get_transaction(self, transaction_id: int) -> TransactionObject:
method=self.Methods.GET,
url_path=[APIConfig.LUNCHMONEY_TRANSACTIONS, transaction_id],
)
return TransactionObject(**response_data)
return TransactionObject.model_validate(response_data)

ListOrSingleTransactionUpdateObject = Optional[
Union[TransactionUpdateObject, TransactionObject]
Expand Down
2 changes: 1 addition & 1 deletion lunchable/models/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,5 @@ def get_user(self) -> UserObject:
response_data = self.make_request(
method=self.Methods.GET, url_path=APIConfig.LUNCHMONEY_ME
)
me = UserObject(**response_data)
me = UserObject.model_validate(response_data)
return me
4 changes: 2 additions & 2 deletions lunchable/plugins/base/base_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,9 +244,9 @@ def _cache_single_object(
json_body.decode("utf-8")
)
if isinstance(json_data, dict):
data_objects = model(**json_data)
data_objects = model.model_validate(json_data)
else:
data_objects = [model(**item) for item in json_data]
data_objects = [model.model_validate(item) for item in json_data]
return data_objects

def get_latest_cache(
Expand Down
2 changes: 1 addition & 1 deletion lunchable/plugins/base/pandas_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def df_to_models(
array_df = df.copy()
array_df = array_df.fillna(np.NaN).replace([np.NaN], [None])
model_array = array_df.to_dict(orient="records")
return [model_type(**item) for item in model_array]
return [model_type.model_validate(item) for item in model_array]


class LunchablePandasTransactionsApp(LunchableTransactionsApp, LunchablePandasApp):
Expand Down
6 changes: 3 additions & 3 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,9 @@ def test_transactions() -> List[TransactionObject]:
"tags": None,
"type": None,
}
transaction_1 = TransactionObject(**transaction_dict_1)
transaction_2 = TransactionObject(**transaction_dict_2)
transaction_3 = TransactionObject(**transaction_dict_3)
transaction_1 = TransactionObject.model_validate(transaction_dict_1)
transaction_2 = TransactionObject.model_validate(transaction_dict_2)
transaction_3 = TransactionObject.model_validate(transaction_dict_3)
return [transaction_1, transaction_2, transaction_3]


Expand Down

0 comments on commit 348da1d

Please sign in to comment.