Skip to content

Commit

Permalink
Fix the check for None type in inspect._get_parser (#96)
Browse files Browse the repository at this point in the history
Fix the check for `None` type in `inspect._get_parser`
  • Loading branch information
jdidion authored Feb 22, 2024
1 parent 94b3696 commit bd2b283
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
5 changes: 4 additions & 1 deletion fgpyo/util/inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ def split_at_given_level(
return out_vals


NoneType = type(None)


def _get_parser(
cls: Type, type_: TypeAlias, parsers: Optional[Dict[type, Callable[[str], Any]]] = None
) -> partial:
Expand Down Expand Up @@ -226,7 +229,7 @@ def dict_parse(dict_string: str) -> Dict[Any, Any]:
return types.make_enum_parser(type_)
elif types.is_constructible_from_str(type_):
return functools.partial(type_)
elif isinstance(type_, type(type(None))):
elif type_ == NoneType:
return functools.partial(types.none_parser)
elif types.get_origin_type(type_) is Union:
return types.make_union_parser(
Expand Down
21 changes: 21 additions & 0 deletions fgpyo/util/tests/test_inspect.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from typing import Optional

import attr
import pytest

from fgpyo.util.inspect import attr_from
from fgpyo.util.inspect import attribute_has_default
Expand Down Expand Up @@ -45,3 +46,23 @@ def test_attribute_has_default() -> None:
assert attribute_has_default(fields_dict["optional_no_default"])
assert attribute_has_default(fields_dict["optional_with_default_none"])
assert attribute_has_default(fields_dict["optional_with_default_some"])


class Foo:
pass


@attr.s(auto_attribs=True, frozen=True)
class Bar:
foo: Foo


# Test for regression #94 - the call to attr_from succeeds when the check for None type
# in inspect._get_parser is done incorrectly.
def test_attr_from_custom_type_without_parser_fails() -> None:
with pytest.raises(AssertionError):
attr_from(
cls=Bar,
kwargs={"foo": ""},
parsers={},
)

0 comments on commit bd2b283

Please sign in to comment.