From c21dfba291fdd2307a8f73a7aaff767a9004ad71 Mon Sep 17 00:00:00 2001 From: Nytelife26 Date: Sun, 4 Jul 2021 14:35:53 +0100 Subject: [PATCH] feat: add exceptions for existence_check --- proselint/tools.py | 6 ++++-- tests/test_existence_check.py | 11 +++++++++++ tests/test_tools.py | 2 +- 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/proselint/tools.py b/proselint/tools.py index 5c209594e..332999cf0 100644 --- a/proselint/tools.py +++ b/proselint/tools.py @@ -327,8 +327,8 @@ def preferred_forms_check(text, list, err, msg, ignore_case=True, offset=0, def existence_check(text, list, err, msg, ignore_case=True, str=False, max_errors=float("inf"), offset=0, require_padding=True, dotall=False, - excluded_topics=None, join=False): - """Build a checker that blacklists certain words.""" + excluded_topics=None, exceptions=[], join=False): + """Build a checker that prohibits certain words or phrases.""" flags = 0 msg = " ".join(msg.split()) @@ -358,6 +358,8 @@ def existence_check(text, list, err, msg, ignore_case=True, rx = "|".join(regex.format(w) for w in list) for m in re.finditer(rx, text, flags=flags): txt = m.group(0).strip() + if any([re.search(exception, txt) for exception in exceptions]): + continue errors.append(( m.start() + 1 + offset, m.end() + offset, diff --git a/tests/test_existence_check.py b/tests/test_existence_check.py index 612bb4d2a..fb0194a5d 100644 --- a/tests/test_existence_check.py +++ b/tests/test_existence_check.py @@ -50,3 +50,14 @@ def test_string_types(self): assert chk("abc is easy as 123", self.L, self.err, self.msg) != [] assert chk(u'abc is easy as 123', self.L, self.err, self.msg) != [] assert chk(u"abc is easy as 123", self.L, self.err, self.msg) != [] + + def test_exceptions(self): + """Test that existence_check does not report excluded phrases""" + regex = [r"\b(\w+)\b\s\1"] + no = ["should should"] + errs = chk("should should flag flag.", regex, "", "", + require_padding=False) + assert len(errs) == 2 + errs = chk("should should flag flag.", regex, "", "", exceptions=no, + require_padding=False) + assert len(errs) == 1 diff --git a/tests/test_tools.py b/tests/test_tools.py index 15bfab202..80401402a 100644 --- a/tests/test_tools.py +++ b/tests/test_tools.py @@ -4,7 +4,7 @@ from .check import Check -from proselint.tools import lint as lint +from proselint.tools import lint, existence_check class TestLint(Check):