diff --git a/swh/model/git_objects.py b/swh/model/git_objects.py index 4b31cbc4d596c69750c5c3e17c95692ab3e738a0..a1e429396803b393841e0397922532934ba4b0ff 100644 --- a/swh/model/git_objects.py +++ b/swh/model/git_objects.py @@ -485,7 +485,7 @@ def snapshot_git_object( if not target: target_type = b"dangling" target_id = b"" - elif target.target_type == model.TargetType.ALIAS: + elif target.target_type == model.SnapshotTargetType.ALIAS: target_type = b"alias" target_id = target.target if target_id not in snapshot.branches or target_id == name: diff --git a/swh/model/hypothesis_strategies.py b/swh/model/hypothesis_strategies.py index e41e275de1d71278f176e87b1396ee2ae8c5882c..df90cc714209aa9f68a7359a3f9303f2b70fa51a 100644 --- a/swh/model/hypothesis_strategies.py +++ b/swh/model/hypothesis_strategies.py @@ -7,6 +7,7 @@ import datetime import string from typing import Sequence +from deprecated import deprecated from hypothesis import assume from hypothesis.extra.dateutil import timezones from hypothesis.strategies import ( @@ -49,7 +50,7 @@ from .model import ( SkippedContent, Snapshot, SnapshotBranch, - TargetType, + SnapshotTargetType, Timestamp, TimestampWithTimezone, ) @@ -413,31 +414,48 @@ def branch_names(): return binary(min_size=1) -def branch_targets_object_d(): +def snapshot_targets_object_d(): return builds( dict, target=sha1_git(), target_type=sampled_from( - [x.value for x in TargetType if x.value not in ("alias",)] + [x.value for x in SnapshotTargetType if x.value not in ("alias",)] ), ) -def branch_targets_alias_d(): +branch_targets_object_d = deprecated( + version="v6.13.0", reason="use snapshot_targets_object_d" +)(snapshot_targets_object_d) + + +def snapshot_targets_alias_d(): return builds( dict, target=sha1_git(), target_type=just("alias") - ) # TargetType.ALIAS.value)) + ) # SnapshotTargetType.ALIAS.value)) + +branch_targets_alias_d = deprecated( + version="v6.13.0", reason="use snapshot_targets_alias_d" +)(snapshot_targets_alias_d) -def branch_targets_d(*, only_objects=False): + +def snapshot_targets_d(*, only_objects=False): if only_objects: - return branch_targets_object_d() + return snapshot_targets_object_d() else: - return one_of(branch_targets_alias_d(), branch_targets_object_d()) + return one_of(snapshot_targets_alias_d(), snapshot_targets_object_d()) -def branch_targets(*, only_objects=False): - return builds(SnapshotBranch.from_dict, branch_targets_d(only_objects=only_objects)) +branch_targets_d = deprecated(version="v6.13.0", reason="use snapshot_targets_d")( + snapshot_targets_d +) + + +def snapshot_targets(*, only_objects=False): + return builds( + SnapshotBranch.from_dict, snapshot_targets_d(only_objects=only_objects) + ) @composite @@ -445,7 +463,7 @@ def snapshots_d(draw, *, min_size=0, max_size=100, only_objects=False): branches = draw( dictionaries( keys=branch_names(), - values=optional(branch_targets_d(only_objects=only_objects)), + values=optional(snapshot_targets_d(only_objects=only_objects)), min_size=min_size, max_size=max_size, ) @@ -466,7 +484,7 @@ def snapshots_d(draw, *, min_size=0, max_size=100, only_objects=False): # Override alias branch with one pointing to a real object # if max_size constraint is reached alias = alias_target if len(branches) < max_size else alias_name - branches[alias] = draw(branch_targets_d(only_objects=True)) + branches[alias] = draw(snapshot_targets_d(only_objects=True)) # Ensure no cycles between aliases while True: @@ -480,7 +498,7 @@ def snapshots_d(draw, *, min_size=0, max_size=100, only_objects=False): ) except ValueError as e: for source, target in e.args[1]: - branches[source] = draw(branch_targets_d(only_objects=True)) + branches[source] = draw(snapshot_targets_d(only_objects=True)) else: break diff --git a/swh/model/model.py b/swh/model/model.py index a37300b74a230a62ce6fe591e72c98aa9c97a14b..060b1acdbc297185d6432950ad050ea74fb19557 100644 --- a/swh/model/model.py +++ b/swh/model/model.py @@ -967,7 +967,7 @@ class OriginVisitStatus(BaseModel): return CoreSWHID(object_type=SwhidObjectType.SNAPSHOT, object_id=self.snapshot) -class TargetType(Enum): +class SnapshotTargetType(Enum): """The type of content pointed to by a snapshot branch. Usually a revision or an alias.""" @@ -979,7 +979,12 @@ class TargetType(Enum): ALIAS = "alias" def __repr__(self): - return f"TargetType.{self.name}" + return f"SnapshotTargetType.{self.name}" + + +TargetType = deprecated(version="v6.13.0", reason="Use model.SnapshotTargetType")( + SnapshotTargetType +) class ReleaseTargetType(Enum): @@ -1007,7 +1012,7 @@ class SnapshotBranch(BaseModel): object_type: Final = ModelObjectType.SNAPSHOT_BRANCH target = attr.ib(type=bytes, repr=hash_repr) - target_type = attr.ib(type=TargetType, validator=generic_type_validator) + target_type = attr.ib(type=SnapshotTargetType, validator=generic_type_validator) @target.validator def check_target(self, attribute, value): @@ -1015,18 +1020,18 @@ class SnapshotBranch(BaseModel): valid sha1_git.""" if value.__class__ is not bytes: raise AttributeTypeError(value, attribute) - if self.target_type != TargetType.ALIAS and self.target is not None: + if self.target_type != SnapshotTargetType.ALIAS and self.target is not None: if len(value) != 20: raise ValueError("Wrong length for bytes identifier: %d" % len(value)) @classmethod def from_dict(cls, d): - return cls(target=d["target"], target_type=TargetType(d["target_type"])) + return cls(target=d["target"], target_type=SnapshotTargetType(d["target_type"])) def swhid(self) -> Optional[CoreSWHID]: """Returns a SWHID for the current branch or None if the branch has no target or is an alias.""" - if self.target is None or self.target_type == TargetType.ALIAS: + if self.target is None or self.target_type == SnapshotTargetType.ALIAS: return None return CoreSWHID( object_id=self.target, object_type=SwhidObjectType[self.target_type.name] diff --git a/swh/model/tests/swh_model_data.py b/swh/model/tests/swh_model_data.py index 4e9ec129aaee8a59a3eb42e8483a1ede262318c5..8850a5e0e54d91cbd262edd231a81a1889732f9c 100644 --- a/swh/model/tests/swh_model_data.py +++ b/swh/model/tests/swh_model_data.py @@ -30,7 +30,7 @@ from swh.model.model import ( SkippedContent, Snapshot, SnapshotBranch, - TargetType, + SnapshotTargetType, Timestamp, TimestampWithTimezone, ) @@ -357,7 +357,7 @@ SNAPSHOTS = [ id=hash_to_bytes("9e78d7105c5e0f886487511e2a92377b4ee4c32a"), branches={ b"master": SnapshotBranch( - target_type=TargetType.REVISION, target=REVISIONS[0].id + target_type=SnapshotTargetType.REVISION, target=REVISIONS[0].id ) }, ), @@ -365,21 +365,21 @@ SNAPSHOTS = [ id=hash_to_bytes("0e7f84ede9a254f2cd55649ad5240783f557e65f"), branches={ b"target/revision": SnapshotBranch( - target_type=TargetType.REVISION, + target_type=SnapshotTargetType.REVISION, target=REVISIONS[0].id, ), b"target/alias": SnapshotBranch( - target_type=TargetType.ALIAS, target=b"target/revision" + target_type=SnapshotTargetType.ALIAS, target=b"target/revision" ), b"target/directory": SnapshotBranch( - target_type=TargetType.DIRECTORY, + target_type=SnapshotTargetType.DIRECTORY, target=DIRECTORIES[0].id, ), b"target/release": SnapshotBranch( - target_type=TargetType.RELEASE, target=RELEASES[0].id + target_type=SnapshotTargetType.RELEASE, target=RELEASES[0].id ), b"target/snapshot": SnapshotBranch( - target_type=TargetType.SNAPSHOT, + target_type=SnapshotTargetType.SNAPSHOT, target=hash_to_bytes("9e78d7105c5e0f886487511e2a92377b4ee4c32a"), ), }, diff --git a/swh/model/tests/test_hypothesis_strategies.py b/swh/model/tests/test_hypothesis_strategies.py index e43f3127c8c7bc50c626f6155085ed0cf23d7033..d93f37f8017e2d92dfed2fdcb45f6709279ff117 100644 --- a/swh/model/tests/test_hypothesis_strategies.py +++ b/swh/model/tests/test_hypothesis_strategies.py @@ -20,7 +20,7 @@ from swh.model.hypothesis_strategies import ( skipped_contents, snapshots, ) -from swh.model.model import TargetType +from swh.model.model import SnapshotTargetType target_types = ("content", "directory", "revision", "release", "snapshot", "alias") all_but_skipped_content = ( @@ -174,7 +174,7 @@ def test_snapshots_strategy(snapshot): # check snapshot integrity for name, branch in branches.items(): assert branch is None or branch.target_type.value in target_types - if branch is not None and branch.target_type == TargetType.ALIAS: + if branch is not None and branch.target_type == SnapshotTargetType.ALIAS: aliases.append(name) assert branch.target in branches @@ -184,7 +184,7 @@ def test_snapshots_strategy(snapshot): current_alias = alias while ( branches[current_alias] is not None - and branches[current_alias].target_type == TargetType.ALIAS + and branches[current_alias].target_type == SnapshotTargetType.ALIAS ): assert branches[current_alias].target not in processed_alias processed_alias.add(current_alias) diff --git a/swh/model/tests/test_model.py b/swh/model/tests/test_model.py index 5fb20591c7a27c0983cc2bc60fccddc352711459..b3bbb365730fa9e6cebe797c5b30661a3d9ad9f1 100644 --- a/swh/model/tests/test_model.py +++ b/swh/model/tests/test_model.py @@ -42,7 +42,7 @@ from swh.model.model import ( SkippedContent, Snapshot, SnapshotBranch, - TargetType, + SnapshotTargetType, Timestamp, TimestampWithTimezone, optimized_validator, @@ -287,8 +287,8 @@ _TYPE_VALIDATOR_PARAMETERS: List[Tuple[Any, List[Any], List[Any]]] = [ ), # enums: ( - TargetType, - [TargetType.CONTENT, TargetType.ALIAS], + SnapshotTargetType, + [SnapshotTargetType.CONTENT, SnapshotTargetType.ALIAS], ["content", "alias", 123, None], ), ] @@ -1666,7 +1666,7 @@ def test_snapshot_evolve(snapshot): **snapshot.branches, longest_branch_name + b"x": SnapshotBranch( - target_type=TargetType.RELEASE, + target_type=SnapshotTargetType.RELEASE, target=b"\x00" * 20, ), }