diff --git a/swh/model/from_disk.py b/swh/model/from_disk.py --- a/swh/model/from_disk.py +++ b/swh/model/from_disk.py @@ -121,7 +121,7 @@ """ __slots__ = [] # type: List[str] - type = "content" + object_type: Final = "content" @classmethod def from_bytes(cls, *, mode, data): @@ -300,7 +300,7 @@ """ __slots__ = ["__entries"] - type = "directory" + object_type: Final = "directory" @classmethod def from_disk( @@ -352,14 +352,14 @@ @staticmethod def child_to_directory_entry(name, child): - if isinstance(child, Directory): + if child.object_type == "directory": return { "type": "dir", "perms": DentryPerms.directory, "target": child.hash, "name": name, } - elif isinstance(child, Content): + elif child.object_type == "content": return { "type": "file", "perms": child.data["perms"], @@ -367,7 +367,7 @@ "name": name, } else: - raise ValueError("unknown child") + raise ValueError(f"unknown child {child}") def get_data(self, **kwargs): return { diff --git a/swh/model/merkle.py b/swh/model/merkle.py --- a/swh/model/merkle.py +++ b/swh/model/merkle.py @@ -1,4 +1,4 @@ -# Copyright (C) 2017 The Software Heritage developers +# Copyright (C) 2017-2020 The Software Heritage developers # See the AUTHORS file at the top-level directory of this distribution # License: GNU General Public License version 3, or any later version # See top-level LICENSE file for more information @@ -8,7 +8,7 @@ import abc import collections -from typing import Iterator, List, Optional, Set +from typing import Iterator, List, Set def deep_update(left, right): @@ -111,7 +111,6 @@ __slots__ = ["parents", "data", "__hash", "collected"] - type = None # type: Optional[str] # TODO: make this an enum """Type of the current node (used as a classifier for :func:`collect`)""" def __init__(self, data=None): @@ -234,7 +233,7 @@ """ if not self.collected: self.collected = True - return {self.type: {self.hash: self.get_data(**kwargs)}} + return {self.object_type: {self.hash: self.get_data(**kwargs)}} else: return {} diff --git a/swh/model/tests/test_merkle.py b/swh/model/tests/test_merkle.py --- a/swh/model/tests/test_merkle.py +++ b/swh/model/tests/test_merkle.py @@ -1,4 +1,4 @@ -# Copyright (C) 2017 The Software Heritage developers +# Copyright (C) 2017-2020 The Software Heritage developers # See the AUTHORS file at the top-level directory of this distribution # License: GNU General Public License version 3, or any later version # See top-level LICENSE file for more information @@ -9,7 +9,7 @@ class MerkleTestNode(merkle.MerkleNode): - type = "tested_merkle_node_type" + object_type = "tested_merkle_node_type" def __init__(self, data): super().__init__(data) @@ -23,7 +23,7 @@ class MerkleTestLeaf(merkle.MerkleLeaf): - type = "tested_merkle_leaf_type" + object_type = "tested_merkle_leaf_type" def __init__(self, data): super().__init__(data) @@ -62,7 +62,11 @@ collected = self.instance.collect() self.assertEqual( collected, - {self.instance.type: {self.instance.hash: self.instance.get_data(),},}, + { + self.instance.object_type: { + self.instance.hash: self.instance.get_data(), + }, + }, ) collected2 = self.instance.collect() self.assertEqual(collected2, {}) @@ -162,7 +166,7 @@ def test_collect(self): collected = self.root.collect() - self.assertEqual(len(collected[self.root.type]), len(self.nodes)) + self.assertEqual(len(collected[self.root.object_type]), len(self.nodes)) for node in self.nodes.values(): self.assertTrue(node.collected) collected2 = self.root.collect() @@ -229,7 +233,7 @@ # Ensure we collected root, root/b, and both new children collected_after_update = self.root.collect() self.assertCountEqual( - collected_after_update[MerkleTestNode.type], + collected_after_update[MerkleTestNode.object_type], [ self.nodes[b"root"].hash, self.nodes[b"root/b"].hash,