You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Type annotations for attrs.field(converter=...) incorrectly reject tuples #1460
Converters can currently be specified as _ConverterType, list[_ConverterType], or tuple[_ConverterType]. The last of these does not mean "tuple of _ConverterType instances" as was likely intended; it specifies a tuple with exactly one _ConverterType element. The correct form for a tuple with any number of instances is tuple[_ConverterType, ...].
A pull request will be created shortly.
I was tempted to change the parameter's type by replacing both the list and tuple expressions with a unified Sequence[_ConverterType], but that currently doesn't work at runtime (attrs seems to treat sequences other than list and tuple as if they should be callable converters, rather than an ordered collection of converters):
Traceback (most recent call last):
File "<frozen runpy>", line 198, in _run_module_as_main
File "<frozen runpy>", line 88, in _run_code
File "..../bug.py", line 56, in <module>
inst = SomeClass(3)
^^^^^^^^^^^^
File "<attrs generated methods __main__.SomeClass>", line 24, in __init__
TypeError: 'ThirdSequenceType' object is not callable
If we wrap the argument to converter= in tuple(), i.e., change the last few lines to:
... then everything works as expected. My interpretation is that ThirdSequenceType is a perfectly well-behaved Sequence, but not one that attrs.field(converter=...) is willing to accept.
Activity
finite-state-machine commentedon Aug 28, 2025
I've submitted a PR as #1461.