diff --git a/payments/__init__.py b/payments/__init__.py index 2a6f6dbf5..3ca8e04d9 100644 --- a/payments/__init__.py +++ b/payments/__init__.py @@ -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 @@ -9,6 +10,9 @@ from payments import version # type: ignore[attr-defined] +if TYPE_CHECKING: + from decimal import Decimal + __version__ = version.version @@ -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: diff --git a/payments/authorizenet/__init__.py b/payments/authorizenet/__init__.py index edf76525f..cf8aebe9d 100644 --- a/payments/authorizenet/__init__.py +++ b/payments/authorizenet/__init__.py @@ -1,3 +1,5 @@ +from __future__ import annotations + import requests from django.core.exceptions import ImproperlyConfigured from django.http import HttpResponseForbidden diff --git a/payments/authorizenet/forms.py b/payments/authorizenet/forms.py index 9185209bb..be61c6ab8 100644 --- a/payments/authorizenet/forms.py +++ b/payments/authorizenet/forms.py @@ -1,3 +1,5 @@ +from __future__ import annotations + from payments import PaymentStatus from payments.forms import CreditCardPaymentForm diff --git a/payments/authorizenet/test_authorizenet.py b/payments/authorizenet/test_authorizenet.py index 81fa8fbcd..ccd2997cb 100644 --- a/payments/authorizenet/test_authorizenet.py +++ b/payments/authorizenet/test_authorizenet.py @@ -1,3 +1,5 @@ +from __future__ import annotations + from datetime import date from unittest import TestCase from unittest.mock import MagicMock diff --git a/payments/braintree/__init__.py b/payments/braintree/__init__.py index 82ddc3577..efb8b05c8 100644 --- a/payments/braintree/__init__.py +++ b/payments/braintree/__init__.py @@ -1,3 +1,5 @@ +from __future__ import annotations + import braintree from django.core.exceptions import ImproperlyConfigured diff --git a/payments/braintree/forms.py b/payments/braintree/forms.py index dbf989b62..5a2f9b5e9 100644 --- a/payments/braintree/forms.py +++ b/payments/braintree/forms.py @@ -1,3 +1,5 @@ +from __future__ import annotations + import braintree from payments import PaymentStatus diff --git a/payments/braintree/test_braintree.py b/payments/braintree/test_braintree.py index b56e4f007..e1700fca9 100644 --- a/payments/braintree/test_braintree.py +++ b/payments/braintree/test_braintree.py @@ -1,3 +1,5 @@ +from __future__ import annotations + from datetime import date from unittest import TestCase from unittest.mock import MagicMock diff --git a/payments/coinbase/__init__.py b/payments/coinbase/__init__.py index 009ba6a24..2a1383ba9 100644 --- a/payments/coinbase/__init__.py +++ b/payments/coinbase/__init__.py @@ -1,3 +1,5 @@ +from __future__ import annotations + import hashlib import hmac import json diff --git a/payments/coinbase/test_coinbase.py b/payments/coinbase/test_coinbase.py index 8a732ef28..200c191ec 100644 --- a/payments/coinbase/test_coinbase.py +++ b/payments/coinbase/test_coinbase.py @@ -1,3 +1,5 @@ +from __future__ import annotations + import hashlib import json from decimal import Decimal diff --git a/payments/core.py b/payments/core.py index c96c46e1c..386cbaa89 100644 --- a/payments/core.py +++ b/payments/core.py @@ -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 + from .models import BasePayment -PAYMENT_VARIANTS: Dict[str, Tuple[str, Dict]] = { +PAYMENT_VARIANTS: dict[str, tuple[str, dict]] = { "default": ("payments.dummy.DummyProvider", {}) } @@ -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``. @@ -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 diff --git a/payments/cybersource/__init__.py b/payments/cybersource/__init__.py index 32644b247..da87cb068 100644 --- a/payments/cybersource/__init__.py +++ b/payments/cybersource/__init__.py @@ -1,3 +1,5 @@ +from __future__ import annotations + import contextlib import datetime import os.path diff --git a/payments/cybersource/forms.py b/payments/cybersource/forms.py index b041ba905..2ad01002a 100644 --- a/payments/cybersource/forms.py +++ b/payments/cybersource/forms.py @@ -1,3 +1,5 @@ +from __future__ import annotations + from uuid import uuid4 from django import forms diff --git a/payments/cybersource/test_cybersource.py b/payments/cybersource/test_cybersource.py index 92e1b5049..fdbf07a47 100644 --- a/payments/cybersource/test_cybersource.py +++ b/payments/cybersource/test_cybersource.py @@ -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 @@ -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" diff --git a/payments/dotpay/__init__.py b/payments/dotpay/__init__.py index 00c199966..f4636e965 100644 --- a/payments/dotpay/__init__.py +++ b/payments/dotpay/__init__.py @@ -1,3 +1,5 @@ +from __future__ import annotations + from decimal import Decimal from django.core.exceptions import ImproperlyConfigured @@ -50,7 +52,7 @@ def __init__( lang="pl", lock=False, type=2, - **kwargs + **kwargs, ): self.seller_id = seller_id self.pin = pin diff --git a/payments/dotpay/forms.py b/payments/dotpay/forms.py index 2601fb5f0..59fa89c60 100644 --- a/payments/dotpay/forms.py +++ b/payments/dotpay/forms.py @@ -1,3 +1,5 @@ +from __future__ import annotations + import hashlib from django import forms diff --git a/payments/dotpay/test_dotpay.py b/payments/dotpay/test_dotpay.py index 4c943cf8d..54dc65248 100644 --- a/payments/dotpay/test_dotpay.py +++ b/payments/dotpay/test_dotpay.py @@ -1,3 +1,5 @@ +from __future__ import annotations + import hashlib from unittest import TestCase from unittest.mock import MagicMock diff --git a/payments/dummy/__init__.py b/payments/dummy/__init__.py index 531d5778d..800cd31cd 100644 --- a/payments/dummy/__init__.py +++ b/payments/dummy/__init__.py @@ -1,3 +1,5 @@ +from __future__ import annotations + from urllib.error import URLError from urllib.parse import urlencode diff --git a/payments/dummy/forms.py b/payments/dummy/forms.py index 0b5d62a32..04d755316 100644 --- a/payments/dummy/forms.py +++ b/payments/dummy/forms.py @@ -1,3 +1,5 @@ +from __future__ import annotations + from django import forms from payments import FraudStatus diff --git a/payments/dummy/test_dummy.py b/payments/dummy/test_dummy.py index 63fc43099..37f6bc32c 100644 --- a/payments/dummy/test_dummy.py +++ b/payments/dummy/test_dummy.py @@ -1,3 +1,5 @@ +from __future__ import annotations + from unittest import TestCase from unittest.mock import MagicMock from urllib.error import URLError diff --git a/payments/fields.py b/payments/fields.py index 125590b7c..59fbeb6ab 100644 --- a/payments/fields.py +++ b/payments/fields.py @@ -1,3 +1,5 @@ +from __future__ import annotations + import re from calendar import monthrange from datetime import date diff --git a/payments/forms.py b/payments/forms.py index a999bb81c..c16e84074 100644 --- a/payments/forms.py +++ b/payments/forms.py @@ -1,3 +1,5 @@ +from __future__ import annotations + from collections import OrderedDict from django import forms diff --git a/payments/mercadopago/__init__.py b/payments/mercadopago/__init__.py index 4eb56d3b9..0f1533e8e 100644 --- a/payments/mercadopago/__init__.py +++ b/payments/mercadopago/__init__.py @@ -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 @@ -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 logger = logging.getLogger(__name__) diff --git a/payments/mercadopago/test_mercadopago.py b/payments/mercadopago/test_mercadopago.py index 26b7ffd94..7a0527ec8 100644 --- a/payments/mercadopago/test_mercadopago.py +++ b/payments/mercadopago/test_mercadopago.py @@ -1,10 +1,11 @@ -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 @@ -12,6 +13,9 @@ from payments import RedirectNeeded from payments.mercadopago import MercadoPagoProvider +if TYPE_CHECKING: + from django.test import RequestFactory + class Payment(Mock): id = 1 @@ -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 = "john@doe.com" def change_status(self, status, message=""): diff --git a/payments/models.py b/payments/models.py index f2ee0bfbe..922156bf3 100644 --- a/payments/models.py +++ b/payments/models.py @@ -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 @@ -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. """ diff --git a/payments/paypal/__init__.py b/payments/paypal/__init__.py index 97982f014..e7f2d0e58 100644 --- a/payments/paypal/__init__.py +++ b/payments/paypal/__init__.py @@ -1,3 +1,5 @@ +from __future__ import annotations + import json import logging from datetime import timedelta diff --git a/payments/paypal/forms.py b/payments/paypal/forms.py index 8b1f77528..b73ee4a3d 100644 --- a/payments/paypal/forms.py +++ b/payments/paypal/forms.py @@ -1,3 +1,5 @@ +from __future__ import annotations + from requests.exceptions import HTTPError from payments import PaymentStatus diff --git a/payments/paypal/test_paypal.py b/payments/paypal/test_paypal.py index 461b94ccf..28fc7149d 100644 --- a/payments/paypal/test_paypal.py +++ b/payments/paypal/test_paypal.py @@ -1,3 +1,5 @@ +from __future__ import annotations + import json from datetime import date from decimal import Decimal diff --git a/payments/sagepay/__init__.py b/payments/sagepay/__init__.py index 363da0653..3577cadfd 100644 --- a/payments/sagepay/__init__.py +++ b/payments/sagepay/__init__.py @@ -1,3 +1,5 @@ +from __future__ import annotations + import binascii from cryptography.hazmat.backends import default_backend diff --git a/payments/sagepay/test_sagepay.py b/payments/sagepay/test_sagepay.py index 204cb7263..564d1370c 100644 --- a/payments/sagepay/test_sagepay.py +++ b/payments/sagepay/test_sagepay.py @@ -1,3 +1,5 @@ +from __future__ import annotations + from unittest import TestCase from unittest.mock import MagicMock from unittest.mock import Mock diff --git a/payments/signals.py b/payments/signals.py index 06d50dd42..776274dc5 100644 --- a/payments/signals.py +++ b/payments/signals.py @@ -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 diff --git a/payments/sofort/__init__.py b/payments/sofort/__init__.py index 2ecf0aedf..9bcf30fa9 100644 --- a/payments/sofort/__init__.py +++ b/payments/sofort/__init__.py @@ -1,3 +1,5 @@ +from __future__ import annotations + import json import requests diff --git a/payments/sofort/test_sofort.py b/payments/sofort/test_sofort.py index 7bc19307b..071cf0492 100644 --- a/payments/sofort/test_sofort.py +++ b/payments/sofort/test_sofort.py @@ -1,3 +1,5 @@ +from __future__ import annotations + import json from unittest import TestCase from unittest.mock import MagicMock diff --git a/payments/stripe/__init__.py b/payments/stripe/__init__.py index 8d10cf935..05930f30e 100644 --- a/payments/stripe/__init__.py +++ b/payments/stripe/__init__.py @@ -1,3 +1,5 @@ +from __future__ import annotations + import json import warnings from decimal import Decimal diff --git a/payments/stripe/forms.py b/payments/stripe/forms.py index 2999e1b9d..a4ef39d58 100644 --- a/payments/stripe/forms.py +++ b/payments/stripe/forms.py @@ -1,3 +1,5 @@ +from __future__ import annotations + import json import stripe diff --git a/payments/stripe/providers.py b/payments/stripe/providers.py index 65cfc26a2..69c9c2789 100644 --- a/payments/stripe/providers.py +++ b/payments/stripe/providers.py @@ -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 @@ -18,10 +19,10 @@ @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 @@ -29,17 +30,17 @@ 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 = [ diff --git a/payments/stripe/test_stripe.py b/payments/stripe/test_stripe.py index 94ba97e74..b9cd05067 100644 --- a/payments/stripe/test_stripe.py +++ b/payments/stripe/test_stripe.py @@ -1,3 +1,5 @@ +from __future__ import annotations + from contextlib import contextmanager from unittest import TestCase from unittest.mock import Mock diff --git a/payments/stripe/test_stripev3.py b/payments/stripe/test_stripev3.py index 787d8d07b..ea3c7a506 100644 --- a/payments/stripe/test_stripev3.py +++ b/payments/stripe/test_stripev3.py @@ -1,3 +1,5 @@ +from __future__ import annotations + from unittest import TestCase from unittest.mock import Mock from unittest.mock import patch diff --git a/payments/stripe/widgets.py b/payments/stripe/widgets.py index de96b71dc..db5395924 100644 --- a/payments/stripe/widgets.py +++ b/payments/stripe/widgets.py @@ -1,3 +1,5 @@ +from __future__ import annotations + from django.forms.utils import flatatt from django.forms.widgets import HiddenInput from django.forms.widgets import Input diff --git a/payments/test_core.py b/payments/test_core.py index 87f7e004f..8c9190dd6 100644 --- a/payments/test_core.py +++ b/payments/test_core.py @@ -1,3 +1,5 @@ +from __future__ import annotations + from datetime import date from decimal import Decimal from unittest import TestCase diff --git a/payments/urls.py b/payments/urls.py index a64b54f65..b1decdaec 100644 --- a/payments/urls.py +++ b/payments/urls.py @@ -2,6 +2,8 @@ This module is responsible for automatic processing of provider callback data (asynchronous transaction updates). """ +from __future__ import annotations + from django.db.transaction import atomic from django.http import Http404 from django.shortcuts import get_object_or_404 diff --git a/payments/utils.py b/payments/utils.py index 9c7ac2326..8ea01738d 100644 --- a/payments/utils.py +++ b/payments/utils.py @@ -1,3 +1,5 @@ +from __future__ import annotations + from datetime import date from django.utils.translation import gettext_lazy as _ diff --git a/payments/widgets.py b/payments/widgets.py index 68922fa73..a857de91c 100644 --- a/payments/widgets.py +++ b/payments/widgets.py @@ -1,3 +1,5 @@ +from __future__ import annotations + import re from django.forms.widgets import MultiWidget diff --git a/setup.py b/setup.py index d01e70024..34fca57ff 100755 --- a/setup.py +++ b/setup.py @@ -1,4 +1,6 @@ #!/usr/bin/env python +from __future__ import annotations + from setuptools import setup PACKAGES = [ diff --git a/test_settings.py b/test_settings.py index 039e21e08..4e362d566 100644 --- a/test_settings.py +++ b/test_settings.py @@ -1,3 +1,5 @@ +from __future__ import annotations + import os PROJECT_ROOT = os.path.normpath(os.path.join(os.path.dirname(__file__), "payments")) diff --git a/testapp/manage.py b/testapp/manage.py index b6b55fc14..4c28ac632 100755 --- a/testapp/manage.py +++ b/testapp/manage.py @@ -1,5 +1,7 @@ #!/usr/bin/env python """Django's command-line utility for administrative tasks.""" +from __future__ import annotations + import os import sys diff --git a/testapp/testapp/asgi.py b/testapp/testapp/asgi.py index 9f3d83d7f..9aa05edbe 100644 --- a/testapp/testapp/asgi.py +++ b/testapp/testapp/asgi.py @@ -6,6 +6,7 @@ For more information on this file, see https://docs.djangoproject.com/en/3.2/howto/deployment/asgi/ """ +from __future__ import annotations import os diff --git a/testapp/testapp/settings.py b/testapp/testapp/settings.py index c74b4f11e..a0673d9b7 100644 --- a/testapp/testapp/settings.py +++ b/testapp/testapp/settings.py @@ -1,3 +1,5 @@ +from __future__ import annotations + from pathlib import Path import dj_database_url diff --git a/testapp/testapp/testmain/forms.py b/testapp/testapp/testmain/forms.py index 53b6476e6..4815c57f2 100644 --- a/testapp/testapp/testmain/forms.py +++ b/testapp/testapp/testmain/forms.py @@ -1,3 +1,5 @@ +from __future__ import annotations + from django import forms from payments import get_payment_model diff --git a/testapp/testapp/testmain/migrations/0001_initial.py b/testapp/testapp/testmain/migrations/0001_initial.py index 278cfc1de..fc5fc439c 100644 --- a/testapp/testapp/testmain/migrations/0001_initial.py +++ b/testapp/testapp/testmain/migrations/0001_initial.py @@ -1,4 +1,5 @@ # Generated by Django 3.2.9 on 2021-11-23 20:48 +from __future__ import annotations from django.db import migrations from django.db import models diff --git a/testapp/testapp/testmain/migrations/0002_payment_billing_phone.py b/testapp/testapp/testmain/migrations/0002_payment_billing_phone.py index e156b1766..5c0146c4a 100644 --- a/testapp/testapp/testmain/migrations/0002_payment_billing_phone.py +++ b/testapp/testapp/testmain/migrations/0002_payment_billing_phone.py @@ -1,4 +1,5 @@ # Generated by Django 4.2.3 on 2023-07-18 16:54 +from __future__ import annotations import phonenumber_field.modelfields from django.db import migrations diff --git a/testapp/testapp/testmain/models.py b/testapp/testapp/testmain/models.py index c95cd25f9..86d7db239 100644 --- a/testapp/testapp/testmain/models.py +++ b/testapp/testapp/testmain/models.py @@ -1,3 +1,5 @@ +from __future__ import annotations + from payments import PurchasedItem from payments.models import BasePayment diff --git a/testapp/testapp/testmain/urls.py b/testapp/testapp/testmain/urls.py index 3c64e3e2b..c373f78ce 100644 --- a/testapp/testapp/testmain/urls.py +++ b/testapp/testapp/testmain/urls.py @@ -1,3 +1,5 @@ +from __future__ import annotations + from django.urls import path from testapp.testmain import views diff --git a/testapp/testapp/testmain/views.py b/testapp/testapp/testmain/views.py index 82af0a667..b8562fdb6 100644 --- a/testapp/testapp/testmain/views.py +++ b/testapp/testapp/testmain/views.py @@ -1,3 +1,5 @@ +from __future__ import annotations + from django.http import HttpResponse from django.shortcuts import get_object_or_404 from django.shortcuts import redirect diff --git a/testapp/testapp/urls.py b/testapp/testapp/urls.py index 25eb93419..d52bbbc29 100644 --- a/testapp/testapp/urls.py +++ b/testapp/testapp/urls.py @@ -1,3 +1,5 @@ +from __future__ import annotations + from django.contrib import admin from django.urls import include from django.urls import path diff --git a/testapp/testapp/wsgi.py b/testapp/testapp/wsgi.py index 4f7d0bfdc..2b902f76c 100644 --- a/testapp/testapp/wsgi.py +++ b/testapp/testapp/wsgi.py @@ -6,6 +6,7 @@ For more information on this file, see https://docs.djangoproject.com/en/3.2/howto/deployment/wsgi/ """ +from __future__ import annotations import os