From 9979e1501e31dd29b270a324083d1a4d5a05091c Mon Sep 17 00:00:00 2001 From: mhh Date: Wed, 31 Jan 2024 13:30:02 +0100 Subject: [PATCH 1/3] Ignore instead of forbid extra fields --- aleph_message/models/__init__.py | 27 +------------------ aleph_message/models/abstract.py | 5 +--- aleph_message/models/execution/environment.py | 9 ------- aleph_message/models/execution/volume.py | 5 +--- 4 files changed, 3 insertions(+), 43 deletions(-) diff --git a/aleph_message/models/__init__.py b/aleph_message/models/__init__.py index 4f15e42..627e038 100644 --- a/aleph_message/models/__init__.py +++ b/aleph_message/models/__init__.py @@ -6,7 +6,7 @@ from pathlib import Path from typing import Any, Dict, List, Literal, Optional, Type, TypeVar, Union, cast -from pydantic import BaseModel, Extra, Field, validator +from pydantic import BaseModel, Field, validator from typing_extensions import TypeAlias from .abstract import BaseContent @@ -22,9 +22,6 @@ class MongodbId(BaseModel): oid: str = Field(alias="$oid") - class Config: - extra = Extra.forbid - class ChainRef(BaseModel): """Some POST messages have a 'ref' field referencing other content""" @@ -44,9 +41,6 @@ class MessageConfirmationHash(BaseModel): binary: str = Field(alias="$binary") type: str = Field(alias="$type") - class Config: - extra = Extra.forbid - class MessageConfirmation(BaseModel): """Format of the result when a message has been confirmed on a blockchain""" @@ -61,16 +55,10 @@ class MessageConfirmation(BaseModel): default=None, description="The address that published the transaction." ) - class Config: - extra = Extra.forbid - class AggregateContentKey(BaseModel): name: str - class Config: - extra = Extra.forbid - class PostContent(BaseContent): """Content of a POST message""" @@ -92,9 +80,6 @@ def check_type(cls, v, values): raise ValueError("A 'ref' is required for POST type 'amend'") return v - class Config: - extra = Extra.forbid - class AggregateContent(BaseContent): """Content of an AGGREGATE message""" @@ -104,9 +89,6 @@ class AggregateContent(BaseContent): ) content: Dict = Field(description="The content of an aggregate must be a dict") - class Config: - extra = Extra.forbid - class StoreContent(BaseContent): """Content of a STORE message""" @@ -118,9 +100,6 @@ class StoreContent(BaseContent): ref: Optional[str] = None metadata: Optional[Dict[str, Any]] = Field(description="Metadata of the VM") - class Config: - extra = Extra.allow - class ForgetContent(BaseContent): """Content of a FORGET message""" @@ -238,7 +217,6 @@ def convert_float_to_datetime(cls, v, values): return v class Config: - extra = Extra.forbid exclude = {"id_", "_id"} @@ -403,6 +381,3 @@ class MessagesResponse(BaseModel): pagination_total: int pagination_per_page: int pagination_item: str - - class Config: - extra = Extra.forbid diff --git a/aleph_message/models/abstract.py b/aleph_message/models/abstract.py index f272dbd..1882f01 100644 --- a/aleph_message/models/abstract.py +++ b/aleph_message/models/abstract.py @@ -1,4 +1,4 @@ -from pydantic import BaseModel, Extra +from pydantic import BaseModel def hashable(obj): @@ -23,6 +23,3 @@ class BaseContent(BaseModel): address: str time: float - - class Config: - extra = Extra.forbid diff --git a/aleph_message/models/execution/environment.py b/aleph_message/models/execution/environment.py index 1e97073..8e34ed0 100644 --- a/aleph_message/models/execution/environment.py +++ b/aleph_message/models/execution/environment.py @@ -28,9 +28,6 @@ class FunctionTriggers(HashableModel): description="Persist the execution of the program instead of running it on demand.", ) - class Config: - extra = Extra.forbid - class NetworkProtocol(str, Enum): tcp = "tcp" @@ -78,9 +75,6 @@ class CpuProperties(HashableModel): default=None, description="CPU vendor. Allows other vendors." ) - class Config: - extra = Extra.forbid - class HypervisorType(str, Enum): qemu = "qemu" @@ -101,9 +95,6 @@ class NodeRequirements(HashableModel): default=None, description="Node address must match this regular expression" ) - class Config: - extra = Extra.forbid - class HostRequirements(HashableModel): cpu: Optional[CpuProperties] = Field( diff --git a/aleph_message/models/execution/volume.py b/aleph_message/models/execution/volume.py index 0fd0c9e..8e4e487 100644 --- a/aleph_message/models/execution/volume.py +++ b/aleph_message/models/execution/volume.py @@ -4,7 +4,7 @@ from enum import Enum from typing import Literal, Optional, Union -from pydantic import ConstrainedInt, Extra +from pydantic import ConstrainedInt from ...utils import Gigabytes, gigabyte_to_mebibyte from ..abstract import HashableModel @@ -19,9 +19,6 @@ class AbstractVolume(HashableModel, ABC): def is_read_only(self): ... - class Config: - extra = Extra.forbid - class ImmutableVolume(AbstractVolume): ref: ItemHash From e872112275ff28537fcd60affa5973f485798915 Mon Sep 17 00:00:00 2001 From: mhh Date: Wed, 31 Jan 2024 13:53:20 +0100 Subject: [PATCH 2/3] Reformat with black/isort/ruff --- aleph_message/exceptions.py | 3 +-- aleph_message/models/__init__.py | 2 +- aleph_message/models/execution/abstract.py | 8 ++------ aleph_message/models/execution/volume.py | 3 +-- 4 files changed, 5 insertions(+), 11 deletions(-) diff --git a/aleph_message/exceptions.py b/aleph_message/exceptions.py index c8f7873..de043c3 100644 --- a/aleph_message/exceptions.py +++ b/aleph_message/exceptions.py @@ -1,2 +1 @@ -class UnknownHashError(ValueError): - ... +class UnknownHashError(ValueError): ... diff --git a/aleph_message/models/__init__.py b/aleph_message/models/__init__.py index 627e038..3c6e7d0 100644 --- a/aleph_message/models/__init__.py +++ b/aleph_message/models/__init__.py @@ -11,9 +11,9 @@ from .abstract import BaseContent from .base import Chain, HashType, MessageType +from .execution.base import MachineType, Payment, PaymentType # noqa from .execution.instance import InstanceContent from .execution.program import ProgramContent -from .execution.base import PaymentType, MachineType, Payment # noqa from .item_hash import ItemHash, ItemType diff --git a/aleph_message/models/execution/abstract.py b/aleph_message/models/execution/abstract.py index 833ba7c..c6271b5 100644 --- a/aleph_message/models/execution/abstract.py +++ b/aleph_message/models/execution/abstract.py @@ -5,14 +5,10 @@ from pydantic import Field -from .environment import ( - FunctionEnvironment, - HostRequirements, - MachineResources, -) +from ..abstract import BaseContent, HashableModel from .base import Payment +from .environment import FunctionEnvironment, HostRequirements, MachineResources from .volume import MachineVolume -from ..abstract import BaseContent, HashableModel class BaseExecutableContent(HashableModel, BaseContent, ABC): diff --git a/aleph_message/models/execution/volume.py b/aleph_message/models/execution/volume.py index 8e4e487..aa761d0 100644 --- a/aleph_message/models/execution/volume.py +++ b/aleph_message/models/execution/volume.py @@ -16,8 +16,7 @@ class AbstractVolume(HashableModel, ABC): mount: Optional[str] = None @abstractmethod - def is_read_only(self): - ... + def is_read_only(self): ... class ImmutableVolume(AbstractVolume): From 0dc8e17dc236ae1805bad95c647d3694d8c8a659 Mon Sep 17 00:00:00 2001 From: mhh Date: Wed, 31 Jan 2024 15:41:16 +0100 Subject: [PATCH 3/3] Add Extra.forbid back to AbstractVolume because of greedy encoding of MachineVolume types --- aleph_message/models/execution/volume.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/aleph_message/models/execution/volume.py b/aleph_message/models/execution/volume.py index aa761d0..5fbe9b7 100644 --- a/aleph_message/models/execution/volume.py +++ b/aleph_message/models/execution/volume.py @@ -4,7 +4,7 @@ from enum import Enum from typing import Literal, Optional, Union -from pydantic import ConstrainedInt +from pydantic import ConstrainedInt, Extra from ...utils import Gigabytes, gigabyte_to_mebibyte from ..abstract import HashableModel @@ -18,6 +18,11 @@ class AbstractVolume(HashableModel, ABC): @abstractmethod def is_read_only(self): ... + class Config: + # This is the only type where we really need to forbid extra fields. + # Otherwise the pydantic_encoder will take the first allowed type instead of the correct one. + extra = Extra.forbid + class ImmutableVolume(AbstractVolume): ref: ItemHash