Skip to content

Classmethods as attrs converters #19520

@AdrianSosic

Description

@AdrianSosic

Hi, this is a copy of an issue originally opened in the attrs repo, where it was pointed out that the limitation really comes from the mypy plugin. While the below is just a toy example, the problem was originally noticed in real production-ready code base of mine. It would be great if the use case could be supported in the future!

Feature

from __future__ import annotations

from attrs import define, field


@define
class LeContainer:
    attr: int

    @classmethod
    def make(cls, a: int) -> LeContainer:
        return LeContainer(a)


@define
class LeClass:
    # x: LeContainer = field(converter=LeContainer)  <-- This works
    x: LeContainer = field(converter=LeContainer.make)  # <-- This makes mypy complain

Error Message:

error: Unsupported converter, only named functions, types and lambdas are currently supported  [misc]

Pitch

Extending the allowed converter types and thus getting rid of this limitation would allow for a broader / more flexible use of converters.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions