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: user models #86

Closed
wants to merge 2 commits into from
Closed

Conversation

anand2312
Copy link
Contributor

This pull request makes APIResponse, and the request builders' .execute methods generic, to allow users to pass their own pydantic BaseModels to use as response models.
Example:

from datetime import datetime
from typing import Optional, List

from postgrest_py import SyncPostgrestClient as Client
from pydantic import BaseModel

client = SyncPostgrestClient(...)

class City(BaseModel):
    id: Optional[int]
    created_at: Optional[datetime]
    country_id: Optional[int]
    city_name: Optional[str]


class Country(BaseModel):
    id: Optional[int]
    created_at: Optional[datetime]
    country_name: Optional[str]
    capital: Optional[str]
    cities: Optional[List[City]]

r = client.from_("countries").select("*,cities(*)").execute()
reveal_type(r.data)  # Type is List[Dict[str, Any]]

r = client.from_("countries").select("*,cities(*)").execute(model=Country)
reveal_type(r.data)  # Type is List[Country]

The `APIResponse`, and the request builder's `execute` methods are now
both generic (accept type arguments). This allows users to define their
own pydantic models, and make postgrest_py use those models in responses.
@sourcery-ai
Copy link
Contributor

sourcery-ai bot commented Jan 31, 2022

Sourcery Code Quality Report

❌  Merging this PR will decrease code quality in the affected files by 0.29%.

Quality metrics Before After Change
Complexity 0.63 ⭐ 0.63 ⭐ 0.00
Method Length 26.22 ⭐ 26.88 ⭐ 0.66 👎
Working memory 5.88 ⭐ 5.98 ⭐ 0.10 👎
Quality 88.61% 88.32% -0.29% 👎
Other metrics Before After Change
Lines 460 483 23
Changed files Quality Before Quality After Quality Change
postgrest_py/base_request_builder.py 88.74% ⭐ 88.81% ⭐ 0.07% 👍
postgrest_py/_async/request_builder.py 88.32% ⭐ 87.26% ⭐ -1.06% 👎
postgrest_py/_sync/request_builder.py 88.35% ⭐ 87.29% ⭐ -1.06% 👎

Here are some functions in these files that still need a tune-up:

File Function Complexity Length Working Memory Quality Recommendation

Legend and Explanation

The emojis denote the absolute quality of the code:

  • ⭐ excellent
  • 🙂 good
  • 😞 poor
  • ⛔ very poor

The 👍 and 👎 indicate whether the quality has improved or gotten worse with this pull request.


Please see our documentation here for details on how these metrics are calculated.

We are actively working on this report - lots more documentation and extra metrics to come!

Help us improve this quality report!

@anand2312 anand2312 requested review from dreinon and leynier January 31, 2022 17:57
@anand2312
Copy link
Contributor Author

@leynier @J0 @dreinon marking this PR as draft to finetune some stuff, can you look through the basic implementation in the meantime to see if there are any obvious flaws? Thanks 😄

@codecov
Copy link

codecov bot commented Jan 31, 2022

Codecov Report

Merging #86 (434e57f) into master (34fd1bd) will decrease coverage by 0.05%.
The diff coverage is 87.50%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master      #86      +/-   ##
==========================================
- Coverage   90.08%   90.02%   -0.06%     
==========================================
  Files          22       22              
  Lines         867      872       +5     
==========================================
+ Hits          781      785       +4     
- Misses         86       87       +1     
Impacted Files Coverage Δ
postgrest_py/_async/request_builder.py 89.36% <80.00%> (+0.23%) ⬆️
postgrest_py/_sync/request_builder.py 89.36% <80.00%> (+0.47%) ⬆️
postgrest_py/base_request_builder.py 75.73% <100.00%> (-0.31%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 34fd1bd...434e57f. Read the comment docs.

@leynier leynier changed the title User models feat: user models Mar 17, 2022
@anand2312
Copy link
Contributor Author

Will write an updated implementation

@anand2312 anand2312 closed this May 1, 2022
@anand2312 anand2312 mentioned this pull request Jun 7, 2023
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.

1 participant