From 6ef8cb15d045b659d0c21b15cb2defc2d564445b Mon Sep 17 00:00:00 2001 From: Mike Date: Sun, 1 Sep 2024 21:24:18 -0700 Subject: [PATCH] fix(attribute): restore backward compatibility for invalid slugs Replace ValidationError with a warning when creating Attributes with invalid slugs. This change allows existing code to continue functioning while alerting users to potential issues. --- eav/models/attribute.py | 12 +++++------- tests/test_attributes.py | 11 +++++++++-- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/eav/models/attribute.py b/eav/models/attribute.py index f44311f5..1f52e7d3 100644 --- a/eav/models/attribute.py +++ b/eav/models/attribute.py @@ -2,6 +2,7 @@ from __future__ import annotations +import warnings from typing import TYPE_CHECKING, Optional from django.contrib.contenttypes.models import ContentType @@ -311,13 +312,10 @@ def clean_fields(self, exclude=None): super().clean_fields(exclude=exclude) if not self.slug.isidentifier(): - raise ValidationError( - { - "slug": _( - "Slug must be a valid Python identifier (no spaces, " - + "special characters, or leading digits).", - ), - }, + warnings.warn( + f"Slug '{self.slug}' is not a valid Python identifier. " + + "Consider updating it.", + stacklevel=3, ) def get_choices(self): diff --git a/tests/test_attributes.py b/tests/test_attributes.py index 56b6d167..8ad4999c 100644 --- a/tests/test_attributes.py +++ b/tests/test_attributes.py @@ -167,8 +167,15 @@ def test_large_name_input(self, name_value) -> None: @pytest.mark.django_db -def test_attribute_create_with_invalid_slug(): - with pytest.raises(ValidationError): +def test_attribute_create_with_invalid_slug() -> None: + """ + Test that creating an Attribute with an invalid slug raises a UserWarning. + + This test ensures that when an Attribute is created with a slug that is not + a valid Python identifier, a UserWarning is raised. The warning should + indicate that the slug is invalid and suggest updating it. + """ + with pytest.warns(UserWarning): Attribute.objects.create( name="Test Attribute", slug="123-invalid",