Skip to content

Commit

Permalink
Modernise all type annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
WhyNotHugo committed Oct 14, 2023
1 parent a292d9a commit d265bbd
Show file tree
Hide file tree
Showing 55 changed files with 137 additions and 29 deletions.
10 changes: 7 additions & 3 deletions payments/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from decimal import Decimal
from __future__ import annotations

from typing import TYPE_CHECKING
from typing import NamedTuple
from typing import Optional

from django.apps import apps
from django.conf import settings
Expand All @@ -9,6 +10,9 @@

from payments import version # type: ignore[attr-defined]

if TYPE_CHECKING:
from decimal import Decimal

Check warning on line 14 in payments/__init__.py

View check run for this annotation

Codecov / codecov/patch

payments/__init__.py#L14

Added line #L14 was not covered by tests

__version__ = version.version


Expand All @@ -20,7 +24,7 @@ class PurchasedItem(NamedTuple):
price: Decimal
currency: str
sku: str
tax_rate: Optional[Decimal] = None
tax_rate: Decimal | None = None


class PaymentStatus:
Expand Down
2 changes: 2 additions & 0 deletions payments/authorizenet/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import requests
from django.core.exceptions import ImproperlyConfigured
from django.http import HttpResponseForbidden
Expand Down
2 changes: 2 additions & 0 deletions payments/authorizenet/forms.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from payments import PaymentStatus
from payments.forms import CreditCardPaymentForm

Expand Down
2 changes: 2 additions & 0 deletions payments/authorizenet/test_authorizenet.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from datetime import date
from unittest import TestCase
from unittest.mock import MagicMock
Expand Down
2 changes: 2 additions & 0 deletions payments/braintree/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import braintree
from django.core.exceptions import ImproperlyConfigured

Expand Down
2 changes: 2 additions & 0 deletions payments/braintree/forms.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import braintree

from payments import PaymentStatus
Expand Down
2 changes: 2 additions & 0 deletions payments/braintree/test_braintree.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from datetime import date
from unittest import TestCase
from unittest.mock import MagicMock
Expand Down
2 changes: 2 additions & 0 deletions payments/coinbase/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import hashlib
import hmac
import json
Expand Down
2 changes: 2 additions & 0 deletions payments/coinbase/test_coinbase.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import hashlib
import json
from decimal import Decimal
Expand Down
14 changes: 7 additions & 7 deletions payments/core.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
from __future__ import annotations

import re
from typing import TYPE_CHECKING
from typing import Dict
from typing import Optional
from typing import Tuple
from urllib.parse import urlencode
from urllib.parse import urljoin

from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from django.http import HttpRequest
from django.utils.module_loading import import_string

if TYPE_CHECKING:
from django.http import HttpRequest

Check warning on line 13 in payments/core.py

View check run for this annotation

Codecov / codecov/patch

payments/core.py#L13

Added line #L13 was not covered by tests

from .models import BasePayment

