From 6b091d473994c111c31b0f1ca1e27ba5cb668b51 Mon Sep 17 00:00:00 2001 From: Antonyjin Date: Tue, 22 Oct 2024 00:23:28 +0900 Subject: [PATCH 01/10] Fix: Project do not use the good version of aleph message and sdk Aleph-message and sdk now uses pydantic v2 but no released yet Using the github link atm and wait for the release. --- pyproject.toml | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 2dc7a411..132dd923 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -33,16 +33,16 @@ dynamic = [ "version" ] dependencies = [ "aiodns==3.2", "aiohttp==3.10.6", - "aleph-message>=0.5", - "aleph-sdk-python>=1.2,<2", - "base58==2.1.1", # Needed now as default with _load_account changement - "py-sr25519-bindings==0.2", # Needed for DOT signatures + "aleph-message @ git+https://github.com/aleph-im/aleph-message#egg=master", # Change the version after the release + "aleph-sdk-python @ git+https://github.com/aleph-im/aleph-sdk-python#egg=177-upgrade-pydantic-version", # Change the version after the release + "base58==2.1.1", # Needed now as default with _load_account changement + "py-sr25519-bindings==0.2", # Needed for DOT signatures "pygments==2.18", - "pynacl==1.5", # Needed now as default with _load_account changement + "pynacl==1.5", # Needed now as default with _load_account changement "python-magic==0.4.27", "rich==13.9.3", "setuptools>=65.5", - "substrate-interface==1.7.11", # Needed for DOT signatures + "substrate-interface==1.7.11", # Needed for DOT signatures "textual==0.73", "typer==0.12.5", ] From 4f6b670a8691286b9d0cc5d6f38773d7f6f28f7a Mon Sep 17 00:00:00 2001 From: Antonyjin Date: Tue, 22 Oct 2024 00:28:44 +0900 Subject: [PATCH 02/10] Fix: Replacing deprecated function after pydantic migration When upgrading the pydantic version, some function are / will become deprecated. replacing them in those files. --- src/aleph_client/commands/account.py | 2 +- src/aleph_client/commands/aggregate.py | 2 +- src/aleph_client/commands/files.py | 8 ++++---- src/aleph_client/commands/instance/network.py | 4 ++-- src/aleph_client/commands/program.py | 8 ++++---- src/aleph_client/commands/utils.py | 3 ++- 6 files changed, 14 insertions(+), 13 deletions(-) diff --git a/src/aleph_client/commands/account.py b/src/aleph_client/commands/account.py index da7f890d..30523e26 100644 --- a/src/aleph_client/commands/account.py +++ b/src/aleph_client/commands/account.py @@ -235,7 +235,7 @@ def sign_bytes( coroutine = account.sign_raw(message.encode()) signature = asyncio.run(coroutine) - typer.echo("\nSignature: " + signature.hex()) + typer.echo("\nSignature: " + f"0x{signature.hex()}") @app.command() diff --git a/src/aleph_client/commands/aggregate.py b/src/aleph_client/commands/aggregate.py index 5b1cd6f1..b60e295d 100644 --- a/src/aleph_client/commands/aggregate.py +++ b/src/aleph_client/commands/aggregate.py @@ -81,7 +81,7 @@ async def post( inline=inline, address=address, ) - log_message = json.dumps(message.dict(), indent=4, default=extended_json_encoder) + log_message = json.dumps(message.model_dump(), indent=4, default=extended_json_encoder) typer.echo(log_message) diff --git a/src/aleph_client/commands/files.py b/src/aleph_client/commands/files.py index 095202b8..fc46bda0 100644 --- a/src/aleph_client/commands/files.py +++ b/src/aleph_client/commands/files.py @@ -52,7 +52,7 @@ async def pin( ref=ref, ) logger.debug("Upload finished") - typer.echo(f"{result.json(indent=4)}") + typer.echo(f"{result.model_dump_json(indent=4)}") @app.command() @@ -91,7 +91,7 @@ async def upload( ref=ref, ) logger.debug("Upload finished") - typer.echo(f"{result.json(indent=4)}") + typer.echo(f"{result.model_dump_json(indent=4)}") @app.command() @@ -142,7 +142,7 @@ async def forget( async with AuthenticatedAlephHttpClient(account=account, api_server=settings.API_HOST) as client: value = await client.forget(hashes=[ItemHash(item_hash)], reason=reason, channel=channel) - typer.echo(f"{value[0].json(indent=4)}") + typer.echo(f"{value[0].model_dump_json(indent=4)}") class GetAccountFilesQueryParams(BaseModel): @@ -229,7 +229,7 @@ async def list( uri = f"{settings.API_HOST}/api/v0/addresses/{address}/files" async with aiohttp.ClientSession() as session: - response = await session.get(uri, params=query_params.dict()) + response = await session.get(uri, params=query_params.model_dump()) if response.status == 200: files_data = await response.json() formatted_files_data = json_lib.dumps(files_data, indent=4) diff --git a/src/aleph_client/commands/instance/network.py b/src/aleph_client/commands/instance/network.py index 6394e7ef..94dddd99 100644 --- a/src/aleph_client/commands/instance/network.py +++ b/src/aleph_client/commands/instance/network.py @@ -11,7 +11,7 @@ from aleph_message.models import InstanceMessage from aleph_message.models.execution.base import PaymentType from aleph_message.models.item_hash import ItemHash -from pydantic import ValidationError +from pydantic import ValidationError, field_validator from aleph_client.commands import help_strings from aleph_client.commands.node import NodeInfo, _fetch_nodes @@ -49,7 +49,7 @@ async def fetch_crn_info(node_url: str) -> dict | None: async with session.get(url) as resp: resp.raise_for_status() system: dict = await resp.json() - info["machine_usage"] = MachineUsage.parse_obj(system) + info["machine_usage"] = MachineUsage.model_validate(system) return info except aiohttp.InvalidURL as e: logger.debug(f"Invalid CRN URL: {url}: {e}") diff --git a/src/aleph_client/commands/program.py b/src/aleph_client/commands/program.py index a020ad09..dabe81c9 100644 --- a/src/aleph_client/commands/program.py +++ b/src/aleph_client/commands/program.py @@ -124,7 +124,7 @@ async def upload( ) logger.debug("Upload finished") if print_messages or print_code_message: - typer.echo(f"{user_code.json(indent=4)}") + typer.echo(f"{user_code.model_dump_json(indent=4)}") program_ref = user_code.item_hash # Register the program @@ -144,7 +144,7 @@ async def upload( ) logger.debug("Upload finished") if print_messages or print_program_message: - typer.echo(f"{message.json(indent=4)}") + typer.echo(f"{message.model_dump_json(indent=4)}") item_hash: ItemHash = message.item_hash hash_base32 = b32encode(b16decode(item_hash.upper())).strip(b"=").lower().decode() @@ -212,7 +212,7 @@ async def update( ) logger.debug("Upload finished") if print_message: - typer.echo(f"{message.json(indent=4)}") + typer.echo(f"{message.model_dump_json(indent=4)}") @app.command() @@ -240,7 +240,7 @@ async def unpersist( message_type=message.type, channel=message.channel, ) - typer.echo(f"{message.json(indent=4)}") + typer.echo(f"{message.model_dump_json(indent=4)}") @app.command() diff --git a/src/aleph_client/commands/utils.py b/src/aleph_client/commands/utils.py index 6e4eeb75..508790f9 100644 --- a/src/aleph_client/commands/utils.py +++ b/src/aleph_client/commands/utils.py @@ -1,6 +1,7 @@ from __future__ import annotations import asyncio +import json import logging import os import sys @@ -50,7 +51,7 @@ def colorized_status(status: MessageStatus) -> str: def colorful_message_json(message: GenericMessage): """Render a message in JSON with colors.""" - return colorful_json(message.json(sort_keys=True, indent=4)) + return colorful_json(json.dumps(message.model_dump(), sort_keys=True, indent=4)) def input_multiline() -> str: From d9e30e1245058438677360184f6be3feefb79e06 Mon Sep 17 00:00:00 2001 From: Antonyjin Date: Tue, 22 Oct 2024 00:35:09 +0900 Subject: [PATCH 03/10] feat: Adding custom encoding for JSON serialization Added `default=extended_json_encoder` to `json.dumps()` to handle serialization of non-native JSON types, such as dates and other complex objects. Replacing function that are / will be deprecated in pydantic v2 and v3 --- .../commands/instance/__init__.py | 6 +++--- src/aleph_client/commands/message.py | 19 ++++++++++--------- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/src/aleph_client/commands/instance/__init__.py b/src/aleph_client/commands/instance/__init__.py index 6b380f43..cad15342 100644 --- a/src/aleph_client/commands/instance/__init__.py +++ b/src/aleph_client/commands/instance/__init__.py @@ -407,7 +407,7 @@ async def create( ) raise typer.Exit(code=1) if print_messages: - echo(f"{message.json(indent=4)}") + echo(f"{message.model_dump_json(indent=4)}") item_hash: ItemHash = message.item_hash item_hash_text = Text(item_hash, style="bright_cyan") @@ -584,7 +584,7 @@ async def delete( message, status = await client.forget(hashes=[ItemHash(item_hash)], reason=reason) if print_message: - echo(f"{message.json(indent=4)}") + echo(f"{message.model_dump_json(indent=4)}") echo(f"Instance {item_hash} has been deleted.") @@ -761,7 +761,7 @@ async def list( echo(f"Address: {address}\n\nNo instance found\n") raise typer.Exit(code=1) if json: - echo(messages.json(indent=4)) + echo(messages.model_dump_json(indent=4)) else: # Since we filtered on message type, we can safely cast as InstanceMessage. messages = cast(List[InstanceMessage], messages) diff --git a/src/aleph_client/commands/message.py b/src/aleph_client/commands/message.py index 00ee2bbf..3c4f3513 100644 --- a/src/aleph_client/commands/message.py +++ b/src/aleph_client/commands/message.py @@ -7,6 +7,7 @@ import sys import tempfile import time +from datetime import datetime from pathlib import Path from typing import Dict, List, Optional @@ -18,7 +19,7 @@ from aleph.sdk.query.responses import MessagesResponse from aleph.sdk.types import AccountFromPrivateKey, StorageEnum from aleph.sdk.utils import extended_json_encoder -from aleph_message.models import AlephMessage, ProgramMessage +from aleph_message.models import AlephMessage, ProgramMessage, StoreMessage from aleph_message.models.base import MessageType from aleph_message.models.item_hash import ItemHash from aleph_message.status import MessageStatus @@ -46,9 +47,9 @@ async def get( typer.echo(f"Message Status: {colorized_status(status)}") if status == MessageStatus.REJECTED: reason = await client.get_message_error(item_hash=ItemHash(item_hash)) - typer.echo(colorful_json(json.dumps(reason, indent=4))) + typer.echo(colorful_json(json.dumps(reason, indent=4, default=extended_json_encoder))) else: - typer.echo(colorful_message_json(message)) + typer.echo(colorful_json(json.dumps(message, indent=4, default=extended_json_encoder))) @app.command() @@ -102,7 +103,7 @@ async def find( ), ignore_invalid_messages=ignore_invalid_messages, ) - typer.echo(colorful_json(response.json(sort_keys=True, indent=4))) + typer.echo(colorful_json(json.dumps(response.model_dump(), sort_keys=True, indent=4, default=extended_json_encoder))) @app.command() @@ -156,7 +157,7 @@ async def post( storage_engine=storage_engine, ) - typer.echo(json.dumps(result.dict(), indent=4, default=extended_json_encoder)) + typer.echo(json.dumps(result.model_dump(), indent=4, default=extended_json_encoder)) @app.command() @@ -178,7 +179,7 @@ async def amend( editor: str = os.getenv("EDITOR", default="nano") with tempfile.NamedTemporaryFile(suffix="json") as fd: # Fill in message template - fd.write(existing_message.content.json(indent=4).encode()) + fd.write(existing_message.content.model_dump_json(indent=4).encode()) fd.seek(0) # Launch editor @@ -203,11 +204,11 @@ async def amend( typer.echo(new_content) async with AuthenticatedAlephHttpClient(account=account, api_server=settings.API_HOST) as client: message, status, response = await client.submit( - content=new_content.dict(), + content=new_content.model_dump(), message_type=existing_message.type, channel=existing_message.channel, ) - typer.echo(f"{message.json(indent=4)}") + typer.echo(f"{message.model_dump_json(indent=4)}") @app.command() @@ -245,7 +246,7 @@ async def watch( async for message in client.watch_messages( message_filter=MessageFilter(refs=[ref], addresses=[original.content.address]) ): - typer.echo(f"{message.json(indent=indent)}") + typer.echo(f"{message.model_dump_json(indent=indent)}") @app.command() From 96a607e20d4134e93b1acee4de339ac7f142a4d9 Mon Sep 17 00:00:00 2001 From: Antonyjin Date: Fri, 25 Oct 2024 16:31:05 +0900 Subject: [PATCH 04/10] Fix: Conflit between pydantic and fastapi version Pydantic>=2 is not compatible with fastapi<0.100.0 --- pyproject.toml | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 132dd923..f0dba3b7 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -33,16 +33,16 @@ dynamic = [ "version" ] dependencies = [ "aiodns==3.2", "aiohttp==3.10.6", - "aleph-message @ git+https://github.com/aleph-im/aleph-message#egg=master", # Change the version after the release - "aleph-sdk-python @ git+https://github.com/aleph-im/aleph-sdk-python#egg=177-upgrade-pydantic-version", # Change the version after the release - "base58==2.1.1", # Needed now as default with _load_account changement - "py-sr25519-bindings==0.2", # Needed for DOT signatures + "aleph-message @ git+https://github.com/aleph-im/aleph-message#egg=master", # Change the version after the release + "aleph-sdk-python @ git+https://github.com/aleph-im/aleph-sdk-python@177-upgrade-pydantic-version#egg=aleph-sdk-python", # Change the version after the release + "base58==2.1.1", # Needed now as default with _load_account changement + "py-sr25519-bindings==0.2", # Needed for DOT signatures "pygments==2.18", - "pynacl==1.5", # Needed now as default with _load_account changement + "pynacl==1.5", # Needed now as default with _load_account changement "python-magic==0.4.27", "rich==13.9.3", "setuptools>=65.5", - "substrate-interface==1.7.11", # Needed for DOT signatures + "substrate-interface==1.7.11", # Needed for DOT signatures "textual==0.73", "typer==0.12.5", ] @@ -83,7 +83,7 @@ dependencies = [ "pytest-cov==5.0.0", "mypy==1.10.0", "base58==2.1.1", - "fastapi==0.98.0", + "fastapi==0.100.0", "httpx==0.27.0", "types-requests==2.32.0.20240602", "types-setuptools==70.0.0.20240524", @@ -99,7 +99,7 @@ dependencies = [ "pytest-cov==5.0.0", "mypy==1.10.0", "base58==2.1.1", - "fastapi==0.98.0", + "fastapi==0.100.0", "httpx==0.27.0", ] [tool.hatch.envs.testing.scripts] From 4d5611da1899fea694ffbc2dbc1e34436cc389cf Mon Sep 17 00:00:00 2001 From: Antonyjin Date: Fri, 25 Oct 2024 16:51:31 +0900 Subject: [PATCH 05/10] Fix: Lint test did not pass and conflict between dependencies Conflit between pydantic and yamlfix black did not pass --- pyproject.toml | 4 ++-- src/aleph_client/commands/message.py | 4 +++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index f0dba3b7..f1273f70 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -38,7 +38,7 @@ dependencies = [ "base58==2.1.1", # Needed now as default with _load_account changement "py-sr25519-bindings==0.2", # Needed for DOT signatures "pygments==2.18", - "pynacl==1.5", # Needed now as default with _load_account changement + "pynacl==1.5", # Needed now as default with _load_account changement "python-magic==0.4.27", "rich==13.9.3", "setuptools>=65.5", @@ -122,7 +122,7 @@ dependencies = [ "mypy==1.10.0", "ruff==0.4.9", "isort==5.13.2", - "yamlfix==1.16.1", + "yamlfix==1.17.0", "pyproject-fmt==2.2.1", "types-requests==2.32.0.20240602", diff --git a/src/aleph_client/commands/message.py b/src/aleph_client/commands/message.py index 3c4f3513..a7ae7a20 100644 --- a/src/aleph_client/commands/message.py +++ b/src/aleph_client/commands/message.py @@ -103,7 +103,9 @@ async def find( ), ignore_invalid_messages=ignore_invalid_messages, ) - typer.echo(colorful_json(json.dumps(response.model_dump(), sort_keys=True, indent=4, default=extended_json_encoder))) + typer.echo( + colorful_json(json.dumps(response.model_dump(), sort_keys=True, indent=4, default=extended_json_encoder)) + ) @app.command() From f5dc78278955ec0d67d970d6543929f9bbd7add8 Mon Sep 17 00:00:00 2001 From: Antonyjin Date: Tue, 5 Nov 2024 23:07:34 +0900 Subject: [PATCH 06/10] fix: Wrong aleph-message, aleph-python-sdk version --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index f1273f70..0ac681a6 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -33,7 +33,7 @@ dynamic = [ "version" ] dependencies = [ "aiodns==3.2", "aiohttp==3.10.6", - "aleph-message @ git+https://github.com/aleph-im/aleph-message#egg=master", # Change the version after the release + "aleph-message @ git+https://github.com/aleph-im/aleph-message@108-upgrade-pydantic-version#egg=aleph-message", # Change the version after the release "aleph-sdk-python @ git+https://github.com/aleph-im/aleph-sdk-python@177-upgrade-pydantic-version#egg=aleph-sdk-python", # Change the version after the release "base58==2.1.1", # Needed now as default with _load_account changement "py-sr25519-bindings==0.2", # Needed for DOT signatures From 08ad3a426f4763a97ce5a8787e157700563b5707 Mon Sep 17 00:00:00 2001 From: Antonyjin Date: Tue, 5 Nov 2024 23:33:25 +0900 Subject: [PATCH 07/10] fix: Pydantic 2 being more strict about type validation With Pydantic v2, strict type validation is enforced. The type field is expected to have the exact value "POST" as a literal. --- tests/test_post.json | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/test_post.json b/tests/test_post.json index 58de8274..19a25683 100644 --- a/tests/test_post.json +++ b/tests/test_post.json @@ -1 +1,4 @@ -{"hello": "world"} \ No newline at end of file +{ + "type": "POST", + "hello": "world" +} From 93e1e2470d579c539a57f87f446e166e0ae00349 Mon Sep 17 00:00:00 2001 From: Antonyjin Date: Tue, 3 Dec 2024 02:04:38 +0900 Subject: [PATCH 08/10] change version of typing_extension --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index 0ac681a6..627b1bc5 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -127,7 +127,7 @@ dependencies = [ "types-requests==2.32.0.20240602", "types-setuptools==70.0.0.20240524", - "typing_extensions==4.12.2", + "typing_extensions==4.5.0", ] [tool.hatch.envs.linting.scripts] typing = "mypy {args:} ./src/ ./tests/" From e621718eccbdd7e890ae589e38817e6dc3b11cbd Mon Sep 17 00:00:00 2001 From: Antonyjin Date: Tue, 3 Dec 2024 02:08:25 +0900 Subject: [PATCH 09/10] change version of typing_extension --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index 627b1bc5..0ac681a6 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -127,7 +127,7 @@ dependencies = [ "types-requests==2.32.0.20240602", "types-setuptools==70.0.0.20240524", - "typing_extensions==4.5.0", + "typing_extensions==4.12.2", ] [tool.hatch.envs.linting.scripts] typing = "mypy {args:} ./src/ ./tests/" From 11d7c2180904708d66046e31f27bb7c6764be53a Mon Sep 17 00:00:00 2001 From: Antonyjin Date: Tue, 3 Dec 2024 02:27:22 +0900 Subject: [PATCH 10/10] style: ymlfix --- pyproject.toml | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 0ac681a6..953fcbc3 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -33,16 +33,16 @@ dynamic = [ "version" ] dependencies = [ "aiodns==3.2", "aiohttp==3.10.6", - "aleph-message @ git+https://github.com/aleph-im/aleph-message@108-upgrade-pydantic-version#egg=aleph-message", # Change the version after the release - "aleph-sdk-python @ git+https://github.com/aleph-im/aleph-sdk-python@177-upgrade-pydantic-version#egg=aleph-sdk-python", # Change the version after the release - "base58==2.1.1", # Needed now as default with _load_account changement - "py-sr25519-bindings==0.2", # Needed for DOT signatures + "aleph-message @ git+https://github.com/aleph-im/aleph-message@108-upgrade-pydantic-version#egg=aleph-message", # Change the version after the release + "aleph-sdk-python @ git+https://github.com/aleph-im/aleph-sdk-python@177-upgrade-pydantic-version#egg=aleph-sdk-python", # Change the version after the release + "base58==2.1.1", # Needed now as default with _load_account changement + "py-sr25519-bindings==0.2", # Needed for DOT signatures "pygments==2.18", "pynacl==1.5", # Needed now as default with _load_account changement "python-magic==0.4.27", "rich==13.9.3", "setuptools>=65.5", - "substrate-interface==1.7.11", # Needed for DOT signatures + "substrate-interface==1.7.11", # Needed for DOT signatures "textual==0.73", "typer==0.12.5", ]