Skip to content

Commit 5e13aa0

Browse files
committed
Handle not alphadigital chars in verbose name, fixes #32
1 parent 07eb62d commit 5e13aa0

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

src/extra_checks/checks/model_field_checks.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,14 @@ def apply(
8787
class CheckFieldVerboseNameGettextCase(GetTextMixin, CheckModelField):
8888
Id = CheckId.X052
8989

90+
@classmethod
91+
def is_invalid(cls, value: object) -> bool:
92+
return bool(
93+
value
94+
and isinstance(value, str)
95+
and any(w != w.lower() and w != w.upper() for w in value.split(" "))
96+
)
97+
9098
def apply(
9199
self, field: models.fields.Field, ast: FieldASTProtocol, **kwargs: Any
92100
) -> Iterator[django.core.checks.CheckMessage]:
@@ -96,13 +104,7 @@ def apply(
96104
and verbose_name.callable_func_name == self.gettext_func
97105
):
98106
value = verbose_name.get_call_first_args()
99-
if (
100-
value
101-
and isinstance(value, str)
102-
and not all(
103-
w.islower() or w.isupper() or w.isdigit() for w in value.split(" ")
104-
)
105-
):
107+
if self.is_invalid(value):
106108
yield self.message(
107109
"Words in verbose name must be all upper case or all lower case.",
108110
hint=f'Change verbose name to "{value.lower()}".',

tests/test_model_field_checks.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,17 @@ def test_check_field_verbose_name_gettext_case(test_case):
5959
}
6060

6161

62+
def test_check_field_verbose_name_gettext_check_case():
63+
is_invalid = model_field_checks.CheckFieldVerboseNameGettextCase.is_invalid
64+
assert is_invalid("Abc Def")
65+
assert not is_invalid("abc def")
66+
assert not is_invalid("ABC def")
67+
assert not is_invalid("ABC DEF")
68+
assert not is_invalid("abc123 ABC123")
69+
assert is_invalid("Abc123 AbC123")
70+
assert not is_invalid("abc / def")
71+
72+
6273
def test_check_field_null_boolean(test_case):
6374
messages = (
6475
test_case.models(models.ModelFieldNullFalse)

0 commit comments

Comments
 (0)