Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix JSONTop() == 1 and JSONbot() == 0 #31

Merged
merged 1 commit into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions jsonsubschema/_checkers.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ def __bool__(self):


def is_top(obj):
return obj == True or obj == {} or isinstance(obj, JSONtop)
return obj is True or obj == {} or isinstance(obj, JSONtop)


class JSONbot(JSONschema):
Expand Down Expand Up @@ -275,7 +275,7 @@ def __bool__(self):


def is_bot(obj):
return obj == False \
return obj is False \
or (utils.is_dict(obj) and obj.get("not") == {}) \
or isinstance(obj, JSONbot) \
or (isinstance(obj, JSONschema) and obj.isUninhabited())
Expand Down
35 changes: 35 additions & 0 deletions test/test_checkers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from jsonsubschema._canonicalization import simplify_schema_and_embed_checkers
from jsonsubschema._checkers import JSONbot, JSONtop, is_bot, is_top
import unittest


class TestIsTop(unittest.TestCase):
def test_true_is_top(self) -> None:
self.assertTrue(is_top(True))

def test_empty_object_is_top(self) -> None:
self.assertTrue(is_top({}))

def test_json_top_is_top(self) -> None:
self.assertTrue(is_top(JSONtop()))

def test_one_is_not_top(self) -> None:
self.assertFalse(is_top(1))


class TestIsBot(unittest.TestCase):
def test_false_is_bot(self) -> None:
self.assertTrue(is_bot(False))

def test_not_empty_object_is_bot(self) -> None:
self.assertTrue(is_bot({"not": {}}))

def test_json_bot_is_bot(self) -> None:
self.assertTrue(is_bot(JSONbot()))

def test_uninhabited_schema_is_bot(self) -> None:
uninhabited_schema = simplify_schema_and_embed_checkers({"type": "integer", "minimum": 2, "maximum": 1})
self.assertTrue(is_bot(uninhabited_schema))

def test_zero_is_not_bot(self) -> None:
self.assertFalse(is_bot(0))