Skip to content

Commit a90e8c8

Browse files
authored
feat: check imperative mood by form rather than by vocabulary (#540)
1 parent 61bd994 commit a90e8c8

4 files changed

Lines changed: 220 additions & 20 deletions

File tree

‎.pre-commit-config.yaml‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ repos:
3333
hooks:
3434
- id: codespell
3535
# iTerm is a terminal emulator, named in the hyperlink support probe.
36-
args: [--ignore-words-list=iterm]
36+
# "sting" is a real verb, listed in NON_IMPERATIVE_LOOKALIKES.
37+
# Quoted: an unquoted comma splits the flow sequence into two args.
38+
args: ["--ignore-words-list=iterm,sting"]
3739
- repo: https://github.com/commit-check/commit-check
3840
rev: v2.11.0
3941
hooks:

‎commit_check/engine.py‎

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
git_merge_base,
2525
git_rev_parse_verify,
2626
)
27-
from commit_check.imperatives import IMPERATIVES
27+
from commit_check.imperatives import IMPERATIVES, NON_IMPERATIVE_LOOKALIKES
2828

2929

3030
class ValidationResult(IntEnum):
@@ -392,7 +392,20 @@ def _validate_subject(self, subject: str) -> ValidationResult:
392392

393393

394394
class SubjectImperativeValidator(SubjectValidator):
395-
"""Validates that subject uses imperative mood."""
395+
"""Validates that subject uses imperative mood.
396+
397+
Decides on the first word's form rather than its membership in a
398+
vocabulary: a past tense ("fixed"), a gerund ("adding") or a third person
399+
("fixes") is not imperative, and nothing else disqualifies. A list can
400+
only reject correct subjects wherever it falls short, and it always does:
401+
on 59k strictly imperative subjects from git.git it rejected 45%, against
402+
1% here. See #526.
403+
404+
The loosening is deliberate: a noun-led subject ("parser improvements")
405+
now passes, where the list rejected it by accident of vocabulary.
406+
"""
407+
408+
_INFLECTED = ("ed", "ing")
396409

397410
def _validate_subject(self, subject: str) -> ValidationResult:
398411
# Skip merge commits and fixup commits
@@ -408,11 +421,40 @@ def _validate_subject(self, subject: str) -> ValidationResult:
408421
return ValidationResult.PASS
409422

410423
first_word = match.group(1).lower()
411-
if first_word in IMPERATIVES:
412-
return ValidationResult.PASS
413424

414-
self._print_failure(subject)
415-
return ValidationResult.FAIL
425+
if self._is_inflected(first_word):
426+
self._print_failure(subject)
427+
return ValidationResult.FAIL
428+
429+
return ValidationResult.PASS
430+
431+
@classmethod
432+
def _is_inflected(cls, word: str) -> bool:
433+
"""Whether *word* carries past-tense, gerund or third-person marking."""
434+
if word in NON_IMPERATIVE_LOOKALIKES:
435+
return False
436+
if word.endswith(cls._INFLECTED):
437+
return True
438+
return cls._is_third_person(word)
439+
440+
@classmethod
441+
def _is_third_person(cls, word: str) -> bool:
442+
"""Whether *word* is a verb wearing the third-person singular -s.
443+
444+
Unlike -ed and -ing, a trailing -s is weak evidence on its own --
445+
plural nouns wear one too ("status report") -- so it asks for
446+
corroboration: the stem has to be a verb we already know. "tests" is
447+
genuinely both, and this reads it as the verb.
448+
"""
449+
# "address" and "process" end in -ss without being third person.
450+
if not word.endswith("s") or word.endswith("ss"):
451+
return False
452+
stems = {word[:-1]}
453+
if word.endswith("es"):
454+
stems.add(word[:-2])
455+
if word.endswith("ies"):
456+
stems.add(word[:-3] + "y")
457+
return any(stem in IMPERATIVES for stem in stems)
416458

417459

418460
class SubjectLengthValidator(SubjectValidator):

‎commit_check/imperatives.py‎

