Make attrs work with a converter instance that does not evaluate True

This commit is contained in:
Filipe Brandenburger 2024-11-14 23:18:58 -05:00
parent e21793e90a
commit 50cba80b21
2 changed files with 20 additions and 1 deletions

View File

@ -2021,7 +2021,7 @@ def _attrs_to_init_script(
has_factory = isinstance(a.default, Factory)
maybe_self = "self" if has_factory and a.default.takes_self else ""
if a.converter and not isinstance(a.converter, Converter):
if a.converter is not None and not isinstance(a.converter, Converter):
converter = Converter(a.converter)
else:
converter = a.converter

View File

@ -112,6 +112,25 @@ class TestConverter:
assert float is c.__call__.__annotations__["return"]
assert None is c2.__call__.__annotations__.get("return")
def test_falsey_converter(self):
"""
Passing a false-y instance still produces a valid converter.
"""
class MyConv:
def __bool__(self):
return False
def __call__(self, value):
return value * 2
@attr.s
class C:
a = attrib(converter=MyConv())
c = C(21)
assert 42 == c.a
class TestOptional:
"""