Skip to content

Support classmethod and staticmethod as converters in attrs plugin#21548

Open
mondeja wants to merge 1 commit into
python:masterfrom
mondeja:attrs-classmethod-staticmethod-converter
Open

Support classmethod and staticmethod as converters in attrs plugin#21548
mondeja wants to merge 1 commit into
python:masterfrom
mondeja:attrs-classmethod-staticmethod-converter

Conversation

@mondeja
Copy link
Copy Markdown

@mondeja mondeja commented May 27, 2026

Adds support for passing staticmethod / classmethod with no decorators to converter in attrs.

Is sound only when the member access is the descriptor's canonical counit (the evaluation of its declared signature for classmethod, the identity for staticmethod) i.e. when there are no other decorators, since there the inferred type coincides by construction with the type checker's.


I'm currently writing

def _to_foo(value: Any) -> Foo:
    ...

@attrs.define
class MyAttrClass:
    foo: Foo = field(
        factory=Foo,
        converter=_to_foo,
    )

but I would prefer just

@attrs.define
class MyAttrClass:
    foo: Foo = field(
        factory=Foo,
        converter=Foo.from_value,
    )

as from_value is a real converter factory.

@github-actions
Copy link
Copy Markdown
Contributor

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant