Skip to content

Commit

Permalink
Added try except statement around isinstance check to avoid excepti…
Browse files Browse the repository at this point in the history
…on (#627)

* Added try except statement around `isinstance` check to avoid exception.
  • Loading branch information
rubenthoms authored Sep 16, 2022
1 parent da235f9 commit c07833d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
9 changes: 9 additions & 0 deletions tests/test_callback_typecheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,12 @@ def expect_union(arg: Union[str, int]) -> Union[str, int]:
assert isinstance(callback_typecheck(expect_union)(1.5), str)

############################################################

def expect_union_list(arg: Union[List[str], str]) -> Union[List[str], str]:
return arg

assert isinstance(callback_typecheck(expect_union_list)(["1", "2"]), list)
assert isinstance(callback_typecheck(expect_union_list)(["1", "2"])[0], str)
assert isinstance(callback_typecheck(expect_union_list)("1"), str)

############################################################
9 changes: 6 additions & 3 deletions webviz_config/utils/_callback_typecheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class ConversionError(Exception):


def convert(arg: Any, convert_to: T) -> T:
# pylint: disable=too-many-return-statements, too-many-branches
# pylint: disable=too-many-return-statements, too-many-branches, too-many-nested-blocks
additional_error_message: str = ""
try:
if convert_to is None and arg is None:
Expand Down Expand Up @@ -57,8 +57,11 @@ def convert(arg: Any, convert_to: T) -> T:
if get_origin(convert_to) is Union:
if "__args__" in dir(convert_to):
for convert_type in convert_to.__args__: # type: ignore[attr-defined]
if isinstance(arg, convert_type):
return arg
try:
if isinstance(arg, convert_type):
return arg
except TypeError:
pass
for convert_type in convert_to.__args__: # type: ignore[attr-defined]
try:
return convert(arg, convert_type)
Expand Down

0 comments on commit c07833d

Please sign in to comment.