Add support for classmethod converters in attrs plugin
import attr
@attr.s
class Bar:
b = attr.ib()
@classmethod
def make(cls, one: int) -> 'Bar':
return cls(one)
@attr.s
class Foo:
a = attr.ib(converter=Bar.make)
error: Unsupported converter, only named functions and types are currently supported
Attrs is perfectly happy with make
>>> Foo(1)
Foo(a=Bar(b=1))
Granted the converter limitations are a known issue, I couldn't find this use-case covered anywhere.
Sounds like a reasonable idea.
Surprised this doesn't already work. Class methods are the best way to construct many types, and are recommended by attrs. Is it especially hard to do this?
Superficially looking at the code, it appears that every "case" has very specific code handling it. I haven't dug deep to understand why it's this way.
If PEP 712 is accepted and implemented in mypy, this functionality will come "for free" because attrs uses dataclass_transform.