Skip to content

Commit 13105a6

Browse files
authored
Use an explicit comparison to None for the converter of a field (#1374)
Make attrs work with a converter instance that does not evaluate True
1 parent e21793e commit 13105a6

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

src/attr/_make.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2021,7 +2021,7 @@ def _attrs_to_init_script(
20212021
has_factory = isinstance(a.default, Factory)
20222022
maybe_self = "self" if has_factory and a.default.takes_self else ""
20232023

2024-
if a.converter and not isinstance(a.converter, Converter):
2024+
if a.converter is not None and not isinstance(a.converter, Converter):
20252025
converter = Converter(a.converter)
20262026
else:
20272027
converter = a.converter

tests/test_converters.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,25 @@ def wrapped(_, __, ___) -> float:
112112
assert float is c.__call__.__annotations__["return"]
113113
assert None is c2.__call__.__annotations__.get("return")
114114

115+
def test_falsey_converter(self):
116+
"""
117+
Passing a false-y instance still produces a valid converter.
118+
"""
119+
120+
class MyConv:
121+
def __bool__(self):
122+
return False
123+
124+
def __call__(self, value):
125+
return value * 2
126+
127+
@attr.s
128+
class C:
129+
a = attrib(converter=MyConv())
130+
131+
c = C(21)
132+
assert 42 == c.a
133+
115134

116135
class TestOptional:
117136
"""

0 commit comments

Comments
 (0)