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

Make Converter a kind of adapter, fix converters.pipe #1328

Merged
merged 7 commits into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
29 changes: 16 additions & 13 deletions src/attr/_make.py
Original file line number Diff line number Diff line change
Expand Up @@ -2712,7 +2712,6 @@ class Converter:
"takes_field",
"_first_param_type",
"_global_name",
"__call__",
)

def __init__(self, converter, *, takes_self=False, takes_field=False):
Expand All @@ -2724,6 +2723,21 @@ def __init__(self, converter, *, takes_self=False, takes_field=False):
converter
).get_first_param_type()

def __call__(self, value: object, instance: object, field: Attribute):
"""
Call the converter with the appropriate subset of *instance* and
*field*.
"""
return self.converter(
value,
*{
hynek marked this conversation as resolved.
Show resolved Hide resolved
(False, False): (),
(True, False): (instance,),
(False, True): (field,),
(True, True): (instance, field),
}[self.takes_self, self.takes_field],
)

@staticmethod
def _get_global_name(attr_name: str) -> str:
"""
Expand Down Expand Up @@ -2943,18 +2957,7 @@ def pipe(*converters):

def pipe_converter(val, inst, field):
for c in converters:
if isinstance(c, Converter):
val = c.converter(
val,
*{
(False, False): (),
(True, False): (c.takes_self,),
(False, True): (c.takes_field,),
(True, True): (c.takes_self, c.takes_field),
}[c.takes_self, c.takes_field],
)
else:
val = c(val)
val = c(val, inst, field) if isinstance(c, Converter) else c(val)

return val

Expand Down
9 changes: 7 additions & 2 deletions src/attr/setters.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
Commonly used hooks for on_setattr.
"""


from . import _config
from .exceptions import FrozenAttributeError

Expand Down Expand Up @@ -63,7 +62,13 @@ def convert(instance, attrib, new_value):
"""
c = attrib.converter
if c:
return c(new_value)
# This can be removed once we drop 3.8 and use attrs.Converter instead.
from ._make import Converter

if not isinstance(c, Converter):
return c(new_value)

return c(new_value, instance, attrib)

return new_value

Expand Down
36 changes: 35 additions & 1 deletion tests/test_setattr.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,16 +109,34 @@ def test_pipe(self):
used. They can be supplied using the pipe functions or by passing a
list to on_setattr.
"""
taken = None

def takes_all(val, instance, attrib):
nonlocal taken
taken = val, instance, attrib

return val

s = [setters.convert, lambda _, __, nv: nv + 1]

@attr.s
class Piped:
x1 = attr.ib(converter=int, on_setattr=setters.pipe(*s))
x1 = attr.ib(
converter=[
attr.Converter(
takes_all, takes_field=True, takes_self=True
),
int,
],
on_setattr=setters.pipe(*s),
)
x2 = attr.ib(converter=int, on_setattr=s)

p = Piped("41", "22")

assert ("41", p) == taken[:-1]
assert "x1" == taken[-1].name

assert 41 == p.x1
assert 22 == p.x2

Expand Down Expand Up @@ -417,3 +435,19 @@ def test_docstring(self):
"Method generated by attrs for class WithOnSetAttrHook."
== WithOnSetAttrHook.__setattr__.__doc__
)

def test_setattr_converter_piped(self):
"""
If a converter is used, it is piped through the on_setattr hooks.

Regression test for https://github.com/python-attrs/attrs/issues/1327
"""

@attr.define # converter on setattr is implied in NG
class C:
x = attr.field(converter=[int])

c = C("1")
c.x = "2"

assert 2 == c.x