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

Add unit test for helper function json._check_type #716

Merged
merged 14 commits into from
Dec 10, 2024
Next Next commit
add type annotation
DanielYang59 committed Oct 21, 2024
commit 20149581f03d1656eb6a5aed0d3143900f5b9beb
8 changes: 3 additions & 5 deletions src/monty/json.py
Original file line number Diff line number Diff line change
@@ -79,12 +79,12 @@ def _load_redirect(redirect_file):
return dict(redirect_dict)


def _check_type(obj, type_str) -> bool:
def _check_type(obj: object, type_str: str | tuple[str, ...]) -> bool:
"""Alternative to isinstance that avoids imports.

Checks whether obj is an instance of the type defined by type_str. This
removes the need to explicitly import type_str. Handles subclasses like
isinstance does. E.g.::
isinstance does. E.g.:
class A:
pass

@@ -99,10 +99,8 @@ class B(A):
assert isinstance(b, A)
assert not isinstance(a, B)

type_str: str | tuple[str]

Note for future developers: the type_str is not always obvious for an
object. For example, pandas.DataFrame is actually pandas.core.frame.DataFrame.
object. For example, pandas.DataFrame is actually "pandas.core.frame.DataFrame".
To find out the type_str for an object, run type(obj).mro(). This will
list all the types that an object can resolve to in order of generality
(all objects have the builtins.object as the last one).