-
-
Notifications
You must be signed in to change notification settings - Fork 385
Way to change defaults? #630
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
For you particular use case you can use our easter egg (that mypy knows about) I guess being able to tell mypy about different options is a feature in mypy? Introducing global preferences for attrs is impossible, since too many projects in the ecosystem rely on it and that would cause quite the havoc. I honestly find this frustrating too, since before typing, I had a bunch of pre-configured attrs decorators but that's all gone now. :| |
+1 here. As a convention, we use |
As said, that's all up to mypy, not us. If we adopt mypy's attrs pluging as we discuss in #480, we might have a chance to do this. It really depends on who is gonna be willing to help out tho, since I have no knowledge about mypy plugins and I'm not sure I have enough cycles free to learn it. |
To be clear, we can make a pull request on mypy to fix this. And what I have done to solve this problem is the following. Add a mypy plugin to your codebase that looks like this:
This won't let you change the defaults but if your methods take more arguments or have names you prefer you can use them and still get typechecking. This is a python/mypy#5406 mypy issue that talks about the problem. Edit: the argument to attr_class_makers.add line was incorrect. |
@euresti Interesting. Our current codebases have the problem that mypy doesn't recognize attrs wrappers. I've written a wrapper library for https://en.wikipedia.org/wiki/FlatBuffers that depends on attrs under the hood (things like this are a superpower of attrs with type definitions)[1]. You define classes like this:
and it'll create an attrs class (under the hood it calls |
@euresti I have trouble understanding that code and your comment fully; why does it allow changing argument names but not changing defaults? |
The mypy plugin basically detects calls to functions in Sadly the only thing it handles is auto_attribs having a True or False default. |
@euresti Just wanted to say thanks for sharing the snippet above. I've successfully integrated it into one of our flattrs-using codebases. Wish stuff like this was more publicly documented though! |
Any suggestions where to put it Tin? 😇 |
I read this page on the mypy docs, https://mypy.readthedocs.io/en/stable/additional_features.html which says that you can't simply write a function like this:
And expect things to type check correctly.
Is there any convenient, mypy compatible way, to change defaults in attrs? I saw the RFC about defaults being inconvenient; better defaults (above all auto_attribs=True) will help but of course people will not necessarily agree on the best defaults, hence why being able to wrap it would be such a win.
It looks like pydantic has done it (https://github.com/samuelcolvin/pydantic/blob/master/pydantic/dataclasses.py), but it's hard for me to separate out the bits that are related to their extra functionality, from the bare minimum.
The text was updated successfully, but these errors were encountered: