Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restore backward compatibility for Attribute creation with invalid slugs #639

Merged
merged 1 commit into from
Sep 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions eav/models/attribute.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from __future__ import annotations

import warnings
from typing import TYPE_CHECKING, Optional

from django.contrib.contenttypes.models import ContentType
Expand Down Expand Up @@ -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):
Expand Down
11 changes: 9 additions & 2 deletions tests/test_attributes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Loading