Skip to content

Commit 59705dd

Browse files
authored
Merge pull request #34 from trailofbits/21-nonetype
Adds support for null and None types
2 parents 37d1200 + 60854b4 commit 59705dd

File tree

5 files changed

+44
-3
lines changed

5 files changed

+44
-3
lines changed

docs/_templates/layout.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
{% endfor %}
1818
{% else %}
1919
<dd><a href="/graphtage/latest">latest</a></dd>
20+
<dd><a href="/graphtage/v0.2.1">0.2.2</a></dd>
2021
<dd><a href="/graphtage/v0.2.1">0.2.1</a></dd>
2122
<dd><a href="/graphtage/v0.2.0">0.2.0</a></dd>
2223
<dd><a href="/graphtage/v0.1.1">0.1.1</a></dd>

graphtage/graphtage.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -832,6 +832,37 @@ def __init__(self, bool_like: bool):
832832
super().__init__(bool_like)
833833

834834

835+
class NullNode(LeafNode):
836+
"""A node representing a null or :const:`None` type."""
837+
838+
def __init__(self):
839+
super().__init__(None)
840+
841+
def calculate_total_size(self) -> int:
842+
return 0
843+
844+
def edits(self, node: TreeNode) -> Edit:
845+
if isinstance(node, NullNode):
846+
return Match(self, node, 0)
847+
else:
848+
return Replace(self, node)
849+
850+
def __lt__(self, other):
851+
if isinstance(other, NullNode):
852+
return False
853+
else:
854+
return True
855+
856+
def __eq__(self, other):
857+
return isinstance(other, NullNode)
858+
859+
def __hash__(self):
860+
return hash(self.object)
861+
862+
def __repr__(self):
863+
return f"{self.__class__.__name__}()"
864+
865+
835866
def string_edit_distance(s1: str, s2: str) -> EditDistance:
836867
"""A convenience function for computing the edit distance between two strings.
837868

graphtage/json.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from typing import Optional, Union
1212

1313
from .graphtage import BoolNode, BuildOptions, DictNode, Filetype, FixedKeyDictNode, \
14-
FloatNode, IntegerNode, KeyValuePairNode, LeafNode, ListNode, StringFormatter, StringNode
14+
FloatNode, IntegerNode, KeyValuePairNode, LeafNode, ListNode, NullNode, StringFormatter, StringNode
1515
from .printer import Fore, Printer
1616
from .sequences import SequenceFormatter
1717
from .tree import ContainerNode, GraphtageFormatter, TreeNode
@@ -66,6 +66,8 @@ def build_tree(
6666
return DictNode.from_dict(dict_items)
6767
else:
6868
return FixedKeyDictNode.from_dict(dict_items)
69+
elif python_obj is None:
70+
return NullNode()
6971
else:
7072
raise ValueError(f"Unsupported Python object {python_obj!r} of type {type(python_obj)}")
7173

graphtage/version.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def git_branch() -> Optional[str]:
4848
return None
4949

5050

51-
DEV_BUILD = True
51+
DEV_BUILD = False
5252
"""Sets whether this build is a development build.
5353
5454
This should only be set to :const:`False` to coincide with a release. It should *always* be :const:`False` before
@@ -59,7 +59,7 @@ def git_branch() -> Optional[str]:
5959
"""
6060

6161

62-
__version__: Tuple[Union[int, str], ...] = (0, 3, 0)
62+
__version__: Tuple[Union[int, str], ...] = (0, 2, 2)
6363

6464
if DEV_BUILD:
6565
branch_name = git_branch()

test/test_graphtage.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,10 @@ def test_empty_list(self):
103103
self.assertEqual(1, len(diff.edit_list))
104104
self.assertIsInstance(diff.edit_list[0], graphtage.Match)
105105
self.assertEqual(0, diff.edit_list[0].bounds().upper_bound)
106+
107+
def test_null_json(self):
108+
diff = graphtage.json.build_tree([None]).diff(graphtage.json.build_tree([1]))
109+
self.assertIsInstance(diff, graphtage.ListNode)
110+
self.assertIsInstance(diff, graphtage.tree.EditedTreeNode)
111+
self.assertEqual(1, len(diff.edit_list))
112+
self.assertIsInstance(diff.edit_list[0], graphtage.FixedLengthSequenceEdit)

0 commit comments

Comments
 (0)