Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use an explicit comparison to None for the converter of a field #1374

Merged
merged 1 commit into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/attr/_make.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 19 additions & 0 deletions tests/test_converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
"""
Expand Down