Skip to content

Commit

Permalink
fix unittests and lint
Browse files Browse the repository at this point in the history
  • Loading branch information
orgua committed Jan 5, 2024
1 parent 5a552be commit 4b09ec2
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 20 deletions.
12 changes: 5 additions & 7 deletions proselint/checks/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,9 @@ def existence_check( # noqa: PLR0913, PLR0917
return errors


def simple_existence_check(text: str, pattern: str, err: str, msg: str, offset: int = 0):
def simple_existence_check(
text: str, pattern: str, err: str, msg: str, offset: int = 0
):
"""Build a checker for single patters.
in comparison to existence_check:
- does not work on lists
Expand All @@ -276,12 +278,8 @@ def simple_existence_check(text: str, pattern: str, err: str, msg: str, offset:
"""

return [
(
inst.start() + offset,
inst.end() + offset,
err,
msg,
None) for inst in re.finditer(pattern, text)
(inst.start() + offset, inst.end() + offset, err, msg, None)
for inst in re.finditer(pattern, text)
]


Expand Down
2 changes: 1 addition & 1 deletion proselint/checks/lexical_illusions/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def check(text: str) -> list[ResultCheck]:
"""Check the text."""
err = "lexical_illusions.misc"
msg = "There's a lexical illusion here: a word is repeated."
regex = r"\b(\w+)(\b\s\1)+\b"
regex = r"\b(?<!\-)(\w+)(\b\s\1)+\b"
exceptions = [r"^had had$", r"^that that$"]

return existence_check(
Expand Down
24 changes: 16 additions & 8 deletions proselint/checks/punctuation_spacing/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,16 @@
- checks for acceptable spacing after ending punctuation
- (!?) which must be 1 or 2 spaces.
"""
from proselint.checks import ResultCheck, simple_existence_check
from __future__ import annotations

from proselint.checks import ResultCheck
from proselint.checks import simple_existence_check


def check(text: str) -> list[ResultCheck]:
"""Combine the above tests into one - these are super fast and should be load-balanced"""
"""Combine the above tests into one -
these are quick and should be load-balanced
"""

results: list[ResultCheck] = []
results.extend(find_end_punctuation_spacing(text))
Expand All @@ -28,18 +33,18 @@ def check(text: str) -> list[ResultCheck]:

def find_end_punctuation_spacing(text: str) -> list[ResultCheck]:
"""Check the text."""
err = "punctuation_spaces.punctuation_spaces"
err = "punctuation_spacing.misc.end_punctuation"
msg = "Unacceptable number of spaces behind ! or ? (must be 1 or 2)."

pattern = r'[?!]\s{2,} |[?!](?!\s|$)'
pattern = r"[?!]\s{2,} |[?!](?!\s|$)"

return simple_existence_check(text, pattern, err, msg)


# checks for acceptable behind ,";: which should be no more or less than 1
def find_general_spacing(text: str) -> list[ResultCheck]:
"""Check the text."""
err = "punctuation_spaces.punctuation_spaces"
err = "punctuation_spacing.misc.general"
msg = '"Unacceptable number of spaces behind ";: (must be 1)."'

pattern = r'[;:"]\s{1,} |[;:](;:\s|$])'
Expand All @@ -50,14 +55,17 @@ def find_general_spacing(text: str) -> list[ResultCheck]:
# comma is slightly more complex, consider the number 1,000
def find_comma_spacing(text: str) -> list[ResultCheck]:
"""Check the text."""
err = "punctuation_spaces.punctuation_spaces"
msg = ("Unacceptable number of spaces behind "
"(must be 1) except when used in numbers.")
err = "punctuation_spacing.misc.comma"
msg = (
"Unacceptable number of spaces behind "
"(must be 1) except when used in numbers."
)

pattern = r';:"]\s{2,} |[;:](;:\s|$])'

return simple_existence_check(text, pattern, err, msg)


# period is complex consider the cases of ellipsis, period between numbers
# as a decimal, period to signify subsections (A.23), period used in
# between abbreviations Washington D.C.
5 changes: 4 additions & 1 deletion proselint/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,10 @@ def output_errors(
out_fmt = Output[config_base.proselint_base["output_format"]]

if not isinstance(errors, list):
log.error("[OutputError] no list provided (guess: results of lint_path() need to be extracted first)")
log.error(
"[OutputError] no list provided "
"(guess: results of lint_path() need to be extracted first)"
)
return

if out_fmt == Output.json:
Expand Down
6 changes: 3 additions & 3 deletions tests/checks/test_punctuation_spacing.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
"""Tests for punctuation_spacing.misc check."""

from proselint.checks.punctuation_spacing.misc import check
from tests.conftest import assert_pass, assert_fail
from tests.conftest import assert_fail
from tests.conftest import assert_pass


def test_end_punctuation():
Expand All @@ -10,7 +11,7 @@ def test_end_punctuation():
assert_pass(check, "Smoke phrase with nothing flagged!")
assert_fail(check, "flagged! ")
assert_pass(check, "flagged? ")
assert_pass(check, "flagged? ")
assert_fail(check, "flagged? ")


def test_general_punctuation():
Expand All @@ -20,4 +21,3 @@ def test_general_punctuation():
assert_fail(check, "The quick brown fox jumps; over the lazy dog!")
assert_pass(check, "The quick brown fox jumps:over the lazy dog!")
assert_pass(check, "The quick brown fox jumps :over the lazy dog!")

0 comments on commit 4b09ec2

Please sign in to comment.