You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Allow to register new class/dataclass/attribute makers in attrs plugin #5406
attrs is very much intended to be composable (e.g. https://github.com/hynek/environ_config) but currently the attrs plugin only works on the core library's functions:
# The names of the different functions that create classes or arguments.
attr_class_makers= {
'attr.s',
'attr.attrs',
'attr.attributes',
}
attr_dataclass_makers= {
'attr.dataclass',
}
attr_attrib_makers= {
'attr.ib',
'attr.attrib',
'attr.attr',
}
I would like to be able to signal somehow to mypy, that some function is a wrapper around @attr.s/@attr.ib/@attr.dataclass. Not only would third-party applications benefit, but I could really use it while experimenting on python-attrs/attrs#408.
FYI in our code I literally append to these lists in a mypy plugin.
I did have a vision that the plugin could support an @class_maker(...) decorator that would do nothing at runtime but let the plugin know that this method (along with some default values) is a class_maker or attrib_maker.
Heh. Here's what I did in our code base. I added a plugin that adds some methods when imported. (Did y'all wonder why I made those variables globals?)
from mypy.plugin import Plugin
from mypy.plugins.attrs import (
attr_attrib_makers,
attr_class_makers,
attr_dataclass_makers,
)
# These work just like attr.dataclass
attr_dataclass_makers.add("my_module.method_looks_like_attr_dataclass")
# This work just like attr.s
attr_class_makers.add("my_module.method_looks_like_attr_s")
# These are our attr.ib makers.
attr_attrib_makers.add("my_module.method_looks_like_attrib")
class MyPlugin(Plugin):
# Our plugin does nothing but it has to exist so this file gets loaded.
pass
def plugin(version):
return MyPlugin
Activity
euresti commentedon Jul 31, 2018
FYI in our code I literally append to these lists in a mypy plugin.
I did have a vision that the plugin could support an
@class_maker(...)decorator that would do nothing at runtime but let the plugin know that this method (along with some default values) is aclass_makerorattrib_maker.So you'd do:
Or something like that.
ilevkivskyi commentedon Aug 1, 2018
Another possible pattern we can support is:
ilevkivskyi commentedon Jan 22, 2019
The same problem exists for dataclasses, see #6239
ilevkivskyi commentedon May 8, 2019
Just FTR cross-posting the workaround that currently works:
but it is arguably worse than #5406 (comment), that currently doesn't work.
euresti commentedon May 8, 2019
Heh. Here's what I did in our code base. I added a plugin that adds some methods when imported. (Did y'all wonder why I made those variables globals?)
Hah. Get mypy to not complain incorrectly about init args.
Hah. Get mypy to not complain incorrectly about init args.
Hah. Get mypy to not complain incorrectly about init args.
LecrisUT commentedon Apr 9, 2026
A checkup since it has been quite a long time. Are there any updates or cleaner workarounds?
I found from the mentions a cleaner one