Lines changed: 55 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,15 @@
22
# https://github.com/crate-ci/imperative/blob/master/assets/imperatives.txt
33
# and extended since.
44
#
5-
# Some of these are more commonly encountered as nouns, but leaving them out
6-
# rejects a subject that is written correctly, which is the worse failure: the
7-
# contributor has to reword something that was never wrong, and the only way
8-
# they learn which words are acceptable is by trial and error.
5+
# This is NOT an allow-list, and a subject is never rejected for being absent
6+
# from it. CC003 decides on the word's form (see SubjectImperativeValidator),
7+
# and the only thing this set is still consulted for is the stem test behind
8+
# the third-person -s rule: "fixes" is a verb because "fix" is in here, while
9+
# "status" is not, because dropping its -s leaves nothing this set contains.
910
#
10-
# For the same reason both spellings of every -ize/-ise verb are listed. A
11-
# project writing British English is not making a mistake.
12-
#
13-
# Additions are welcome and cheap. The list can only ever approximate "is this
14-
# an English imperative verb", so treat a rejected-but-correct subject as a bug
15-
# in this file rather than as something the author should work around.
11+
# So a missing verb costs a missed violation, never a false rejection -- there
12+
# is nothing to keep up with, and no need to send a patch adding the verb you
13+
# just used. Additions still help the -s rule catch more, and cost nothing.
1614

1715
IMPERATIVES = {
1816
"abort",
@@ -44,7 +42,6 @@
4442
"authenticate",
4543
"authorise",
4644
"authorize",
47-
"auto",
4845
"automate",
4946
"avoid",
5047
"await",
@@ -304,7 +301,6 @@
304301
"parameterise",
305302
"parameterize",
306303
"parse",
307-
"partial",
308304
"pass",
309305
"pause",
310306
"perform",
@@ -441,7 +437,6 @@
441437
"serve",
442438
"set",
443439
"settle",
444-
"setup",
445440
"shard",
446441
"shorten",
447442
"show",
@@ -545,3 +540,50 @@
545540
"yield",
546541
"zip",
547542
}
543+
544+
545+
# Imperative verbs that end in -ed or -ing without being a past tense or a
546+
# gerund, and so would be misread by the morphology rule in
547+
# SubjectImperativeValidator. Short enough to enumerate, which is what lets
548+
# that rule work on shape instead of on a vocabulary of every English verb.
549+
#
550+
# Both groups are meant to be the whole family rather than a sample of it --
551+
# a half-enumerated closed set would put back exactly the "add my word"
552+
# treadmill this check was rebuilt to escape. Err towards including a word:
553+
# an entry that never comes up costs nothing, a missing one rejects someone
554+
# who wrote correct English.
555+
NON_IMPERATIVE_LOOKALIKES = {
556+
# -ed, but not a past tense
557+
"bleed",
558+
"breed",
559+
"embed",
560+
"exceed",
561+
"feed",
562+
"heed",
563+
"need",
564+
"proceed",
565+
"seed",
566+
"shed",
567+
"shred",
568+
"speed",
569+
"spread",
570+
"succeed",
571+
"wed",
572+
"weed",
573+
# -ing, but not a gerund
574+
"bring",
575+
"cling",
576+
"ding",
577+
"fling",
578+
"ping",
579+
"ring",
580+
"sing",
581+
"sling",
582+
"spring",
583+
"sting",
584+
"string",
585+
"swing",
586+
"wing",
587+
"wring",
588+
"zing",
589+
}

