From 12a855684083ac50c8a7499826163acf9695799d Mon Sep 17 00:00:00 2001 From: gabino Date: Sun, 22 Dec 2024 10:24:54 -0600 Subject: [PATCH] Refactor type hints and casting in resource files --- cuenca/resources/arpc.py | 3 ++- cuenca/resources/balance_entries.py | 6 ++---- cuenca/resources/base.py | 4 ++-- cuenca/resources/card_validations.py | 2 +- cuenca/resources/curp_validations.py | 2 +- cuenca/resources/file_batches.py | 10 ++++++++-- cuenca/resources/files.py | 9 +++++++-- cuenca/resources/kyc_verifications.py | 2 +- cuenca/resources/limited_wallets.py | 2 +- cuenca/resources/platforms.py | 2 +- cuenca/resources/sessions.py | 4 ++-- cuenca/resources/transfers.py | 3 ++- cuenca/resources/user_lists_validation.py | 3 ++- cuenca/resources/users.py | 6 ++++-- 14 files changed, 36 insertions(+), 22 deletions(-) diff --git a/cuenca/resources/arpc.py b/cuenca/resources/arpc.py index 773d6a99..77e663a8 100644 --- a/cuenca/resources/arpc.py +++ b/cuenca/resources/arpc.py @@ -1,6 +1,7 @@ import datetime as dt from typing import ClassVar, Optional, cast +from cuenca_validations.types.enums import TrackDataMethod from cuenca_validations.types.requests import ARPCRequest from ..http import Session, session as global_session @@ -50,6 +51,6 @@ def create( transaction_counter=transaction_counter, pan_sequence=pan_sequence, unique_number=unique_number, - track_data_method=track_data_method, + track_data_method=cast(TrackDataMethod, track_data_method), ) return cast('Arpc', cls._create(session=session, **req.model_dump())) diff --git a/cuenca/resources/balance_entries.py b/cuenca/resources/balance_entries.py index 9104f38d..e160f6bb 100644 --- a/cuenca/resources/balance_entries.py +++ b/cuenca/resources/balance_entries.py @@ -1,4 +1,4 @@ -from typing import ClassVar, TypeVar, cast +from typing import ClassVar, Union, cast from cuenca_validations.types import BalanceEntryQuery, EntryType @@ -8,9 +8,7 @@ from .resources import retrieve_uri from .service_providers import ServiceProvider -FundingInstrument = TypeVar( - 'FundingInstrument', Account, ServiceProvider, Card -) +FundingInstrument = Union[Account, ServiceProvider, Card] class BalanceEntry(Retrievable, Queryable): diff --git a/cuenca/resources/base.py b/cuenca/resources/base.py index d8745a31..c8da1294 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, Dict, Generator, Optional from urllib.parse import urlencode from cuenca_validations.types import ( @@ -24,7 +24,7 @@ class Resource(BaseModel): id: str @classmethod - def _from_dict(cls, obj_dict: Dict[str, Union[str, int]]) -> 'Resource': + def _from_dict(cls, obj_dict: Dict[str, Any]) -> 'Resource': cls._filter_excess_fields(obj_dict) return cls(**obj_dict) diff --git a/cuenca/resources/card_validations.py b/cuenca/resources/card_validations.py index 5e03b895..59870a48 100644 --- a/cuenca/resources/card_validations.py +++ b/cuenca/resources/card_validations.py @@ -37,7 +37,7 @@ def create( exp_month: Optional[int] = None, exp_year: Optional[int] = None, pin_block: Optional[str] = None, - pin_attempts_exceeded: Optional[str] = None, + pin_attempts_exceeded: Optional[bool] = None, *, session: Session = global_session, ) -> 'CardValidation': diff --git a/cuenca/resources/curp_validations.py b/cuenca/resources/curp_validations.py index 49cb20f6..85643d5d 100644 --- a/cuenca/resources/curp_validations.py +++ b/cuenca/resources/curp_validations.py @@ -95,7 +95,7 @@ def create( second_surname=second_surname, date_of_birth=date_of_birth, state_of_birth=state_of_birth, - country_of_birth=country_of_birth, + country_of_birth=cast(Country, country_of_birth), gender=gender, manual_curp=manual_curp, ) diff --git a/cuenca/resources/file_batches.py b/cuenca/resources/file_batches.py index 201c7648..e658e435 100644 --- a/cuenca/resources/file_batches.py +++ b/cuenca/resources/file_batches.py @@ -1,6 +1,10 @@ from typing import ClassVar, Dict, List, cast -from cuenca_validations.types import BatchFileMetadata, FileBatchUploadRequest +from cuenca_validations.types import ( + BatchFileMetadata, + FileBatchUploadRequest, + FileRequest, +) from ..http import Session, session as global_session from .base import Creatable, Queryable @@ -21,7 +25,9 @@ def create( *, session: Session = global_session, ) -> 'FileBatch': - req = FileBatchUploadRequest(files=files, user_id=user_id) + req = FileBatchUploadRequest( + files=cast(List[FileRequest], files), user_id=user_id + ) return cast( 'FileBatch', cls._create(session=session, **req.model_dump()) ) diff --git a/cuenca/resources/files.py b/cuenca/resources/files.py index b56bb1a5..a60df8f2 100644 --- a/cuenca/resources/files.py +++ b/cuenca/resources/files.py @@ -1,7 +1,12 @@ from io import BytesIO from typing import ClassVar, Optional, cast -from cuenca_validations.types import FileQuery, FileUploadRequest, KYCFileType +from cuenca_validations.types import ( + FileExtension, + FileQuery, + FileUploadRequest, + KYCFileType, +) from pydantic import HttpUrl from ..http import Session, session as global_session @@ -40,7 +45,7 @@ def upload( req = FileUploadRequest( file=file.read(), type=file_type, - extension=extension, + extension=cast(FileExtension, extension), is_back=is_back, user_id=user_id, ) diff --git a/cuenca/resources/kyc_verifications.py b/cuenca/resources/kyc_verifications.py index b4a378cf..b579b7ef 100644 --- a/cuenca/resources/kyc_verifications.py +++ b/cuenca/resources/kyc_verifications.py @@ -46,7 +46,7 @@ def create(cls, session: Session = global_session) -> 'KYCVerification': def update( cls, kyc_id: str, - curp: Optional[CurpField] = None, + curp: CurpField, ) -> 'KYCVerification': req = KYCVerificationUpdateRequest(curp=curp) return cast('KYCVerification', cls._update(id=kyc_id, **req.dict())) diff --git a/cuenca/resources/limited_wallets.py b/cuenca/resources/limited_wallets.py index dbeddb17..48dc015c 100644 --- a/cuenca/resources/limited_wallets.py +++ b/cuenca/resources/limited_wallets.py @@ -21,7 +21,7 @@ class LimitedWallet(Wallet): @classmethod def create( cls, - allowed_curp: Optional[CurpField] = None, + allowed_curp: CurpField, allowed_rfc: Optional[Rfc] = None, ) -> 'LimitedWallet': """ diff --git a/cuenca/resources/platforms.py b/cuenca/resources/platforms.py index 641e5fc2..f7055153 100644 --- a/cuenca/resources/platforms.py +++ b/cuenca/resources/platforms.py @@ -54,7 +54,7 @@ def create( cls, name: str, rfc: Optional[str] = None, - establishment_date: Optional[str] = None, + establishment_date: Optional[dt.date] = None, country: Optional[Country] = None, state: Optional[State] = None, economic_activity: Optional[str] = None, diff --git a/cuenca/resources/sessions.py b/cuenca/resources/sessions.py index 7639698c..94eef9af 100644 --- a/cuenca/resources/sessions.py +++ b/cuenca/resources/sessions.py @@ -39,8 +39,8 @@ def create( cls, user_id: str, type: SessionType, - success_url: Optional[str] = None, - failure_url: Optional[str] = None, + success_url=cast(Optional[AnyUrl], success_url), + failure_url=cast(Optional[AnyUrl], failure_url), *, session: http.Session = http.session, ) -> 'Session': diff --git a/cuenca/resources/transfers.py b/cuenca/resources/transfers.py index 3e407c1d..2ec50188 100644 --- a/cuenca/resources/transfers.py +++ b/cuenca/resources/transfers.py @@ -1,6 +1,7 @@ import datetime as dt from typing import ClassVar, List, Optional, cast +from clabe import Clabe from cuenca_validations.types import ( TransferNetwork, TransferQuery, @@ -62,7 +63,7 @@ def create( if not idempotency_key: idempotency_key = cls._gen_idempotency_key(account_number, amount) req = TransferRequest( - account_number=account_number, + account_number=cast(Clabe, account_number), amount=amount, descriptor=descriptor, recipient_name=recipient_name, diff --git a/cuenca/resources/user_lists_validation.py b/cuenca/resources/user_lists_validation.py index f475e7d3..072e3420 100644 --- a/cuenca/resources/user_lists_validation.py +++ b/cuenca/resources/user_lists_validation.py @@ -1,6 +1,7 @@ import datetime as dt from typing import ClassVar, Optional, cast +from clabe import Clabe from cuenca_validations.types import UserListsRequest, VerificationStatus from cuenca_validations.types.identities import CurpField @@ -34,7 +35,7 @@ def create( first_surname=first_surname, second_surname=second_surname, curp=curp, - account_number=account_number, + account_number=cast(Clabe, account_number), ) return cast( 'UserListsValidation', diff --git a/cuenca/resources/users.py b/cuenca/resources/users.py index b616ac10..abdcef22 100644 --- a/cuenca/resources/users.py +++ b/cuenca/resources/users.py @@ -104,6 +104,7 @@ def balance(self) -> int: def create( cls, curp: CurpField, + id: Optional[str] = None, phone_number: Optional[PhoneNumber] = None, email_address: Optional[EmailStr] = None, profession: Optional[str] = None, @@ -117,6 +118,7 @@ def create( session: Session = global_session, ) -> 'User': req = UserRequest( + id=id, curp=curp, phone_number=phone_number, email_address=email_address, @@ -147,7 +149,7 @@ def update( status: Optional[UserStatus] = None, email_verification_id: Optional[str] = None, phone_verification_id: Optional[str] = None, - curp_document: Optional[HttpUrl] = None, + curp_document_uri: Optional[HttpUrl] = None, *, session: Session = global_session, ): @@ -164,7 +166,7 @@ def update( verification_id=verification_id, email_verification_id=email_verification_id, phone_verification_id=phone_verification_id, - curp_document=curp_document, + curp_document_uri=curp_document_uri, status=status, ) return cast(