From 50cba80b21054dec3ee42d00ea4a84d86c36c2a7 Mon Sep 17 00:00:00 2001 From: Filipe Brandenburger Date: Thu, 14 Nov 2024 23:18:58 -0500 Subject: [PATCH] Make attrs work with a converter instance that does not evaluate True --- src/attr/_make.py | 2 +- tests/test_converters.py | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/attr/_make.py b/src/attr/_make.py index e0cd9e0b..13cc9943 100644 --- a/src/attr/_make.py +++ b/src/attr/_make.py @@ -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 diff --git a/tests/test_converters.py b/tests/test_converters.py index d4aca430..4747f13c 100644 --- a/tests/test_converters.py +++ b/tests/test_converters.py @@ -112,6 +112,25 @@ def wrapped(_, __, ___) -> float: 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: """