Skip to content

Commit

Permalink
Fix: Replaced .parse_obj() by .model_validate()
Browse files Browse the repository at this point in the history
  • Loading branch information
Andres D. Molins committed Oct 24, 2024
1 parent e4e3d1e commit 472c57b
Show file tree
Hide file tree
Showing 12 changed files with 20 additions and 18 deletions.
4 changes: 3 additions & 1 deletion src/aleph/chains/chain_data_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,9 @@ def _get_tx_messages_smart_contract_protocol(tx: ChainTxDb) -> List[Dict[str, An
)

try:
payload = cast(GenericMessageEvent, payload_model.parse_obj(tx.content))
payload = cast(
GenericMessageEvent, payload_model.model_validate(tx.content)
)
except ValidationError:
raise InvalidContent(f"Incompatible tx content for {tx.chain}/{tx.hash}")

Expand Down
2 changes: 1 addition & 1 deletion src/aleph/chains/indexer_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ async def _query(self, query: str, model: Type[T]) -> T:
response = await self.http_session.post("/", json={"query": query})
response.raise_for_status()
response_json = await response.json()
return model.parse_obj(response_json)
return model.model_validate(response_json)

async def fetch_account_state(
self,
Expand Down
2 changes: 1 addition & 1 deletion src/aleph/chains/tezos.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ async def fetch_messages(
response.raise_for_status()
response_json = await response.json()

return IndexerResponse[IndexerMessageEvent].parse_obj(response_json)
return IndexerResponse[IndexerMessageEvent].model_validate(response_json)


def indexer_event_to_chain_tx(
Expand Down
2 changes: 1 addition & 1 deletion src/aleph/db/models/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def validate_message_content(
content_dict: Dict[str, Any],
) -> BaseContent:
content_type = CONTENT_TYPE_MAP[message_type]
content = content_type.parse_obj(content_dict)
content = content_type.model_validate(content_dict)
# Validate that the content time can be converted to datetime. This will
# raise a ValueError and be caught
# TODO: move this validation in aleph-message
Expand Down
2 changes: 1 addition & 1 deletion src/aleph/schemas/api/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def format_message(message: MessageDb) -> AlephMessage:
def format_message_dict(message: Dict[str, Any]) -> AlephMessage:
message_type = message.get("type")
message_cls = MESSAGE_CLS_DICT[message_type]
return message_cls.parse_obj(message)
return message_cls.model_validate(message)


class BaseMessageStatus(BaseModel):
Expand Down
8 changes: 4 additions & 4 deletions src/aleph/schemas/chains/indexer_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ class AccountEntityState(BaseModel):
processed: List[Tuple[dt.datetime, dt.datetime]]

@field_validator("pending", "processed", mode="before")
def split_datetime_ranges(cls, v):
if isinstance(v, str):
return v.split("/")
return v
def split_datetime_ranges(cls, values):
return map(
lambda value: value.split("/") if isinstance(value, str) else value, values
)


class IndexerAccountStateResponseData(BaseModel):
Expand Down
2 changes: 1 addition & 1 deletion src/aleph/web/controllers/accounts.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ async def get_account_files(request: web.Request) -> web.Response:
address = _get_address_from_request(request)

try:
query_params = GetAccountFilesQueryParams.parse_obj(request.query)
query_params = GetAccountFilesQueryParams.model_validate(request.query)
except ValidationError as e:
raise web.HTTPUnprocessableEntity(text=e.json(indent=4))

Expand Down
2 changes: 1 addition & 1 deletion src/aleph/web/controllers/aggregates.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ async def address_aggregate(request: web.Request) -> web.Response:
address: str = request.match_info["address"]

try:
query_params = AggregatesQueryParams.parse_obj(request.query)
query_params = AggregatesQueryParams.model_validate(request.query)
except ValidationError as e:
raise web.HTTPUnprocessableEntity(
text=e.json(), content_type="application/json"
Expand Down
4 changes: 2 additions & 2 deletions src/aleph/web/controllers/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ async def ccn_metric(request: web.Request) -> web.Response:
"""Fetch metrics for CCN node id"""

session_factory: DbSessionFactory = get_session_factory_from_request(request)
query_params = Metrics.parse_obj(request.query)
query_params = Metrics.model_validate(request.query)

node_id = _get_node_id_from_request(request)

Expand All @@ -124,7 +124,7 @@ async def crn_metric(request: web.Request) -> web.Response:
"""Fetch Metric for crn."""

session_factory: DbSessionFactory = get_session_factory_from_request(request)
query_params = Metrics.parse_obj(request.query)
query_params = Metrics.model_validate(request.query)

node_id = _get_node_id_from_request(request)

Expand Down
4 changes: 2 additions & 2 deletions src/aleph/web/controllers/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ async def view_messages_list(request: web.Request) -> web.Response:
"""Messages list view with filters"""

try:
query_params = MessageQueryParams.parse_obj(request.query)
query_params = MessageQueryParams.model_validate(request.query)
except ValidationError as e:
raise web.HTTPUnprocessableEntity(text=e.json(indent=4))

Expand Down Expand Up @@ -388,7 +388,7 @@ async def messages_ws(request: web.Request) -> web.WebSocketResponse:
mq_channel = await get_mq_ws_channel_from_request(request=request, logger=LOGGER)

try:
query_params = WsMessageQueryParams.parse_obj(request.query)
query_params = WsMessageQueryParams.model_validate(request.query)
except ValidationError as e:
raise web.HTTPUnprocessableEntity(text=e.json(indent=4))

Expand Down
2 changes: 1 addition & 1 deletion src/aleph/web/controllers/p2p.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ class PubMessageRequest(BaseModel):
@shielded
async def pub_message(request: web.Request):
try:
request_data = PubMessageRequest.parse_obj(await request.json())
request_data = PubMessageRequest.model_validate(await request.json())
except ValidationError as e:
raise web.HTTPUnprocessableEntity(text=e.json(indent=4))
except ValueError:
Expand Down
4 changes: 2 additions & 2 deletions src/aleph/web/controllers/posts.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ def merged_post_v0_to_dict(

def get_query_params(request: web.Request) -> PostQueryParams:
try:
query_params = PostQueryParams.parse_obj(request.query)
query_params = PostQueryParams.model_validate(request.query)
except ValidationError as e:
raise web.HTTPUnprocessableEntity(text=e.json(indent=4))

Expand Down Expand Up @@ -231,7 +231,7 @@ async def view_posts_list_v1(request) -> web.Response:
query_string = request.query_string

try:
query_params = PostQueryParams.parse_obj(request.query)
query_params = PostQueryParams.model_validate(request.query)
except ValidationError as e:
raise web.HTTPUnprocessableEntity(text=e.json(indent=4))

Expand Down

0 comments on commit 472c57b

Please sign in to comment.