-
-
Notifications
You must be signed in to change notification settings - Fork 386
Typing: include field type in Attribute type signature #729
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
That would be a thing for the mypy plugin, no? Maybe it would be possible if we follow through with #421? |
Yes probably, did I file this in the wrong place? I'm not sure where the mypy plugin is developed. |
Nobody is. 🤪 They merged @euresti's PR but I think we should still take it over. Unfortunately the "we" doesn't include me, because this is all way above my head. |
This is above my head too but the potential benefits are too great, I guess I'll have to start reading 😇 |
Hi. Yes this is definitely something a Plugin would have to do. I've been trying to find some free time to move the plugin over here. But I still need to figure out how to move the tests over. (Our current testing of the stubs doesn't cover error it only tests that code doesn't fail IIRC) |
Just so we're clear, I want to express my continued appreciation for your work on this @euresti . If we can get this done I think attrs could basically become indispensible when dealing with typed Python. |
Has there been any movement on this? If we can get this working, we would be the basis for the first Python orm/odm with type safe projections. If not, I shall go to the typing mailing list and ask for help there. |
I did start working on a PR to move the plugin over but ran into some issues. #744 Though I don't remember why I just closed the PR. I must've been in a fugue state. |
As are we all from time to time |
Hello again. I've been playing around with learning Mypy internals over the weekend to see if this can be done. First of all, I don't really understand how the Mypy attrs plugin fits with the Mypy plugin interface described at https://mypy.readthedocs.io/en/latest/extending_mypy.html#extending-mypy-using-plugins, since it doesn't really have an entry point as described there. Maybe @euresti can provide some guidance? In any case, following the plugin docs and reading the Mypy source code, I created a plugin to return better data from Here's what it does:
Neat! Here's what we can do with it:
Et voilà! The foundations for the first Python ORM/ODM with type-safe projections! A few questions. I know the plugin as submitted is very rough, since I don't really know Mypy that well. Since it doesn't use the same interface as the existing plugin, how do we integrate them? Also, I'm thinking it would be useful to parametrize So the type of |
Having figured out how the Mypy attrs plugin works a little, I've done some work on this: python/mypy#10467 |
Sorry for not chiming in earlier. Because the attrs plugin is part of mypy it doesn't quite follow the instructions in "Extending mypy using plugins". But it looks like you figured it out. I'll take a look at your PR over in mypy tomorrow. |
Thanks David, no worries - we all have lives. I'd be eager to receive feedback whenever. |
I'm already using attrs to implement several internal ORMs, and attrs (with cattrs) is very good for this. However I find myself missing a feature.
Consider the following code:
The revealed type is
attr.Attribute[Any]
. What would it take for mypy to think the type wasattr.Attribute[int]
?This would be hugely useful for enabling type-safe projections. Often times, whether you're using a relational database or a key-value store like Mongo, you're only interested in a subset of the data. It's natural to represent your database model as a fully type-annotated attrs class, and it would be great to be able to implement something like:
And have mypy properly check it.
The text was updated successfully, but these errors were encountered: