Skip to content
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

feat: generic query builders #309

Merged
merged 9 commits into from
Sep 20, 2023

Conversation

anand2312
Copy link
Contributor

@anand2312 anand2312 commented Sep 17, 2023

Fixes #200
This PR makes all the request builders and the APIResponse classes generic;

  • The type of the data to be returned by the query can now be passed down the builder chain and into the APIResponse class
  • Pydantic then uses this type for any validation to be done
    This fixes the issue by using APIResponse[Any] as the return type for rpc calls, while using APIResponse[list[dict[str, Any]] for other queries.
    This also opens us up to (in the future) allowing users to pass in their own pydantic models to the query, so that the response is well-typed.

Marked as draft to write more tests

@codecov
Copy link

codecov bot commented Sep 17, 2023

Codecov Report

Patch coverage: 89.39% and project coverage change: -0.20% ⚠️

Comparison is base (3329234) 91.56% compared to head (bde784a) 91.36%.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #309      +/-   ##
==========================================
- Coverage   91.56%   91.36%   -0.20%     
==========================================
  Files          24       24              
  Lines        1197     1216      +19     
==========================================
+ Hits         1096     1111      +15     
- Misses        101      105       +4     
Files Changed Coverage Δ
postgrest/_async/request_builder.py 78.70% <84.84%> (-0.91%) ⬇️
postgrest/_sync/request_builder.py 85.18% <84.84%> (-1.23%) ⬇️
postgrest/_async/client.py 90.32% <87.50%> (+0.32%) ⬆️
postgrest/_sync/client.py 90.32% <87.50%> (+0.32%) ⬆️
postgrest/base_request_builder.py 77.92% <95.65%> (+0.40%) ⬆️
postgrest/utils.py 94.44% <100.00%> (+1.11%) ⬆️

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@anand2312 anand2312 marked this pull request as ready for review September 17, 2023 16:06
J0
J0 previously approved these changes Sep 17, 2023
Copy link
Contributor

@J0 J0 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM,

Thanks for putting this together

@anand2312 anand2312 changed the title Generic query builders feat: generic query builders Sep 17, 2023
@anand2312
Copy link
Contributor Author

Just realised that the current solution doesn't type-check correctly when .single or .maybe_single is used:

a = client.from_("entries").select("*").execute()
reveal_type(a.data)  # Type of a.data is List[Dict[str, Any]]

b = client.from_("entries").select("*").single().execute()
reveal_type(b.data) # Type of b.data is List[Dict[str, Any]]
# but it should be just Dict[str, Any]

There are no errors at runtime, but this will be annoying for users who use static type-checkers.

Pushing a fix now.

This makes sure the return types of rpc() and other
query methods are correct.
See https://gist.github.com/anand2312/93d3abf401335fd3310d9e30112303bf
for an explanation.
olirice
olirice previously approved these changes Sep 18, 2023
@olirice
Copy link
Contributor

olirice commented Sep 18, 2023

it would be nice if we could do this python/mypy#9319 (comment)
to clear up the 13 has no attribute "__origin__" errors from mypy to keep that list easy to skim through but went ahead and approved in case thats not feasible

This fixes the type-checker error raised while accessing
RequestBuilder[T].__origin__
@anand2312
Copy link
Contributor Author

@olirice The error for __origin__ wasn't because of the multiple inheritance, it was because __origin__ only existed at runtime (at runtime Base[T] becomes a typing.GenericAlias but for mypy Base[T] remains the same)
I've fixed the error by casting to the right type

@anand2312 anand2312 requested a review from olirice September 19, 2023 02:40
@anand2312 anand2312 merged commit ba9ad8d into supabase:master Sep 20, 2023
@anand2312 anand2312 deleted the generic-query-builders branch September 20, 2023 15:58
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.

RPC call expects data in APIResponse to be a list, which is not mandatory
3 participants