Skip to content

Commit f6dbeb7

Browse files
kmichel-aivenshinnar
authored andcommitted
Fix JSONTop() == 1 and JSONbot() == 0
The == operator in `is_top` and `is_bot` causes type confusion when comparing `JSONTop()` and `JSONbot()` values. Fix it by using identity test instead of equality.
1 parent 8c85130 commit f6dbeb7

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

Diff for: jsonsubschema/_checkers.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ def __bool__(self):
235235

236236

237237
def is_top(obj):
238-
return obj == True or obj == {} or isinstance(obj, JSONtop)
238+
return obj is True or obj == {} or isinstance(obj, JSONtop)
239239

240240

241241
class JSONbot(JSONschema):
@@ -275,7 +275,7 @@ def __bool__(self):
275275

276276

277277
def is_bot(obj):
278-
return obj == False \
278+
return obj is False \
279279
or (utils.is_dict(obj) and obj.get("not") == {}) \
280280
or isinstance(obj, JSONbot) \
281281
or (isinstance(obj, JSONschema) and obj.isUninhabited())

Diff for: test/test_checkers.py

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
from jsonsubschema._canonicalization import simplify_schema_and_embed_checkers
2+
from jsonsubschema._checkers import JSONbot, JSONtop, is_bot, is_top
3+
import unittest
4+
5+
6+
class TestIsTop(unittest.TestCase):
7+
def test_true_is_top(self) -> None:
8+
self.assertTrue(is_top(True))
9+
10+
def test_empty_object_is_top(self) -> None:
11+
self.assertTrue(is_top({}))
12+
13+
def test_json_top_is_top(self) -> None:
14+
self.assertTrue(is_top(JSONtop()))
15+
16+
def test_one_is_not_top(self) -> None:
17+
self.assertFalse(is_top(1))
18+
19+
20+
class TestIsBot(unittest.TestCase):
21+
def test_false_is_bot(self) -> None:
22+
self.assertTrue(is_bot(False))
23+
24+
def test_not_empty_object_is_bot(self) -> None:
25+
self.assertTrue(is_bot({"not": {}}))
26+
27+
def test_json_bot_is_bot(self) -> None:
28+
self.assertTrue(is_bot(JSONbot()))
29+
30+
def test_uninhabited_schema_is_bot(self) -> None:
31+
uninhabited_schema = simplify_schema_and_embed_checkers({"type": "integer", "minimum": 2, "maximum": 1})
32+
self.assertTrue(is_bot(uninhabited_schema))
33+
34+
def test_zero_is_not_bot(self) -> None:
35+
self.assertFalse(is_bot(0))

0 commit comments

Comments
 (0)