‎tests/engine_test.py‎

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2560,3 +2560,117 @@ def test_commit_type_rule_still_fails_for_other_authors(self):
25602560
patch("commit_check.engine.get_commit_info", return_value="Ada Lovelace"),
25612561
):
25622562
assert validator.validate(context) == ValidationResult.FAIL
2563+
2564+
2565+
class TestImperativeMorphology:
2566+
"""The rule decides on form, not on vocabulary. See #526.
2567+
2568+
A word list can only approximate "is this an English imperative", and the
2569+
cost of every gap falls on someone who wrote the subject correctly. These
2570+
pin the three things that replace it: inflected first words are rejected,
2571+
everything else is not, and the few words whose spelling trips the test
2572+
are named rather than guessed at.
2573+
"""
2574+
2575+
def _verdict(self, subject):
2576+
validator = SubjectImperativeValidator(
2577+
ValidationRule(check="subject_imperative")
2578+
)
2579+
return validator.validate(ValidationContext(stdin_text=subject))
2580+
2581+
@pytest.mark.benchmark
2582+
@pytest.mark.parametrize(
2583+
"subject",
2584+
[
2585+
"fix: fixed the parser", # past tense
2586+
"fix: updated the docs",
2587+
"fix: removed the flag",
2588+
"feat: adding a retry", # gerund
2589+
"feat: implementing the cache",
2590+
"fix: fixes the parser", # third person
2591+
"fix: removes the flag",
2592+
"docs: documents the API",
2593+
"fix: tries the fallback", # -ies, so the stem is "try"
2594+
"fix: applies the patch",
2595+
],
2596+
)
2597+
def test_inflected_first_words_are_rejected(self, subject):
2598+
"""The failure the rule exists for, and now the only one it reports."""
2599+
assert self._verdict(subject) == ValidationResult.FAIL
2600+
2601+
@pytest.mark.benchmark
2602+
@pytest.mark.parametrize(
2603+
"subject",
2604+
[
2605+
# Correct imperatives that no list contained, or ever would.
2606+
"feat: reword the report format",
2607+
"refactor: dedupe the helper",
2608+
"chore: decommission the legacy path",
2609+
"fix: loosen the guard",
2610+
"fix: backfill an absent value",
2611+
# Adverb-led subjects, which a list of *verbs* cannot represent
2612+
# without becoming a list of not-verbs.
2613+
"feat: optionally skip the hook",
2614+
"fix: explicitly close the handle",
2615+
# British spelling, which is not a mistake.
2616+
"refactor: normalise the path separators",
2617+
],
2618+
)
2619+
def test_uninflected_first_words_pass_without_being_listed(self, subject):
2620+
assert self._verdict(subject) == ValidationResult.PASS
2621+
2622+
@pytest.mark.benchmark
2623+
@pytest.mark.parametrize(
2624+
"subject",
2625+
[
2626+
"fix: embed the token", # -ed, not a past tense
2627+
"fix: need a newer pip",
2628+
"fix: proceed without the cache",
2629+
"chore: spread the load",
2630+
"feat: bring back the flag", # -ing, not a gerund
2631+
"fix: string the parts together",
2632+
"fix: ping the endpoint",
2633+
"chore: weed out the dead code",
2634+
"fix: heed the configured timeout",
2635+
"feat: sling the payload over the wire",
2636+
],
2637+
)
2638+
def test_lookalikes_are_not_mistaken_for_inflection(self, subject):
2639+
"""The words a suffix rule gets wrong, enumerated rather than guessed."""
2640+
assert self._verdict(subject) == ValidationResult.PASS
2641+
2642+
@pytest.mark.benchmark
2643+
@pytest.mark.parametrize(
2644+
"subject",
2645+
[
2646+
"fix: address the warning", # -ss, never third person
2647+
"fix: process the queue",
2648+
"fix: guess the encoding",
2649+
"fix: focus the search", # -s, but the stem is not a verb
2650+
"fix: status report is empty", # noun-led
2651+
"chore: deps bump",
2652+
# Stems that are not verbs at all, so their plurals are nouns.
2653+
"fix: partials are rendered twice",
2654+
"chore: setups differ between CI and local",
2655+
"fix: always quote the path", # adverb-led
2656+
"fix: sometimes the cache is stale",
2657+
],
2658+
)
2659+
def test_single_s_words_are_not_assumed_third_person(self, subject):
2660+
"""A trailing -s only counts when the stem is a verb we know."""
2661+
assert self._verdict(subject) == ValidationResult.PASS
2662+
2663+
@pytest.mark.benchmark
2664+
def test_a_noun_led_subject_now_passes(self):
2665+
"""Documents the deliberate loosening, so a change to it is a choice.
2666+
2667+
The old list rejected this by accident of vocabulary, not because it
2668+
detected the mood. Naming it here means anyone tightening it later
2669+
does so on purpose.
2670+
"""
2671+
assert self._verdict("fix: parser improvements") == ValidationResult.PASS
2672+
2673+
@pytest.mark.benchmark
2674+
def test_merge_and_fixup_subjects_still_bypass_the_rule(self):
2675+
assert self._verdict("Merge branch 'main' into topic") == ValidationResult.PASS
2676+
assert self._verdict("fixup! fixed the parser") == ValidationResult.PASS

0 commit comments

Comments
 (0)