-
Notifications
You must be signed in to change notification settings - Fork 14
proper type annotation #409
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
1f4d472
remove cast if not neccesary
felipao-mx 3d38490
remove _from_dict because it is not neccesary, using pydantic methods…
felipao-mx af74604
Q_co
felipao-mx 39cefb3
extra ignore
felipao-mx 8576c80
version
felipao-mx a688963
using cast
felipao-mx 0eb3577
version
felipao-mx 1590a9e
version
felipao-mx c96049f
coderabbit comments
felipao-mx File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,7 +2,7 @@ | |
| import datetime as dt | ||
| import json | ||
| from io import BytesIO | ||
| from typing import ClassVar, Dict, Generator, Optional, Union | ||
| from typing import Any, ClassVar, Generator, Optional, Type, TypeVar, cast | ||
| from urllib.parse import urlencode | ||
|
|
||
| from cuenca_validations.types import ( | ||
|
|
@@ -12,34 +12,21 @@ | |
| TransactionQuery, | ||
| TransactionStatus, | ||
| ) | ||
| from pydantic import BaseModel | ||
| from pydantic import BaseModel, Extra | ||
|
|
||
| from ..exc import MultipleResultsFound, NoResultFound | ||
| from ..http import Session, session as global_session | ||
|
|
||
| R_co = TypeVar('R_co', bound='Resource', covariant=True) | ||
|
|
||
|
|
||
| class Resource(BaseModel): | ||
| _resource: ClassVar[str] | ||
|
|
||
| id: str | ||
|
|
||
| @classmethod | ||
| def _from_dict(cls, obj_dict: Dict[str, Union[str, int]]) -> 'Resource': | ||
| cls._filter_excess_fields(obj_dict) | ||
| return cls(**obj_dict) | ||
|
|
||
| @classmethod | ||
| def _filter_excess_fields(cls, obj_dict): | ||
| """ | ||
| dataclasses don't allow __init__ to be called with excess fields. This | ||
| method allows the API to add fields in the response body without | ||
| breaking the client | ||
| """ | ||
| excess = set(obj_dict.keys()) - set( | ||
| cls.schema().get("properties").keys() | ||
| ) | ||
| for f in excess: | ||
| del obj_dict[f] | ||
| class Config: | ||
| extra = Extra.ignore | ||
|
|
||
| def to_dict(self): | ||
| return SantizedDict(self.dict()) | ||
|
|
@@ -48,22 +35,30 @@ def to_dict(self): | |
| class Retrievable(Resource): | ||
| @classmethod | ||
| def retrieve( | ||
| cls, id: str, *, session: Session = global_session | ||
| ) -> Resource: | ||
| cls: Type[R_co], | ||
| id: str, | ||
| *, | ||
| session: Session = global_session, | ||
| ) -> R_co: | ||
| resp = session.get(f'/{cls._resource}/{id}') | ||
| return cls._from_dict(resp) | ||
| return cls(**resp) | ||
|
|
||
| def refresh(self, *, session: Session = global_session): | ||
| def refresh(self, *, session: Session = global_session) -> None: | ||
felipao-mx marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| new = self.retrieve(self.id, session=session) | ||
| for attr, value in new.__dict__.items(): | ||
| setattr(self, attr, value) | ||
|
|
||
|
|
||
| class Creatable(Resource): | ||
| @classmethod | ||
| def _create(cls, *, session: Session = global_session, **data) -> Resource: | ||
| def _create( | ||
| cls: Type[R_co], | ||
| *, | ||
| session: Session = global_session, | ||
| **data: Any, | ||
felipao-mx marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ) -> R_co: | ||
| resp = session.post(cls._resource, data) | ||
| return cls._from_dict(resp) | ||
| return cls(**resp) | ||
|
|
||
|
|
||
| class Updateable(Resource): | ||
|
|
@@ -72,31 +67,39 @@ class Updateable(Resource): | |
|
|
||
| @classmethod | ||
| def _update( | ||
| cls, id: str, *, session: Session = global_session, **data | ||
| ) -> Resource: | ||
| cls: Type[R_co], | ||
| id: str, | ||
| *, | ||
| session: Session = global_session, | ||
| **data: Any, | ||
| ) -> R_co: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Specify a more precise type for
|
||
| resp = session.patch(f'/{cls._resource}/{id}', data) | ||
| return cls._from_dict(resp) | ||
| return cls(**resp) | ||
|
|
||
|
|
||
| class Deactivable(Resource): | ||
| deactivated_at: Optional[dt.datetime] | ||
|
|
||
| @classmethod | ||
| def deactivate( | ||
| cls, id: str, *, session: Session = global_session, **data | ||
| ) -> Resource: | ||
| cls: Type[R_co], | ||
| id: str, | ||
| *, | ||
| session: Session = global_session, | ||
| **data: Any, | ||
| ) -> R_co: | ||
felipao-mx marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| resp = session.delete(f'/{cls._resource}/{id}', data) | ||
| return cls._from_dict(resp) | ||
| return cls(**resp) | ||
|
|
||
| @property | ||
| def is_active(self): | ||
| def is_active(self) -> bool: | ||
felipao-mx marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return not self.deactivated_at | ||
|
|
||
|
|
||
| class Downloadable(Resource): | ||
| @classmethod | ||
| def download( | ||
| cls, | ||
| cls: Type[R_co], | ||
| id: str, | ||
| file_format: FileFormat = FileFormat.any, | ||
| *, | ||
|
|
@@ -121,13 +124,13 @@ def xml(self) -> bytes: | |
| class Uploadable(Resource): | ||
| @classmethod | ||
| def _upload( | ||
| cls, | ||
| cls: Type[R_co], | ||
| file: bytes, | ||
| user_id: str, | ||
| *, | ||
| session: Session = global_session, | ||
| **data, | ||
| ) -> Resource: | ||
| **data: Any, | ||
| ) -> R_co: | ||
| encoded_file = base64.b64encode(file) | ||
| resp = session.request( | ||
| 'post', | ||
|
|
@@ -138,7 +141,7 @@ def _upload( | |
| **{k: (None, v) for k, v in data.items()}, | ||
| ), | ||
| ) | ||
| return cls._from_dict(json.loads(resp)) | ||
| return cls(**json.loads(resp)) | ||
|
|
||
|
|
||
| class Queryable(Resource): | ||
|
|
@@ -148,50 +151,62 @@ class Queryable(Resource): | |
|
|
||
| @classmethod | ||
| def one( | ||
| cls, *, session: Session = global_session, **query_params | ||
| ) -> Resource: | ||
| q = cls._query_params(limit=2, **query_params) | ||
| cls: Type[R_co], | ||
| *, | ||
| session: Session = global_session, | ||
| **query_params: Any, | ||
| ) -> R_co: | ||
felipao-mx marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| q = cast(Queryable, cls)._query_params(limit=2, **query_params) | ||
| resp = session.get(cls._resource, q.dict()) | ||
| items = resp['items'] | ||
| len_items = len(items) | ||
| if not len_items: | ||
| raise NoResultFound | ||
| if len_items > 1: | ||
| raise MultipleResultsFound | ||
| return cls._from_dict(items[0]) | ||
| return cls(**items[0]) | ||
|
|
||
| @classmethod | ||
| def first( | ||
| cls, *, session: Session = global_session, **query_params | ||
| ) -> Optional[Resource]: | ||
| q = cls._query_params(limit=1, **query_params) | ||
| cls: Type[R_co], | ||
| *, | ||
| session: Session = global_session, | ||
| **query_params: Any, | ||
| ) -> Optional[R_co]: | ||
felipao-mx marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| q = cast(Queryable, cls)._query_params(limit=1, **query_params) | ||
| resp = session.get(cls._resource, q.dict()) | ||
| try: | ||
| item = resp['items'][0] | ||
| except IndexError: | ||
| rv = None | ||
| else: | ||
| rv = cls._from_dict(item) | ||
| rv = cls(**item) | ||
| return rv | ||
|
|
||
| @classmethod | ||
| def count( | ||
| cls, *, session: Session = global_session, **query_params | ||
| cls: Type[R_co], | ||
| *, | ||
| session: Session = global_session, | ||
| **query_params: Any, | ||
felipao-mx marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ) -> int: | ||
| q = cls._query_params(count=True, **query_params) | ||
| q = cast(Queryable, cls)._query_params(count=True, **query_params) | ||
| resp = session.get(cls._resource, q.dict()) | ||
| return resp['count'] | ||
|
|
||
| @classmethod | ||
| def all( | ||
| cls, *, session: Session = global_session, **query_params | ||
| ) -> Generator[Resource, None, None]: | ||
| cls: Type[R_co], | ||
| *, | ||
| session: Session = global_session, | ||
| **query_params: Any, | ||
felipao-mx marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ) -> Generator[R_co, None, None]: | ||
| session = session or global_session | ||
| q = cls._query_params(**query_params) | ||
| q = cast(Queryable, cls)._query_params(**query_params) | ||
| next_page_uri = f'{cls._resource}?{urlencode(q.dict())}' | ||
| while next_page_uri: | ||
| page = session.get(next_page_uri) | ||
| yield from (cls._from_dict(item) for item in page['items']) | ||
| yield from (cls(**item) for item in page['items']) | ||
| next_page_uri = page['next_page_uri'] | ||
|
|
||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.