pythonpython-typingmypypython-attrs

Unsupported converter, only named functions, types and lambdas are currently supported


Using the complicated example as here https://www.attrs.org/en/stable/api.html#attrs.converters.default_if_none, I get the mypy error: Unsupported converter, only named functions, types and lambdas are currently supported

def complicated(value, self_, field):
    return int(value) * self_.factor + field.metadata["offset"]
@define
class C:
    factor = 5  # not an *attrs* field
    x = field(
        metadata={"offset": 200},
        converter=attrs.Converter(
            complicated,
            takes_self=True, takes_field=True
    ))
C("42")
C(x=410)

Solution

  • mypy just doesn't support that. There's a lot of perfectly safe, valid, fine code that mypy doesn't support. You can tell mypy to ignore it with a # type: ignore comment, or you can stop writing your code like that.

    There isn't really a way to get the same converter behavior without attrs.Converter. You could hardcode the offset, but if you try to hardcode factor, your converter will behave differently if you reassign an instance's factor and then try to reassign x. You'd probably be stuck ditching attrs and writing a custom __init__ and a custom @property.