-
Notifications
You must be signed in to change notification settings - Fork 99
Typed queries 2 (reduced) #149
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
Conversation
Accept Column in session.query() Fix type of any we use (it's not from error but implementation limitation) Fix broken test case Query uses keyed tuples, not regular tuples fix: typed queries return tuple for single-value non-entity queries fix tests broken because of new mypy version Fix typing
About the functionality provided by the plugin hook, I was thinking that maybe a simpler approach (for a future PR) would be to add several overloads to the Let's say 8 or 10 overloads, with combinations for querying from 1 to 10 entities, and setting the right return type, e.g. something like That would allow supporting a predefined number of arguments for Anyway, I would leave that for a future PR after this, to keep the scope limited. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, looks good! Could you please sign the CLA and then I can merge this.
Since at every position you can have either an entity or a column, the number of overloads grows exponentially. But maybe this can be a possible temporary hack to have a bit better types until a plugin is implemented. |
Thanks for the review @ilevkivskyi ! Yep, I signed the CLA it right before opening the PR, following the link from the README. Is there anything else I should do about the CLA?
Yeah, I just realized it 😅 Right now I'm playing locally with something like: @overload
def query(self, entity: Union[Type[_T1], Column[_T1]], **kwargs) -> Query[_T1]: ...
@overload
def query(self, entity1: Union[Type[_T1], Column[_T1]], entity2: Union[Type[_T2], Column[_T2]], **kwargs) -> Query[Tuple[_T1, _T2]]: ... But I'm not sure what other types of entities could go there, and if they are all Generics so that that trick is possible. 🤔 |
Using a union is a clever solution, but even with it there is an issue of returning a plain tuple for people who use patterns like this: for row in session.query(User.id, User.name):
do(row.id, row.name) # mypy will complain that tuple doesn't have attributes "id" and "name" So it looks like we can't avoid a plugin here. |
Thanks for merging it! 🎉
Yep, agreed. Although maybe it's possible to improve the version with just types a little bit, even if it's a bit hackish, anyway, I'll play with it a bit more as soon as I have the chance. Thanks handling this (review, merge) so quickly! 🙇 🚀 |
…151) Add one overload to `Session.query()` to handle return types for a query of one single item. This helps with the specific case of querying a single model or column. For more than one item in the `query()` it seems that another plugin hook is the only option, as the return is not a plain `Tuple` but a `KeyedTuple`. This is what I found possible continuing the discussion in #149
This is great! How can we start using this? Can we publish a new version with this change? |
idea drafting, i will delete if preferred?
…On Mon, Jun 15, 2020 at 1:03 AM delaaxe ***@***.***> wrote:
This is great! How can we start using this? Can we publish a new version with this change?
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub, or unsubscribe.
|
First, thanks for making these stubs and plugin! It works great. I'm using it with Pyright and that gives me autocompletion in VS Code for SQLAlchemy stuff, that never worked before. ✨
This PR includes a reduced version of the changes by @rafales in #81 .
I removed the plugin method hook and its tests to reduce the scope of the PR, to make it easier to manage and merge, as just the type annotations are a great improvement.
I applied @ilevkivskyi 's code review from that PR here as well.
There were some unresolved questions/discussions in that PR, so I removed any changes related to those sections.
My objective is just to reduce the scope of the changes to the minimum that would be easily acceptable and postpone any additional improvement for future PRs.