Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Fix typing in reference handler (#17978)
Iga Karbowiak055f6d5fae5c
diff --git a/saleor/graphql/attribute/tests/type_handlers/test_reference_handler.py b/saleor/graphql/attribute/tests/type_handlers/test_reference_handler.pyindex 403664f30a..2b9ebdb6e9 100644--- a/saleor/graphql/attribute/tests/type_handlers/test_reference_handler.py+++ b/saleor/graphql/attribute/tests/type_handlers/test_reference_handler.py@@ -26,7 +26,8 @@ def test_reference_handler_clean_and_validate_product_reference(# thenassert not attribute_errorsassert handler.values_input.references- assert set(handler.values_input.references) == set(product_list)+ assert handler.values_input.reference_objects+ assert set(handler.values_input.reference_objects) == set(product_list)def test_reference_handler_clean_and_validate_page_reference(@@ -46,7 +47,8 @@ def test_reference_handler_clean_and_validate_page_reference(# thenassert not attribute_errorsassert handler.values_input.references- assert set(handler.values_input.references) == set(page_list)+ assert handler.values_input.reference_objects+ assert set(handler.values_input.reference_objects) == set(page_list)def test_reference_handler_clean_and_validate_variant_reference(@@ -68,7 +70,8 @@ def test_reference_handler_clean_and_validate_variant_reference(# thenassert not attribute_errorsassert handler.values_input.references- assert set(handler.values_input.references) == set(product_variant_list)+ assert handler.values_input.reference_objects+ assert set(handler.values_input.reference_objects) == set(product_variant_list)def test_reference_handler_clean_and_validate_category_reference(@@ -87,8 +90,9 @@ def test_reference_handler_clean_and_validate_category_reference(# thenassert not attribute_errors+ assert handler.values_input.reference_objectsassert handler.values_input.references- assert set(handler.values_input.references) == set(category_list)+ assert set(handler.values_input.reference_objects) == set(category_list)def test_reference_handler_clean_and_validate_collection_reference(@@ -110,7 +114,8 @@ def test_reference_handler_clean_and_validate_collection_reference(# thenassert not attribute_errorsassert handler.values_input.references- assert set(handler.values_input.references) == set(collection_list)+ assert handler.values_input.reference_objects+ assert set(handler.values_input.reference_objects) == set(collection_list)def test_single_reference_handler_clean_and_validate_page_reference(@@ -129,7 +134,7 @@ def test_single_reference_handler_clean_and_validate_page_reference(# thenassert not attribute_errors- assert handler.values_input.reference == page+ assert handler.values_input.reference_objects == [page]def test_single_reference_handler_clean_and_validate_variant_reference(@@ -148,7 +153,7 @@ def test_single_reference_handler_clean_and_validate_variant_reference(# thenassert not attribute_errors- assert handler.values_input.reference == variant+ assert handler.values_input.reference_objects == [variant]def test_single_reference_handler_clean_and_validate_category_reference(@@ -167,7 +172,7 @@ def test_single_reference_handler_clean_and_validate_category_reference(# thenassert not attribute_errors- assert handler.values_input.reference == category+ assert handler.values_input.reference_objects == [category]def test_single_reference_handler_clean_and_validate_collection_reference(@@ -186,7 +191,7 @@ def test_single_reference_handler_clean_and_validate_collection_reference(# thenassert not attribute_errors- assert handler.values_input.reference == collection+ assert handler.values_input.reference_objects == [collection]def test_single_reference_handler_clean_and_validate_success(@@ -205,7 +210,7 @@ def test_single_reference_handler_clean_and_validate_success(# thenassert not attribute_errors- assert handler.values_input.reference == product+ assert handler.values_input.reference_objects == [product]def test_reference_handler_clean_and_validate_value_required(@@ -297,7 +302,7 @@ def test_reference_handler_pre_save_value(attribute = product_type_product_reference_attributevalues_input = AttrValuesInput(global_id=graphene.Node.to_global_id("Attribute", attribute.id),- references=product_list,+ reference_objects=product_list,)handler = ReferenceAttributeHandler(attribute, values_input)instance = product@@ -326,7 +331,7 @@ def test_single_reference_handler_pre_save_value(ref_product = product_list[0]values_input = AttrValuesInput(global_id=graphene.Node.to_global_id("Attribute", attribute.id),- reference=ref_product,+ reference_objects=[ref_product],)handler = ReferenceAttributeHandler(attribute, values_input)instance = productdiff --git a/saleor/graphql/attribute/utils/shared.py b/saleor/graphql/attribute/utils/shared.pyindex 7fada3d324..c8b5ecc101 100644--- a/saleor/graphql/attribute/utils/shared.py+++ b/saleor/graphql/attribute/utils/shared.py@@ -18,6 +18,13 @@ if TYPE_CHECKING:T_INSTANCE = product_models.Product | product_models.ProductVariant | page_models.PageT_ERROR_DICT = dict[tuple[str, str], list]+T_REFERENCE = (+ product_models.Product+ | product_models.ProductVariant+ | product_models.Category+ | product_models.Collection+ | page_models.Page+)@dataclass@@ -38,6 +45,7 @@ class AttrValuesInput:numeric: str | None = Nonereference: str | None = Nonereferences: list[str] | None = None+ reference_objects: list[T_REFERENCE] | None = Nonefile_url: str | None = Nonecontent_type: str | None = Nonerich_text: dict | None = Nonediff --git a/saleor/graphql/attribute/utils/type_handlers.py b/saleor/graphql/attribute/utils/type_handlers.pyindex 73c7902804..af2a347133 100644--- a/saleor/graphql/attribute/utils/type_handlers.py+++ b/saleor/graphql/attribute/utils/type_handlers.py@@ -425,7 +425,7 @@ class FileAttributeHandler(AttributeTypeHandler):class ReferenceAttributeHandler(AttributeTypeHandler):"""Handler for Reference and Single Reference attribute type."""- def get_references(self) -> Sequence[str | None]:+ def get_references(self) -> Sequence[str]:if self.attribute.input_type == AttributeInputType.SINGLE_REFERENCE:return [self.values_input.reference] if self.values_input.reference else []return self.values_input.references or []@@ -460,13 +460,10 @@ class ReferenceAttributeHandler(AttributeTypeHandler):self.attribute_identifier)return- if self.attribute.input_type == AttributeInputType.SINGLE_REFERENCE:- self.values_input.reference = ref_instances[0] if ref_instances else None- else:- self.values_input.references = ref_instances+ self.values_input.reference_objects = ref_instancesdef pre_save_value(self, instance: T_INSTANCE) -> list[tuple]:- references = self.get_references()+ references = self.values_input.reference_objectsentity_type = self.attribute.entity_typeif not references or not entity_type:return []@@ -479,7 +476,7 @@ class ReferenceAttributeHandler(AttributeTypeHandler):name = f"{ref.product.name}: {name}" # type: ignore[union-attr]# Reference values are unique per referenced entity- slug = slugify(unidecode(f"{instance.id}_{ref.id}")) # type: ignore[union-attr]+ slug = slugify(unidecode(f"{instance.id}_{ref.id}"))defaults = {"name": name}value_data = {"attribute": self.attribute,