diff --git a/cuenca/__init__.py b/cuenca/__init__.py index 97bfdc5f..32bce1f4 100644 --- a/cuenca/__init__.py +++ b/cuenca/__init__.py @@ -44,8 +44,6 @@ 'get_balance', ] -from typing import cast - from . import http from .resources import ( Account, @@ -96,5 +94,5 @@ def get_balance(session: http.Session = session) -> int: - balance_entry = cast('BalanceEntry', BalanceEntry.first(session=session)) + balance_entry = BalanceEntry.first(session=session) return balance_entry.rolling_balance if balance_entry else 0 diff --git a/cuenca/resources/api_keys.py b/cuenca/resources/api_keys.py index 1675b78c..c7db6c75 100644 --- a/cuenca/resources/api_keys.py +++ b/cuenca/resources/api_keys.py @@ -1,5 +1,5 @@ import datetime as dt -from typing import ClassVar, Optional, cast +from typing import ClassVar, Optional from cuenca_validations.types import ApiKeyQuery, ApiKeyUpdateRequest @@ -36,7 +36,7 @@ def active(self) -> bool: @classmethod def create(cls, *, session: Session = global_session) -> 'ApiKey': - return cast('ApiKey', cls._create(session=session)) + return cls._create(session=session) @classmethod def deactivate( @@ -55,7 +55,7 @@ def deactivate( """ url = cls._resource + f'/{api_key_id}' resp = session.delete(url, dict(minutes=minutes)) - return cast('ApiKey', cls._from_dict(resp)) + return cls(**resp) @classmethod def update( @@ -74,5 +74,4 @@ def update( req = ApiKeyUpdateRequest( metadata=metadata, user_id=user_id, platform_id=platform_id ) - resp = cls._update(api_key_id, **req.dict(), session=session) - return cast('ApiKey', resp) + return cls._update(api_key_id, **req.dict(), session=session) diff --git a/cuenca/resources/arpc.py b/cuenca/resources/arpc.py index 78efd9c5..f045f69b 100644 --- a/cuenca/resources/arpc.py +++ b/cuenca/resources/arpc.py @@ -1,5 +1,5 @@ import datetime as dt -from typing import ClassVar, Optional, cast +from typing import ClassVar, Optional from cuenca_validations.types.requests import ARPCRequest @@ -52,4 +52,4 @@ def create( unique_number=unique_number, track_data_method=track_data_method, ) - return cast('Arpc', cls._create(session=session, **req.dict())) + return cls._create(session=session, **req.dict()) diff --git a/cuenca/resources/base.py b/cuenca/resources/base.py index 7a34dece..1d300fd2 100644 --- a/cuenca/resources/base.py +++ b/cuenca/resources/base.py @@ -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,12 +35,15 @@ 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: new = self.retrieve(self.id, session=session) for attr, value in new.__dict__.items(): setattr(self, attr, value) @@ -61,9 +51,14 @@ def refresh(self, *, session: Session = global_session): 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, + ) -> R_co: resp = session.post(cls._resource, data) - return cls._from_dict(resp) + return cls(**resp) class Updateable(Resource): @@ -72,10 +67,14 @@ 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: resp = session.patch(f'/{cls._resource}/{id}', data) - return cls._from_dict(resp) + return cls(**resp) class Deactivable(Resource): @@ -83,20 +82,24 @@ class Deactivable(Resource): @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: 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: 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,9 +151,12 @@ 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: + q = cast(Queryable, cls)._query_params(limit=2, **query_params) resp = session.get(cls._resource, q.dict()) items = resp['items'] len_items = len(items) @@ -158,40 +164,49 @@ def one( 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]: + 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, ) -> 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, + ) -> 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'] diff --git a/cuenca/resources/card_activations.py b/cuenca/resources/card_activations.py index 6523e253..7743c9b1 100644 --- a/cuenca/resources/card_activations.py +++ b/cuenca/resources/card_activations.py @@ -42,9 +42,7 @@ def create( exp_year=exp_year, cvv2=cvv2, ) - return cast( - 'CardActivation', cls._create(session=session, **req.dict()) - ) + return cls._create(session=session, **req.dict()) @property def card(self) -> Optional[Card]: diff --git a/cuenca/resources/card_validations.py b/cuenca/resources/card_validations.py index 858febd9..0bcd3770 100644 --- a/cuenca/resources/card_validations.py +++ b/cuenca/resources/card_validations.py @@ -51,9 +51,7 @@ def create( pin_block=pin_block, pin_attempts_exceeded=pin_attempts_exceeded, ) - return cast( - 'CardValidation', cls._create(session=session, **req.dict()) - ) + return cls._create(session=session, **req.dict()) @property def card(self) -> Card: diff --git a/cuenca/resources/cards.py b/cuenca/resources/cards.py index ff739615..ea9460f6 100644 --- a/cuenca/resources/cards.py +++ b/cuenca/resources/cards.py @@ -1,5 +1,5 @@ import datetime as dt -from typing import ClassVar, Optional, cast +from typing import ClassVar, Optional from cuenca_validations.types import ( CardFundingType, @@ -81,7 +81,7 @@ def create( card_holder_user_id=card_holder_user_id, is_dynamic_cvv=is_dynamic_cvv, ) - return cast('Card', cls._create(session=session, **req.dict())) + return cls._create(session=session, **req.dict()) @classmethod def update( @@ -106,8 +106,7 @@ def update( req = CardUpdateRequest( status=status, pin_block=pin_block, is_dynamic_cvv=is_dynamic_cvv ) - resp = cls._update(card_id, session=session, **req.dict()) - return cast('Card', resp) + return cls._update(card_id, session=session, **req.dict()) @classmethod def deactivate( @@ -118,4 +117,4 @@ def deactivate( """ url = f'{cls._resource}/{card_id}' resp = session.delete(url) - return cast('Card', cls._from_dict(resp)) + return cls(**resp) diff --git a/cuenca/resources/clabes.py b/cuenca/resources/clabes.py index 46dd77c3..f4ccaa53 100644 --- a/cuenca/resources/clabes.py +++ b/cuenca/resources/clabes.py @@ -1,4 +1,4 @@ -from typing import ClassVar, cast +from typing import ClassVar from ..http import Session, session as global_session from .base import Creatable, Queryable, Retrievable @@ -11,4 +11,4 @@ class Clabe(Creatable, Queryable, Retrievable): @classmethod def create(cls, session: Session = global_session): - return cast('Clabe', cls._create(session=session)) + return cls._create(session=session) diff --git a/cuenca/resources/curp_validations.py b/cuenca/resources/curp_validations.py index 5c466503..144ea332 100644 --- a/cuenca/resources/curp_validations.py +++ b/cuenca/resources/curp_validations.py @@ -1,5 +1,5 @@ import datetime as dt -from typing import ClassVar, Optional, cast +from typing import ClassVar, Optional from cuenca_validations.types import ( Country, @@ -98,7 +98,4 @@ def create( gender=gender, manual_curp=manual_curp, ) - return cast( - 'CurpValidation', - cls._create(session=session, **req.dict()), - ) + return cls._create(session=session, **req.dict()) diff --git a/cuenca/resources/endpoints.py b/cuenca/resources/endpoints.py index 43fb79c9..79aa4750 100644 --- a/cuenca/resources/endpoints.py +++ b/cuenca/resources/endpoints.py @@ -1,4 +1,4 @@ -from typing import ClassVar, List, Optional, cast +from typing import ClassVar, List, Optional from cuenca_validations.types.enums import WebhookEvent from cuenca_validations.types.requests import ( @@ -72,7 +72,7 @@ def create( :return: New active endpoint """ req = EndpointRequest(url=url, events=events) - return cast('Endpoint', cls._create(session=session, **req.dict())) + return cls._create(session=session, **req.dict()) @classmethod def update( @@ -89,6 +89,7 @@ def update( like url and is_active. :param endpoint_id: existing endpoint_id :param url + :param events: Optional list of enabled events to update :param is_enable :param session :return: Updated endpoint object @@ -96,5 +97,4 @@ def update( req = EndpointUpdateRequest( url=url, is_enable=is_enable, events=events ) - resp = cls._update(endpoint_id, session=session, **req.dict()) - return cast('Endpoint', resp) + return cls._update(endpoint_id, session=session, **req.dict()) diff --git a/cuenca/resources/file_batches.py b/cuenca/resources/file_batches.py index 68123898..137ca6a2 100644 --- a/cuenca/resources/file_batches.py +++ b/cuenca/resources/file_batches.py @@ -1,4 +1,4 @@ -from typing import ClassVar, Dict, List, cast +from typing import ClassVar, Dict, List from cuenca_validations.types import BatchFileMetadata, FileBatchUploadRequest @@ -22,4 +22,4 @@ def create( session: Session = global_session, ) -> 'FileBatch': req = FileBatchUploadRequest(files=files, user_id=user_id) - return cast('FileBatch', cls._create(session=session, **req.dict())) + return cls._create(session=session, **req.dict()) diff --git a/cuenca/resources/files.py b/cuenca/resources/files.py index b56bb1a5..30fcecf9 100644 --- a/cuenca/resources/files.py +++ b/cuenca/resources/files.py @@ -1,5 +1,5 @@ from io import BytesIO -from typing import ClassVar, Optional, cast +from typing import ClassVar, Optional from cuenca_validations.types import FileQuery, FileUploadRequest, KYCFileType from pydantic import HttpUrl @@ -44,13 +44,7 @@ def upload( is_back=is_back, user_id=user_id, ) - return cast( - 'File', - cls._upload( - session=session, - **req.dict(), - ), - ) + return cls._upload(session=session, **req.dict()) @property def file(self) -> bytes: diff --git a/cuenca/resources/kyc_validations.py b/cuenca/resources/kyc_validations.py index 1ab0bc47..bb5fa6f1 100644 --- a/cuenca/resources/kyc_validations.py +++ b/cuenca/resources/kyc_validations.py @@ -1,4 +1,4 @@ -from typing import ClassVar, List, Optional, cast +from typing import ClassVar, List, Optional from cuenca_validations.types import KYCFile, KYCValidationRequest @@ -38,6 +38,4 @@ def create( force=force, documents=documents, ) - return cast( - 'KYCValidation', cls._create(**req.dict(), session=session) - ) + return cls._create(**req.dict(), session=session) diff --git a/cuenca/resources/kyc_verifications.py b/cuenca/resources/kyc_verifications.py index 106b77b3..d95d21ad 100644 --- a/cuenca/resources/kyc_verifications.py +++ b/cuenca/resources/kyc_verifications.py @@ -1,5 +1,5 @@ import datetime as dt -from typing import ClassVar, Optional, cast +from typing import ClassVar, Optional from cuenca_validations.types import ( Address, @@ -39,7 +39,7 @@ class Config: @classmethod def create(cls, session: Session = global_session) -> 'KYCVerification': - return cast('KYCVerification', cls._create(session=session)) + return cls._create(session=session) @classmethod def update( @@ -48,4 +48,4 @@ def update( curp: Optional[CurpField] = None, ) -> 'KYCVerification': req = KYCVerificationUpdateRequest(curp=curp) - return cast('KYCVerification', cls._update(id=kyc_id, **req.dict())) + return cls._update(id=kyc_id, **req.dict()) diff --git a/cuenca/resources/limited_wallets.py b/cuenca/resources/limited_wallets.py index 8ea03865..e9b91255 100644 --- a/cuenca/resources/limited_wallets.py +++ b/cuenca/resources/limited_wallets.py @@ -1,4 +1,4 @@ -from typing import ClassVar, Optional, cast +from typing import ClassVar, Optional from clabe import Clabe from cuenca_validations.types import ( @@ -37,4 +37,4 @@ def create( allowed_curp=allowed_curp, allowed_rfc=allowed_rfc, ) - return cast('LimitedWallet', cls._create(**request.dict())) + return cls._create(**request.dict()) diff --git a/cuenca/resources/login_tokens.py b/cuenca/resources/login_tokens.py index 7bb3828b..d5f36c4b 100644 --- a/cuenca/resources/login_tokens.py +++ b/cuenca/resources/login_tokens.py @@ -1,4 +1,4 @@ -from typing import ClassVar, cast +from typing import ClassVar from ..http import Session, session as global_session from .base import Creatable @@ -17,4 +17,4 @@ def create(cls, session: Session = global_session) -> 'LoginToken': Make sure to store this token in a safe place :return: Token that you can use in cuenca.configure """ - return cast('LoginToken', cls._create(session=session)) + return cls._create(session=session) diff --git a/cuenca/resources/otps.py b/cuenca/resources/otps.py index 03450c1b..b3534131 100644 --- a/cuenca/resources/otps.py +++ b/cuenca/resources/otps.py @@ -1,4 +1,4 @@ -from typing import ClassVar, cast +from typing import ClassVar from ..http import Session, session as global_session from .base import Creatable @@ -21,4 +21,4 @@ def create(cls, session: Session = global_session) -> 'Otp': """ Use this method to create a OTP seed """ - return cast('Otp', cls._create(session=session)) + return cls._create(session=session) diff --git a/cuenca/resources/platforms.py b/cuenca/resources/platforms.py index e867fcdb..ad9320f9 100644 --- a/cuenca/resources/platforms.py +++ b/cuenca/resources/platforms.py @@ -1,5 +1,5 @@ import datetime as dt -from typing import ClassVar, Optional, cast +from typing import ClassVar, Optional from cuenca_validations.types import Country, PlatformRequest, State @@ -76,4 +76,4 @@ def create( phone_number=phone_number, email_address=email_address, ) - return cast('Platform', cls._create(session=session, **req.dict())) + return cls._create(session=session, **req.dict()) diff --git a/cuenca/resources/questionnaires.py b/cuenca/resources/questionnaires.py index 5e19c9b6..0d23beac 100644 --- a/cuenca/resources/questionnaires.py +++ b/cuenca/resources/questionnaires.py @@ -1,5 +1,5 @@ import datetime as dt -from typing import ClassVar, cast +from typing import ClassVar from cuenca_validations.types import QuestionnairesRequest @@ -38,6 +38,4 @@ def create( token=token, form_id=form_id, ) - return cast( - 'Questionnaires', cls._create(session=session, **req.dict()) - ) + return cls._create(session=session, **req.dict()) diff --git a/cuenca/resources/resources.py b/cuenca/resources/resources.py index eeac433b..2633e04a 100644 --- a/cuenca/resources/resources.py +++ b/cuenca/resources/resources.py @@ -1,6 +1,6 @@ import re from concurrent.futures import ThreadPoolExecutor -from typing import Dict, List, cast +from typing import Dict, List from .base import Retrievable @@ -13,7 +13,7 @@ def retrieve_uri(uri: str) -> Retrievable: if not m: raise ValueError(f'uri is not a valid format: {uri}') resource, id_ = m.groups() - return cast(Retrievable, RESOURCES[resource].retrieve(id_)) + return RESOURCES[resource].retrieve(id_) def retrieve_uris(uris: List[str]) -> List[Retrievable]: diff --git a/cuenca/resources/savings.py b/cuenca/resources/savings.py index 2a857097..d5722ea9 100644 --- a/cuenca/resources/savings.py +++ b/cuenca/resources/savings.py @@ -1,5 +1,5 @@ import datetime as dt -from typing import ClassVar, Optional, cast +from typing import ClassVar, Optional from cuenca_validations.types import ( SavingCategory, @@ -27,14 +27,14 @@ def create( category: SavingCategory, goal_amount: Optional[int] = None, goal_date: Optional[dt.datetime] = None, - ): + ) -> 'Saving': request = SavingRequest( name=name, category=category, goal_amount=goal_amount, goal_date=goal_date, ) - return cast('Saving', cls._create(**request.dict())) + return cls._create(**request.dict()) @classmethod def update( @@ -51,4 +51,4 @@ def update( goal_amount=goal_amount, goal_date=goal_date, ) - return cast('Saving', cls._update(id=saving_id, **request.dict())) + return cls._update(id=saving_id, **request.dict()) diff --git a/cuenca/resources/sessions.py b/cuenca/resources/sessions.py index 227e3b15..58adaff6 100644 --- a/cuenca/resources/sessions.py +++ b/cuenca/resources/sessions.py @@ -1,5 +1,5 @@ import datetime as dt -from typing import ClassVar, Optional, cast +from typing import ClassVar, Optional from cuenca_validations.types import SessionRequest, SessionType from pydantic import AnyUrl @@ -50,4 +50,4 @@ def create( success_url=success_url, failure_url=failure_url, ) - return cast('Session', cls._create(session=session, **req.dict())) + return cls._create(session=session, **req.dict()) diff --git a/cuenca/resources/transfers.py b/cuenca/resources/transfers.py index 97b0cb9e..6cdf5fde 100644 --- a/cuenca/resources/transfers.py +++ b/cuenca/resources/transfers.py @@ -69,7 +69,7 @@ def create( idempotency_key=idempotency_key, user_id=user_id, ) - return cast('Transfer', cls._create(**req.dict())) + return cls._create(**req.dict()) @classmethod def create_many(cls, requests: List[TransferRequest]) -> DictStrAny: @@ -80,7 +80,7 @@ def create_many(cls, requests: List[TransferRequest]) -> DictStrAny: except (CuencaException, HTTPError) as e: transfers['errors'].append(dict(request=req, error=e)) else: - transfers['submitted'].append(cast('Transfer', transfer)) + transfers['submitted'].append(transfer) return transfers @staticmethod diff --git a/cuenca/resources/user_credentials.py b/cuenca/resources/user_credentials.py index 097dd6ff..b5a05aec 100644 --- a/cuenca/resources/user_credentials.py +++ b/cuenca/resources/user_credentials.py @@ -1,5 +1,5 @@ import datetime as dt -from typing import ClassVar, Optional, cast +from typing import ClassVar, Optional from cuenca_validations.types.requests import ( UserCredentialRequest, @@ -25,9 +25,7 @@ def create( session: Session = global_session, ) -> 'UserCredential': req = UserCredentialRequest(password=password, user_id=user_id) - return cast( - 'UserCredential', cls._create(**req.dict(), session=session) - ) + return cls._create(**req.dict(), session=session) @classmethod def update( @@ -42,7 +40,4 @@ def update( is_active=is_active, password=password, ) - return cast( - 'UserCredential', - cls._update(id=user_id, **req.dict(), session=session), - ) + return cls._update(id=user_id, **req.dict(), session=session) diff --git a/cuenca/resources/user_lists_validation.py b/cuenca/resources/user_lists_validation.py index 8578071b..c248588f 100644 --- a/cuenca/resources/user_lists_validation.py +++ b/cuenca/resources/user_lists_validation.py @@ -1,5 +1,5 @@ import datetime as dt -from typing import ClassVar, Optional, cast +from typing import ClassVar, Optional from cuenca_validations.types import UserListsRequest, VerificationStatus from cuenca_validations.types.identities import CurpField @@ -36,7 +36,4 @@ def create( curp=curp, account_number=account_number, ) - return cast( - 'UserListsValidation', - cls._create(session=session, **req.dict()), - ) + return cls._create(session=session, **req.dict()) diff --git a/cuenca/resources/user_logins.py b/cuenca/resources/user_logins.py index 703f1d81..33b0f2eb 100644 --- a/cuenca/resources/user_logins.py +++ b/cuenca/resources/user_logins.py @@ -1,5 +1,5 @@ import datetime as dt -from typing import ClassVar, Optional, cast +from typing import ClassVar, Optional from cuenca_validations.types.requests import UserLoginRequest @@ -31,7 +31,7 @@ def create( session: Session = global_session, ) -> 'UserLogin': req = UserLoginRequest(password=password, user_id=user_id) - login = cast('UserLogin', cls._create(session=session, **req.dict())) + login = cls._create(session=session, **req.dict()) if login.success: session.headers['X-Cuenca-LoginId'] = login.id return login diff --git a/cuenca/resources/users.py b/cuenca/resources/users.py index 0a0d5816..ffed5c3a 100644 --- a/cuenca/resources/users.py +++ b/cuenca/resources/users.py @@ -59,7 +59,7 @@ class User(Creatable, Retrievable, Updateable, Queryable): @property def balance(self) -> int: - be = cast(BalanceEntry, BalanceEntry.first(user_id=self.id)) + be = BalanceEntry.first(user_id=self.id) return be.rolling_balance if be else 0 class Config: @@ -135,7 +135,7 @@ def create( status=status, terms_of_service=terms_of_service, ) - return cast('User', cls._create(session=session, **req.dict())) + return cls._create(session=session, **req.dict()) @classmethod def update( @@ -157,7 +157,7 @@ def update( curp_document: Optional[HttpUrl] = None, *, session: Session = global_session, - ): + ) -> 'User': request = UserUpdateRequest( phone_number=phone_number, email_address=email_address, @@ -174,10 +174,7 @@ def update( curp_document=curp_document, status=status, ) - return cast( - 'User', - cls._update(id=user_id, **request.dict(), session=session), - ) + return cls._update(id=user_id, **request.dict(), session=session) @property def identity(self) -> Identity: diff --git a/cuenca/resources/verifications.py b/cuenca/resources/verifications.py index bf59b1f7..a70fc487 100644 --- a/cuenca/resources/verifications.py +++ b/cuenca/resources/verifications.py @@ -1,5 +1,5 @@ import datetime as dt -from typing import ClassVar, Optional, Union, cast +from typing import ClassVar, Optional, Union from cuenca_validations.types import ( VerificationAttemptRequest, @@ -44,7 +44,7 @@ def create( req = VerificationRequest( recipient=recipient, type=type, platform_id=platform_id ) - return cast('Verification', cls._create(**req.dict(), session=session)) + return cls._create(**req.dict(), session=session) @classmethod def verify( @@ -54,7 +54,4 @@ def verify( session: Session = global_session, ) -> 'Verification': req = VerificationAttemptRequest(code=code) - return cast( - 'Verification', - cls._update(id=id, **req.dict(), session=session), - ) + return cls._update(id=id, **req.dict(), session=session) diff --git a/cuenca/resources/wallet_transactions.py b/cuenca/resources/wallet_transactions.py index c745d007..5204383d 100644 --- a/cuenca/resources/wallet_transactions.py +++ b/cuenca/resources/wallet_transactions.py @@ -29,10 +29,10 @@ def create( wallet_uri: str, transaction_type: WalletTransactionType, amount: int, - ): + ) -> 'WalletTransaction': request = WalletTransactionRequest( wallet_uri=wallet_uri, transaction_type=transaction_type, amount=amount, ) - return cast('WalletTransaction', cls._create(**request.dict())) + return cls._create(**request.dict()) diff --git a/cuenca/version.py b/cuenca/version.py index 2d56c88c..e2e3271b 100644 --- a/cuenca/version.py +++ b/cuenca/version.py @@ -1,3 +1,3 @@ -__version__ = '1.0.2' +__version__ = '1.0.3' CLIENT_VERSION = __version__ API_VERSION = '2020-03-19' diff --git a/tests/resources/test_api_keys.py b/tests/resources/test_api_keys.py index 42aa9b98..0aabd77f 100644 --- a/tests/resources/test_api_keys.py +++ b/tests/resources/test_api_keys.py @@ -84,7 +84,7 @@ def test_api_key_from_dict(): metadata=None, user_id=None, ) - api_key = ApiKey._from_dict(api_keys_dict) + api_key = ApiKey(**api_keys_dict) assert not hasattr(api_key, 'extra_field_1') assert not hasattr(api_key, 'extra_field_2') assert api_key.id is not None