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

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy