Skip to content

Commit

Permalink
Make the error message show what kind of string is bad. (#103)
Browse files Browse the repository at this point in the history
  • Loading branch information
Julian authored Aug 23, 2021
1 parent 4612549 commit 07f161c
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 78 deletions.
12 changes: 9 additions & 3 deletions flake8_quotes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@ class QuoteChecker(object):
"'": {
'good_single': "'",
'bad_single': '"',
'single_error_message': 'Double quotes found but single quotes preferred',
},
# When user wants only double quotes
'"': {
'good_single': '"',
'bad_single': "'",
'single_error_message': 'Single quotes found but double quotes preferred',
},
}
# Provide aliases for Windows CLI support
Expand All @@ -44,11 +46,13 @@ class QuoteChecker(object):
'good_multiline': "'''",
'good_multiline_ending': '\'"""',
'bad_multiline': '"""',
'multiline_error_message': 'Double quote multiline found but single quotes preferred',
},
'"': {
'good_multiline': '"""',
'good_multiline_ending': '"\'\'\'',
'bad_multiline': "'''",
'multiline_error_message': 'Single quote multiline found but double quotes preferred',
},
}
# Provide Windows CLI and multi-quote aliases
Expand All @@ -61,10 +65,12 @@ class QuoteChecker(object):
"'": {
'good_docstring': "'''",
'bad_docstring': '"""',
'docstring_error_message': 'Double quote docstring found but single quotes preferred',
},
'"': {
'good_docstring': '"""',
'bad_docstring': "'''",
'docstring_error_message': 'Single quote docstring found but double quotes preferred',
},
}
# Provide Windows CLI and docstring-quote aliases
Expand Down Expand Up @@ -221,7 +227,7 @@ def get_quotes_errors(self, file_contents):
continue

yield {
'message': 'Q002 Remove bad quotes from docstring',
'message': 'Q002 ' + self.config['docstring_error_message'],
'line': start_row,
'col': start_col,
}
Expand All @@ -242,7 +248,7 @@ def get_quotes_errors(self, file_contents):

# Output our error
yield {
'message': 'Q001 Remove bad quotes from multiline string',
'message': 'Q001 ' + self.config['multiline_error_message'],
'line': start_row,
'col': start_col,
}
Expand Down Expand Up @@ -276,7 +282,7 @@ def get_quotes_errors(self, file_contents):
# If not preferred type, only allow use to avoid escapes.
if not self.config['good_single'] in string_contents:
yield {
'message': 'Q000 Remove bad quotes',
'message': 'Q000 ' + self.config['single_error_message'],
'line': start_row,
'col': start_col,
}
Expand Down
50 changes: 28 additions & 22 deletions test/test_checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,15 @@ def test_stdin(self):
stdout_lines = stdout.splitlines()
self.assertEqual(stderr, b'')
self.assertEqual(len(stdout_lines), 3)
self.assertRegexpMatches(stdout_lines[0], b'stdin:1:(24|25): Q000 Remove bad quotes')
self.assertRegexpMatches(stdout_lines[1], b'stdin:2:(24|25): Q000 Remove bad quotes')
self.assertRegexpMatches(stdout_lines[2], b'stdin:3:(24|25): Q000 Remove bad quotes')
self.assertRegexpMatches(
stdout_lines[0],
b'stdin:1:(24|25): Q000 Double quotes found but single quotes preferred')
self.assertRegexpMatches(
stdout_lines[1],
b'stdin:2:(24|25): Q000 Double quotes found but single quotes preferred')
self.assertRegexpMatches(
stdout_lines[2],
b'stdin:3:(24|25): Q000 Double quotes found but single quotes preferred')


class DoublesTestChecks(TestCase):
Expand All @@ -37,15 +43,15 @@ class DoublesOptions():
def test_multiline_string(self):
doubles_checker = QuoteChecker(None, filename=get_absolute_path('data/doubles_multiline_string.py'))
self.assertEqual(list(doubles_checker.get_quotes_errors(doubles_checker.get_file_contents())), [
{'col': 4, 'line': 1, 'message': 'Q001 Remove bad quotes from multiline string'},
{'col': 4, 'line': 1, 'message': 'Q001 Double quote multiline found but single quotes preferred'},
])

def test_multiline_string_using_lines(self):
with open(get_absolute_path('data/doubles_multiline_string.py')) as f:
lines = f.readlines()
doubles_checker = QuoteChecker(None, lines=lines)
self.assertEqual(list(doubles_checker.get_quotes_errors(doubles_checker.get_file_contents())), [
{'col': 4, 'line': 1, 'message': 'Q001 Remove bad quotes from multiline string'},
{'col': 4, 'line': 1, 'message': 'Q001 Double quote multiline found but single quotes preferred'},
])

def test_wrapped(self):
Expand All @@ -55,9 +61,9 @@ def test_wrapped(self):
def test_doubles(self):
doubles_checker = QuoteChecker(None, filename=get_absolute_path('data/doubles.py'))
self.assertEqual(list(doubles_checker.get_quotes_errors(doubles_checker.get_file_contents())), [
{'col': 24, 'line': 1, 'message': 'Q000 Remove bad quotes'},
{'col': 24, 'line': 2, 'message': 'Q000 Remove bad quotes'},
{'col': 24, 'line': 3, 'message': 'Q000 Remove bad quotes'},
{'col': 24, 'line': 1, 'message': 'Q000 Double quotes found but single quotes preferred'},
{'col': 24, 'line': 2, 'message': 'Q000 Double quotes found but single quotes preferred'},
{'col': 24, 'line': 3, 'message': 'Q000 Double quotes found but single quotes preferred'},
])

def test_noqa_doubles(self):
Expand Down Expand Up @@ -93,9 +99,9 @@ def test_doubles(self):

doubles_checker = QuoteChecker(None, filename=get_absolute_path('data/doubles.py'))
self.assertEqual(list(doubles_checker.get_quotes_errors(doubles_checker.get_file_contents())), [
{'col': 24, 'line': 1, 'message': 'Q000 Remove bad quotes'},
{'col': 24, 'line': 2, 'message': 'Q000 Remove bad quotes'},
{'col': 24, 'line': 3, 'message': 'Q000 Remove bad quotes'},
{'col': 24, 'line': 1, 'message': 'Q000 Double quotes found but single quotes preferred'},
{'col': 24, 'line': 2, 'message': 'Q000 Double quotes found but single quotes preferred'},
{'col': 24, 'line': 3, 'message': 'Q000 Double quotes found but single quotes preferred'},
])


Expand All @@ -109,7 +115,7 @@ class SinglesOptions():
def test_multiline_string(self):
singles_checker = QuoteChecker(None, filename=get_absolute_path('data/singles_multiline_string.py'))
self.assertEqual(list(singles_checker.get_quotes_errors(singles_checker.get_file_contents())), [
{'col': 4, 'line': 1, 'message': 'Q001 Remove bad quotes from multiline string'},
{'col': 4, 'line': 1, 'message': 'Q001 Single quote multiline found but double quotes preferred'},
])

def test_wrapped(self):
Expand All @@ -119,9 +125,9 @@ def test_wrapped(self):
def test_singles(self):
singles_checker = QuoteChecker(None, filename=get_absolute_path('data/singles.py'))
self.assertEqual(list(singles_checker.get_quotes_errors(singles_checker.get_file_contents())), [
{'col': 24, 'line': 1, 'message': 'Q000 Remove bad quotes'},
{'col': 24, 'line': 2, 'message': 'Q000 Remove bad quotes'},
{'col': 24, 'line': 3, 'message': 'Q000 Remove bad quotes'},
{'col': 24, 'line': 1, 'message': 'Q000 Single quotes found but double quotes preferred'},
{'col': 24, 'line': 2, 'message': 'Q000 Single quotes found but double quotes preferred'},
{'col': 24, 'line': 3, 'message': 'Q000 Single quotes found but double quotes preferred'},
])

def test_noqa_singles(self):
Expand Down Expand Up @@ -157,9 +163,9 @@ def test_singles(self):

singles_checker = QuoteChecker(None, filename=get_absolute_path('data/singles.py'))
self.assertEqual(list(singles_checker.get_quotes_errors(singles_checker.get_file_contents())), [
{'col': 24, 'line': 1, 'message': 'Q000 Remove bad quotes'},
{'col': 24, 'line': 2, 'message': 'Q000 Remove bad quotes'},
{'col': 24, 'line': 3, 'message': 'Q000 Remove bad quotes'},
{'col': 24, 'line': 1, 'message': 'Q000 Single quotes found but double quotes preferred'},
{'col': 24, 'line': 2, 'message': 'Q000 Single quotes found but double quotes preferred'},
{'col': 24, 'line': 3, 'message': 'Q000 Single quotes found but double quotes preferred'},
])


Expand All @@ -172,7 +178,7 @@ class Options():

multiline_checker = QuoteChecker(None, filename=get_absolute_path('data/multiline_string.py'))
self.assertEqual(list(multiline_checker.get_quotes_errors(multiline_checker.get_file_contents())), [
{'col': 4, 'line': 10, 'message': 'Q001 Remove bad quotes from multiline string'},
{'col': 4, 'line': 10, 'message': 'Q001 Single quote multiline found but double quotes preferred'},
])

def test_singles_alias(self):
Expand All @@ -183,7 +189,7 @@ class Options():

multiline_checker = QuoteChecker(None, filename=get_absolute_path('data/multiline_string.py'))
self.assertEqual(list(multiline_checker.get_quotes_errors(multiline_checker.get_file_contents())), [
{'col': 4, 'line': 10, 'message': 'Q001 Remove bad quotes from multiline string'},
{'col': 4, 'line': 10, 'message': 'Q001 Single quote multiline found but double quotes preferred'},
])

def test_doubles(self):
Expand All @@ -194,7 +200,7 @@ class Options():

multiline_checker = QuoteChecker(None, filename=get_absolute_path('data/multiline_string.py'))
self.assertEqual(list(multiline_checker.get_quotes_errors(multiline_checker.get_file_contents())), [
{'col': 4, 'line': 1, 'message': 'Q001 Remove bad quotes from multiline string'},
{'col': 4, 'line': 1, 'message': 'Q001 Double quote multiline found but single quotes preferred'},
])

def test_doubles_alias(self):
Expand All @@ -205,7 +211,7 @@ class Options():

multiline_checker = QuoteChecker(None, filename=get_absolute_path('data/multiline_string.py'))
self.assertEqual(list(multiline_checker.get_quotes_errors(multiline_checker.get_file_contents())), [
{'col': 4, 'line': 1, 'message': 'Q001 Remove bad quotes from multiline string'},
{'col': 4, 'line': 1, 'message': 'Q001 Double quote multiline found but single quotes preferred'},
])


Expand Down
Loading

0 comments on commit 07f161c

Please sign in to comment.