Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Fix mypy errors for extensions

IKarbowiak16eb926ae569
diff --git a/saleor/extensions/plugins/anonymize/plugin.py b/saleor/extensions/plugins/anonymize/plugin.py
index 44d85bf8c1..2bb6939d86 100644
--- a/saleor/extensions/plugins/anonymize/plugin.py
+++ b/saleor/extensions/plugins/anonymize/plugin.py
@@ -25,7 +25,7 @@ class AnonymizePlugin(BasePlugin):
previous_value: "Address",
) -> "Address":
if address.phone:
- address.phone = ""
+ address.phone = "" # type: ignore
address = obfuscate_address(address)
address.save()
return address
diff --git a/saleor/extensions/plugins/avatax/__init__.py b/saleor/extensions/plugins/avatax/__init__.py
index 16898398bf..54ef4a74df 100644
--- a/saleor/extensions/plugins/avatax/__init__.py
+++ b/saleor/extensions/plugins/avatax/__init__.py
@@ -2,6 +2,7 @@ import json
import logging
from dataclasses import dataclass
from datetime import date
+from decimal import Decimal
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Union
from urllib.parse import urljoin
@@ -18,7 +19,7 @@ if TYPE_CHECKING:
# flake8: noqa
from ....checkout.models import Checkout
from ....order.models import Order
- from ....product.models import Product, ProductVariant
+ from ....product.models import Product, ProductVariant, ProductType
logger = logging.getLogger(__name__)
@@ -176,9 +177,9 @@ def taxes_need_new_fetch(data: Dict[str, Any], taxes_token: str) -> bool:
def append_line_to_data(
- data: List[Dict[str, str]],
+ data: List[Dict[str, Union[str, int, bool, None]]],
quantity: int,
- amount: str,
+ amount: Decimal,
tax_code: str,
item_code: str,
description: str = None,
@@ -206,7 +207,7 @@ def append_shipping_to_data(data: List[Dict], shipping_method):
append_line_to_data(
data,
quantity=1,
- amount=str(shipping_method.price.amount),
+ amount=shipping_method.price.amount,
tax_code=COMMON_CARRIER_CODE,
item_code="Shipping",
)
@@ -214,8 +215,8 @@ def append_shipping_to_data(data: List[Dict], shipping_method):
def get_checkout_lines_data(
checkout: "Checkout", discounts=None
-) -> List[Dict[str, str]]:
- data = []
+) -> List[Dict[str, Union[str, int, bool, None]]]:
+ data: List[Dict[str, Union[str, int, bool, None]]] = []
lines = checkout.lines.prefetch_related(
"variant__product__category",
"variant__product__collections",
@@ -232,9 +233,9 @@ def get_checkout_lines_data(
append_line_to_data(
data=data,
quantity=line.quantity,
- amount=str(
- base_calculations.base_checkout_line_total(line, discounts).gross.amount
- ),
+ amount=base_calculations.base_checkout_line_total(
+ line, discounts
+ ).gross.amount,
tax_code=tax_code,
item_code=line.variant.sku,
description=description,
@@ -244,15 +245,17 @@ def get_checkout_lines_data(
return data
-def get_order_lines_data(order: "Order") -> List[Dict[str, str]]:
- data = []
+def get_order_lines_data(
+ order: "Order",
+) -> List[Dict[str, Union[str, int, bool, None]]]:
+ data: List[Dict[str, Union[str, int, bool, None]]] = []
lines = order.lines.prefetch_related(
"variant__product__category",
"variant__product__collections",
"variant__product__product_type",
)
for line in lines:
- if not line.variant.product.charge_taxes:
+ if not line.variant or not line.variant.product.charge_taxes:
continue
product = line.variant.product
product_type = line.variant.product.product_type
@@ -348,7 +351,7 @@ def generate_request_data_from_checkout(
transaction_type=transaction_type,
lines=lines,
transaction_token=transaction_token or str(checkout.token),
- address=address.as_data(),
+ address=address.as_data() if address else {},
customer_code=checkout.user.id if checkout.user else 0,
customer_email=checkout.email,
config=config,
@@ -410,7 +413,7 @@ def get_order_tax_data(
transaction_type=transaction,
lines=lines,
transaction_token=order.token,
- address=address.as_data(),
+ address=address.as_data() if address else {},
customer_code=order.user.id if order.user else None,
customer_email=order.user_email,
config=config,
@@ -422,9 +425,7 @@ def get_order_tax_data(
return response
-def generate_tax_codes_dict(
- response: Dict[str, Union[str, int, bool]]
-) -> Dict[str, str]:
+def generate_tax_codes_dict(response: Dict[str, Any]) -> Dict[str, str]:
tax_codes = {}
for line in response.get("value", []):
if line.get("isActive"):
@@ -449,7 +450,7 @@ def get_cached_tax_codes_or_fetch(
return tax_codes
-def retrieve_tax_code_from_meta(obj: Union["Product", "ProductVariant"]):
+def retrieve_tax_code_from_meta(obj: Union["Product", "ProductVariant", "ProductType"]):
tax = obj.meta.get("taxes", {}).get(META_FIELD, {})
# O9999999 - "Temporary Unmapped Other SKU - taxable default"
return tax.get("code", "O9999999")
diff --git a/saleor/extensions/plugins/avatax/plugin.py b/saleor/extensions/plugins/avatax/plugin.py
index 9fa9ce1e2d..174819fa4d 100644
--- a/saleor/extensions/plugins/avatax/plugin.py
+++ b/saleor/extensions/plugins/avatax/plugin.py
@@ -30,8 +30,10 @@ from . import (
from .tasks import api_post_request_task
if TYPE_CHECKING:
+ # flake8: noqa
from ....checkout.models import Checkout, CheckoutLine
from ....order.models import Order, OrderLine
+ from ....product.models import Product, ProductType
from ...models import PluginConfiguration
from ....discount.types import DiscountsListType
@@ -201,7 +203,7 @@ class AvataxPlugin(BasePlugin):
if not response or "error" in response:
return base_subtotal
- currency = response.get("currencyCode")
+ currency = str(response.get("currencyCode"))
return self._calculate_checkout_subtotal(currency, response.get("lines", []))
def _calculate_checkout_shipping(
@@ -238,7 +240,7 @@ class AvataxPlugin(BasePlugin):
if not response or "error" in response:
return base_shipping_price
- currency = response.get("currencyCode")
+ currency = str(response.get("currencyCode"))
return self._calculate_checkout_shipping(
currency, response.get("lines", []), base_shipping_price
)
@@ -367,7 +369,9 @@ class AvataxPlugin(BasePlugin):
net = Money(amount=net, currency=currency)
return TaxedMoney(net=net, gross=gross)
return TaxedMoney(
- net=order.shipping_method.price, gross=order.shipping_method.price
+ # Ignore mypy checking because it is checked in _validate_order
+ net=order.shipping_method.price, # type: ignore
+ gross=order.shipping_method.price, # type: ignore
)
def get_tax_rate_type_choices(self, previous_value: Any) -> List[TaxType]:
@@ -436,7 +440,7 @@ class AvataxPlugin(BasePlugin):
)
raise ValidationError(
error_msg + ", ".join(missing_fields),
- code=ExtensionsErrorCode.PLUGIN_MISCONFIGURED,
+ code=str(ExtensionsErrorCode.PLUGIN_MISCONFIGURED),
)
@classmethod
diff --git a/saleor/extensions/plugins/vatlayer/plugin.py b/saleor/extensions/plugins/vatlayer/plugin.py
index 4d11571aa5..5be8194209 100644
--- a/saleor/extensions/plugins/vatlayer/plugin.py
+++ b/saleor/extensions/plugins/vatlayer/plugin.py
@@ -20,9 +20,10 @@ from . import (
)
if TYPE_CHECKING:
-
+ # flake8: noqa
from ....checkout.models import Checkout, CheckoutLine
- from ....product.models import Product
+ from ....discount import DiscountInfo
+ from ....product.models import Product, ProductType
from ....account.models import Address
from ....order.models import OrderLine, Order
from ...models import PluginConfiguration
@@ -164,6 +165,8 @@ class VatlayerPlugin(BasePlugin):
address = order_line.order.shipping_address or order_line.order.billing_address
country = address.country if address else None
variant = order_line.variant
+ if not variant:
+ return previous_value
return self.__apply_taxes_to_product(
variant.product, order_line.unit_price, country
)
@@ -297,7 +300,7 @@ class VatlayerPlugin(BasePlugin):
if not settings.VATLAYER_ACCESS_KEY and plugin_configuration.active:
raise ValidationError(
"Cannot be enabled without provided 'settings.VATLAYER_ACCESS_KEY'",
- code=ExtensionsErrorCode.PLUGIN_MISCONFIGURED,
+ code=str(ExtensionsErrorCode.PLUGIN_MISCONFIGURED),
)
@classmethod