Skip to content
Snippets Groups Projects
Commit b7e46133 authored by vlorentz's avatar vlorentz Committed by Phabricator Migration user
Browse files

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.
parent b872718a
No related tags found
No related merge requests found
......@@ -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())
......
......@@ -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)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment