Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
last-partizan committed Aug 13, 2024
1 parent 26e2f23 commit 808bd66
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
7 changes: 4 additions & 3 deletions modeltranslation/_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,13 @@ def monkeypatch() -> None:
]

def class_getitem(cls: type, key: str | type | TypeVar):

if isinstance(key, str) and hasattr(cls, key):
# Fix django-cms compatibility:
# https://github.com/django-cms/django-cms/issues/7948
raise KeyError(f"Key '{key}' found as attribute, use getattr to access it.")
return cls

for class_ in classes:
if not hasattr(class_, "__class_getitem__"):
class_.__class_getitem__ = classmethod(class_getitem) # type: ignore[attr-defined]
for cls in classes:
if not hasattr(cls, "__class_getitem__"):
cls.__class_getitem__ = classmethod(class_getitem) # type: ignore[attr-defined]
8 changes: 8 additions & 0 deletions modeltranslation/tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -2911,6 +2911,14 @@ class ProxyTestModelAdmin(admin.TranslationAdmin):
assert tuple(ma.get_form(request).base_fields.keys()) == tuple(fields)
assert tuple(ma.get_form(request, self.test_obj).base_fields.keys()) == tuple(fields)

def test_class_attribute_access_raises_type_error(self):

class TestModelAdmin(admin.TranslationAdmin[models.TestModel]):
allow_children = True

with pytest.raises(KeyError):
TestModelAdmin["allow_children"] # type: ignore


class ThirdPartyAppIntegrationTest(ModeltranslationTestBase):
"""
Expand Down

0 comments on commit 808bd66

Please sign in to comment.