From b7e46133d7acbb19d66d84042e367510705ed14b Mon Sep 17 00:00:00 2001 From: Valentin Lorentz <vlorentz@softwareheritage.org> Date: Thu, 21 Jul 2022 12:42:42 +0200 Subject: [PATCH] Rename HashableObject to BaseHashableModel and make it a subclass of BaseModel instead of a mix-in class. A future commit will add a method implemented by both with different signatures that mypy cannot unify yet. --- swh/model/model.py | 16 ++++++++-------- swh/model/tests/test_model.py | 4 ++++ 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/swh/model/model.py b/swh/model/model.py index 1073cc61..b7f8c44c 100644 --- a/swh/model/model.py +++ b/swh/model/model.py @@ -204,7 +204,7 @@ def _compute_hash_from_manifest(manifest: bytes) -> Sha1Git: return hashlib.new("sha1", manifest).digest() -class HashableObject(metaclass=ABCMeta): +class BaseHashableModel(BaseModel, metaclass=ABCMeta): """Mixin to automatically compute object identifier hash when the associated model is instantiated.""" @@ -234,14 +234,14 @@ class HashableObject(metaclass=ABCMeta): return self.id def check(self) -> None: - super().check() # type: ignore + super().check() if self.id != self.compute_hash(): raise ValueError("'id' does not match recomputed hash.") -class HashableObjectWithManifest(HashableObject): - """Derived class of HashableObject, for objects that may need to store +class HashableObjectWithManifest(BaseHashableModel): + """Derived class of BaseHashableModel, for objects that may need to store verbatim git objects as ``raw_manifest`` to preserve original hashes.""" __slots__ = () @@ -585,7 +585,7 @@ class TimestampWithTimezone(BaseModel): @attr.s(frozen=True, slots=True) -class Origin(HashableObject, BaseModel): +class Origin(BaseHashableModel): """Represents a software source: a VCS and an URL.""" object_type: Final = "origin" @@ -728,7 +728,7 @@ class SnapshotBranch(BaseModel): @attr.s(frozen=True, slots=True) -class Snapshot(HashableObject, BaseModel): +class Snapshot(BaseHashableModel): """Represents the full state of an origin at a given point in time.""" object_type: Final = "snapshot" @@ -1366,7 +1366,7 @@ def normalize_discovery_date(value: Any) -> datetime.datetime: @attr.s(frozen=True, slots=True) -class RawExtrinsicMetadata(HashableObject, BaseModel): +class RawExtrinsicMetadata(BaseHashableModel): object_type: Final = "raw_extrinsic_metadata" # target object @@ -1583,7 +1583,7 @@ class RawExtrinsicMetadata(HashableObject, BaseModel): @attr.s(frozen=True, slots=True) -class ExtID(HashableObject, BaseModel): +class ExtID(BaseHashableModel): object_type: Final = "extid" extid_type = attr.ib(type=str, validator=type_validator()) diff --git a/swh/model/tests/test_model.py b/swh/model/tests/test_model.py index 4540c433..3d36dadd 100644 --- a/swh/model/tests/test_model.py +++ b/swh/model/tests/test_model.py @@ -1374,9 +1374,13 @@ def test_object_type(objtype_and_obj): def test_object_type_is_final(): + checked_classes = set() object_types = set() def check_final(cls): + if cls in checked_classes: + return + checked_classes.add(cls) if hasattr(cls, "object_type"): assert cls.object_type not in object_types object_types.add(cls.object_type) -- GitLab