Skip to content

Commit bd2b283

Browse files
authored
Fix the check for None type in inspect._get_parser (#96)
Fix the check for `None` type in `inspect._get_parser`
1 parent 94b3696 commit bd2b283

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

fgpyo/util/inspect.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ def split_at_given_level(
7070
return out_vals
7171

7272

73+
NoneType = type(None)
74+
75+
7376
def _get_parser(
7477
cls: Type, type_: TypeAlias, parsers: Optional[Dict[type, Callable[[str], Any]]] = None
7578
) -> partial:
@@ -226,7 +229,7 @@ def dict_parse(dict_string: str) -> Dict[Any, Any]:
226229
return types.make_enum_parser(type_)
227230
elif types.is_constructible_from_str(type_):
228231
return functools.partial(type_)
229-
elif isinstance(type_, type(type(None))):
232+
elif type_ == NoneType:
230233
return functools.partial(types.none_parser)
231234
elif types.get_origin_type(type_) is Union:
232235
return types.make_union_parser(

fgpyo/util/tests/test_inspect.py

+21
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from typing import Optional
22

33
import attr
4+
import pytest
45

56
from fgpyo.util.inspect import attr_from
67
from fgpyo.util.inspect import attribute_has_default
@@ -45,3 +46,23 @@ def test_attribute_has_default() -> None:
4546
assert attribute_has_default(fields_dict["optional_no_default"])
4647
assert attribute_has_default(fields_dict["optional_with_default_none"])
4748
assert attribute_has_default(fields_dict["optional_with_default_some"])
49+
50+
51+
class Foo:
52+
pass
53+
54+
55+
@attr.s(auto_attribs=True, frozen=True)
56+
class Bar:
57+
foo: Foo
58+
59+
60+
# Test for regression #94 - the call to attr_from succeeds when the check for None type
61+
# in inspect._get_parser is done incorrectly.
62+
def test_attr_from_custom_type_without_parser_fails() -> None:
63+
with pytest.raises(AssertionError):
64+
attr_from(
65+
cls=Bar,
66+
kwargs={"foo": ""},
67+
parsers={},
68+
)

0 commit comments

Comments
 (0)