Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add exceptions for existence_check #1182

Merged
merged 1 commit into from
Jul 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions proselint/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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,
Expand Down
11 changes: 11 additions & 0 deletions tests/test_existence_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion tests/test_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down