From d420b6a9db87e133a34d0462af4c699b6debed96 Mon Sep 17 00:00:00 2001 From: Serhii Tereshchenko Date: Tue, 13 Aug 2024 17:24:16 +0300 Subject: [PATCH] fix: Fix django-cms compatibility Refs #748 Refs https://github.com/django-cms/django-cms/issues/7948 --- modeltranslation/_typing.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/modeltranslation/_typing.py b/modeltranslation/_typing.py index 30eb34af..9e67a53f 100644 --- a/modeltranslation/_typing.py +++ b/modeltranslation/_typing.py @@ -29,8 +29,13 @@ def monkeypatch() -> None: BaseModelAdmin, ] + 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( # type: ignore[attr-defined] - lambda cls, *args, **kwargs: cls - ) + class_.__class_getitem__ = classmethod(class_getitem) # type: ignore[attr-defined]