Skip to content

Commit

Permalink
Refactor type hints and casting in resource files
Browse files Browse the repository at this point in the history
  • Loading branch information
gabino committed Dec 22, 2024
1 parent 6abe75c commit 12a8556
Show file tree
Hide file tree
Showing 14 changed files with 36 additions and 22 deletions.
3 changes: 2 additions & 1 deletion cuenca/resources/arpc.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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()))
6 changes: 2 additions & 4 deletions cuenca/resources/balance_entries.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import ClassVar, TypeVar, cast
from typing import ClassVar, Union, cast

from cuenca_validations.types import BalanceEntryQuery, EntryType

Expand All @@ -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):
Expand Down
4 changes: 2 additions & 2 deletions cuenca/resources/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion cuenca/resources/card_validations.py
Original file line number Diff line number Diff line change
Expand Up @@ -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':
Expand Down
2 changes: 1 addition & 1 deletion cuenca/resources/curp_validations.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand Down
10 changes: 8 additions & 2 deletions cuenca/resources/file_batches.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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())
)
9 changes: 7 additions & 2 deletions cuenca/resources/files.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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,
)
Expand Down
2 changes: 1 addition & 1 deletion cuenca/resources/kyc_verifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
2 changes: 1 addition & 1 deletion cuenca/resources/limited_wallets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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':
"""
Expand Down
2 changes: 1 addition & 1 deletion cuenca/resources/platforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions cuenca/resources/sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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':
Expand Down
3 changes: 2 additions & 1 deletion cuenca/resources/transfers.py
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion cuenca/resources/user_lists_validation.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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',
Expand Down
6 changes: 4 additions & 2 deletions cuenca/resources/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
):
Expand All @@ -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(
Expand Down

0 comments on commit 12a8556

Please sign in to comment.