Skip to content

Commit

Permalink
add test for class check
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielYang59 committed Oct 21, 2024
1 parent befffc1 commit 2b7e3e3
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/monty/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,16 +105,16 @@ class B(A):
list all the types that an object can resolve to in order of generality
(all objects have the builtins.object as the last one).
"""
# This function is intended as an alternative of "isinstance",
# therefore wouldn't check class
if isclass(obj):
return False

type_str = type_str if isinstance(type_str, tuple) else (type_str,)

mro = type(obj).mro()

return any(
o.__module__ + "." + o.__qualname__ == ts for o in mro for ts in type_str
)
return any(f"{o.__module__}.{o.__qualname__}" == ts for o in mro for ts in type_str)


class MSONable:
Expand Down
20 changes: 20 additions & 0 deletions tests/test_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -1096,6 +1096,26 @@ class B(A):
assert _check_type(b, class_name_A)
assert isinstance(b, A)

def test_check_type_with_class(self):
class A:
pass

class B(A):
pass

class_name_A = f"{A.__module__}.{A.__qualname__}"
class_name_B = f"{B.__module__}.{B.__qualname__}"

# Test class behavior (should return False, like isinstance does)
assert not _check_type(A, class_name_A)
assert not _check_type(B, class_name_B)
assert not _check_type(B, class_name_A)

# Ensure isinstance gives the same result (isinstance doesn't work with classes)
assert not isinstance(A, A)
assert not isinstance(B, B)
assert not isinstance(B, A)

def test_callable(self):
# Test function
def my_function():
Expand Down

0 comments on commit 2b7e3e3

Please sign in to comment.