PAYMENT_VARIANTS: Dict[str, Tuple[str, Dict]] = {
PAYMENT_VARIANTS: dict[str, tuple[str, dict]] = {
"default": ("payments.dummy.DummyProvider", {})
}

Expand Down Expand Up @@ -156,7 +156,7 @@ def refund(self, payment, amount=None):
PROVIDER_CACHE = {}


def _default_provider_factory(variant: str, payment: Optional["BasePayment"] = None):
def _default_provider_factory(variant: str, payment: BasePayment | None = None):
"""Return the provider instance based on ``variant``.
:arg variant: The name of a variant defined in ``PAYMENT_VARIANTS``.
Expand Down Expand Up @@ -194,7 +194,7 @@ def _default_provider_factory(variant: str, payment: Optional["BasePayment"] = N
]


def get_credit_card_issuer(number: str) -> Tuple[Optional[str], Optional[str]]:
def get_credit_card_issuer(number: str) -> tuple[str | None, str | None]:
for regexp, card_type, name in CARD_TYPES:
if re.match(regexp, number):
return card_type, name
Expand Down
2 changes: 2 additions & 0 deletions payments/cybersource/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import contextlib
import datetime
import os.path
Expand Down
2 changes: 2 additions & 0 deletions payments/cybersource/forms.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from uuid import uuid4

from django import forms
Expand Down
5 changes: 3 additions & 2 deletions payments/cybersource/test_cybersource.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

from datetime import date
from decimal import Decimal
from typing import Dict
from unittest import TestCase
from unittest.mock import MagicMock
from unittest.mock import Mock
Expand Down Expand Up @@ -43,7 +44,7 @@ class Payment(Mock):

class attrs:
fingerprint_session_id = "fake"
merchant_defined_data: Dict[str, str] = {}
merchant_defined_data: dict[str, str] = {}

def get_process_url(self):
return "http://example.com"
Expand Down
4 changes: 3 additions & 1 deletion payments/dotpay/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from decimal import Decimal

from django.core.exceptions import ImproperlyConfigured
Expand Down Expand Up @@ -50,7 +52,7 @@ def __init__(
lang="pl",
lock=False,
type=2,
**kwargs
**kwargs,
):
self.seller_id = seller_id
self.pin = pin
Expand Down
2 changes: 2 additions & 0 deletions payments/dotpay/forms.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import hashlib

from django import forms
Expand Down
2 changes: 2 additions & 0 deletions payments/dotpay/test_dotpay.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import hashlib
from unittest import TestCase
from unittest.mock import MagicMock
Expand Down
2 changes: 2 additions & 0 deletions payments/dummy/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from urllib.error import URLError
from urllib.parse import urlencode

Expand Down
2 changes: 2 additions & 0 deletions payments/dummy/forms.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from django import forms

from payments import FraudStatus
Expand Down
2 changes: 2 additions & 0 deletions payments/dummy/test_dummy.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from unittest import TestCase
from unittest.mock import MagicMock
from urllib.error import URLError
Expand Down
2 changes: 2 additions & 0 deletions payments/fields.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import re
from calendar import monthrange
from datetime import date
Expand Down
2 changes: 2 additions & 0 deletions payments/forms.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from collections import OrderedDict

from django import forms
Expand Down
7 changes: 6 additions & 1 deletion payments/mercadopago/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from __future__ import annotations

import json
import logging
import re
from typing import TYPE_CHECKING
from uuid import uuid4

from django.http import HttpRequest
Expand All @@ -12,7 +15,9 @@
from payments import PaymentStatus
from payments import RedirectNeeded
from payments.core import BasicProvider
from payments.models import BasePayment

if TYPE_CHECKING:
from payments.models import BasePayment

Check warning on line 20 in payments/mercadopago/__init__.py

View check run for this annotation

Codecov / codecov/patch

payments/mercadopago/__init__.py#L20

Added line #L20 was not covered by tests

logger = logging.getLogger(__name__)

Expand Down
10 changes: 7 additions & 3 deletions payments/mercadopago/test_mercadopago.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
from typing import Optional
from __future__ import annotations

from typing import TYPE_CHECKING
from unittest.mock import Mock
from unittest.mock import call
from unittest.mock import patch

import pytest
from django.test import RequestFactory

from payments import PaymentError
from payments import PaymentStatus
from payments import PurchasedItem
from payments import RedirectNeeded
from payments.mercadopago import MercadoPagoProvider

if TYPE_CHECKING:
from django.test import RequestFactory


class Payment(Mock):
id = 1
Expand All @@ -23,7 +27,7 @@ class Payment(Mock):
tax = 10
total = 100
captured_amount = 0
transaction_id: Optional[str] = None
transaction_id: str | None = None
billing_email = "[email protected]"

def change_status(self, status, message=""):
Expand Down
5 changes: 3 additions & 2 deletions payments/models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import json
from typing import Iterable
from typing import Union
from uuid import uuid4

from django.db import models
Expand Down Expand Up @@ -87,7 +88,7 @@ class BasePayment(models.Model):
class Meta:
abstract = True

def change_status(self, status: Union[PaymentStatus, str], message=""):
def change_status(self, status: PaymentStatus | str, message=""):
"""
Updates the Payment status and sends the status_changed signal.
"""
Expand Down
2 changes: 2 additions & 0 deletions payments/paypal/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import json
import logging
from datetime import timedelta
Expand Down
2 changes: 2 additions & 0 deletions payments/paypal/forms.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from requests.exceptions import HTTPError

from payments import PaymentStatus
Expand Down
2 changes: 2 additions & 0 deletions payments/paypal/test_paypal.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import json
from datetime import date
from decimal import Decimal
Expand Down
2 changes: 2 additions & 0 deletions payments/sagepay/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import binascii

from cryptography.hazmat.backends import default_backend
Expand Down
2 changes: 2 additions & 0 deletions payments/sagepay/test_sagepay.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from unittest import TestCase
from unittest.mock import MagicMock
from unittest.mock import Mock
Expand Down
2 changes: 2 additions & 0 deletions payments/signals.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from django.dispatch import Signal

# Signal sent whenever status is changed for a Payment. This usually happens
Expand Down
2 changes: 2 additions & 0 deletions payments/sofort/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import json

import requests
Expand Down
2 changes: 2 additions & 0 deletions payments/sofort/test_sofort.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import json
from unittest import TestCase
from unittest.mock import MagicMock
Expand Down
2 changes: 2 additions & 0 deletions payments/stripe/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import json
import warnings
from decimal import Decimal
Expand Down
2 changes: 2 additions & 0 deletions payments/stripe/forms.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import json

import stripe
Expand Down
21 changes: 11 additions & 10 deletions payments/stripe/providers.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from __future__ import annotations

import json
from dataclasses import asdict
from dataclasses import dataclass
from dataclasses import field
from typing import Any
from typing import Optional

import stripe
from django.http import JsonResponse
Expand All @@ -18,28 +19,28 @@
@dataclass
class StripeProductData:
name: str
description: Optional[str] = field(init=False, repr=False, default=None)
images: Optional[str] = field(init=False, repr=False, default=None)
metadata: Optional[dict] = field(init=False, repr=False, default=None)
tax_code: Optional[str] = field(init=False, repr=False, default=None)
description: str | None = field(init=False, repr=False, default=None)
images: str | None = field(init=False, repr=False, default=None)
metadata: dict | None = field(init=False, repr=False, default=None)
tax_code: str | None = field(init=False, repr=False, default=None)


@dataclass
class StripePriceData:
currency: str
product_data: StripeProductData
unit_amount: int
recurring: Optional[dict] = field(init=False, repr=False, default=None)
tax_behavior: Optional[str] = field(init=False, repr=False, default=None)
recurring: dict | None = field(init=False, repr=False, default=None)
tax_behavior: str | None = field(init=False, repr=False, default=None)


@dataclass
class StripeLineItem:
price_data: StripePriceData
quantity: int
adjustable_quantity: Optional[dict] = field(init=False, repr=False, default=None)
dynamic_tax_rates: Optional[dict] = field(init=False, repr=False, default=None)
tax_rates: Optional[str] = field(init=False, repr=False, default=None)
adjustable_quantity: dict | None = field(init=False, repr=False, default=None)
dynamic_tax_rates: dict | None = field(init=False, repr=False, default=None)
tax_rates: str | None = field(init=False, repr=False, default=None)


zero_decimal_currency: list = [
Expand Down
2 changes: 2 additions & 0 deletions payments/stripe/test_stripe.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from contextlib import contextmanager
from unittest import TestCase
from unittest.mock import Mock
Expand Down
Loading

0 comments on commit d265bbd

Please sign in to comment.