Skip to content

Commit

Permalink
Handle edge case
Browse files Browse the repository at this point in the history
The regex up to now could not handle a quoted string inside another, i.e. ('"lala"').

If that string had some % symbol inside ('"%lala"') it already though that the string
was finished and thus the % symbol was the one triggering a %-format string.

Now we check for a single quote or double quoted string.
  • Loading branch information
gforcada committed Oct 27, 2016
1 parent 07823a8 commit 59dffdb
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
17 changes: 15 additions & 2 deletions flake8_pep3101.py
Expand Up @@ -6,7 +6,19 @@
except ImportError:
import pep8 as pycodestyle

OLD_RE = re.compile(r'^(?:[^\'"]*[\'"][^\'"]*[\'"])*\s*%|^\s*%')

OLD_RE = re.compile(
r'^' # beginning of a string
r'(' # group
r'[^\'"]*' # any character(s) that is/are no single/double quote
r'(\'[^\']*\')*' # any single quoted string
r'("[^"]*")' # any double quotted string
r')*' # end of group, either none or multiple repetitions
r'\s*' # any space
r'%' # percent character
r'|' # either everything up to now or what comes next
r'^\s*%' # beginning of a string, spaces and a percent character
)

FORMATTERS = (
's',
Expand Down Expand Up @@ -37,7 +49,8 @@ class Flake8Pep3101(object):
format string and the parameters to be inserted.
I never found a reference for that but now sentry relies on it.
So this check, looks for:
1. two string delimiter characters following any number of whitespace and %
1. two string delimiter characters following any number of whitespace, a %
an and extra space
2. Beginning of line, any number of whitespace and %
This will ignore log messages that do not do inline message formatting,
Expand Down
22 changes: 22 additions & 0 deletions run_tests.py
Expand Up @@ -252,6 +252,14 @@ def test_multiple_strings(self):
ret = list(checker.run())
self.assertEqual(len(ret), 0)

def test_multiple_single_quotes_strings(self):
file_path = self._given_a_file_in_test_dir('\n'.join([
"'''''''1' if '%' else '2'"
]))
checker = Flake8Pep3101(None, file_path)
ret = list(checker.run())
self.assertEqual(len(ret), 0)

def test_multiple_strings_with_old_formatting(self):
"""Check that multiple quoting is handled properly.
Expand All @@ -267,6 +275,20 @@ def test_multiple_strings_with_old_formatting(self):
self.assertEqual(ret[0][1], 14)
self.assertEqual(ret[0][2], 'S001 found % formatter')

def test_percent_on_string(self):
"""Check that multiple quoting is handled properly.
In this case no string substitution is happening.
Found in plone.app.drafts.tests
"""
file_path = self._given_a_file_in_test_dir('\n'.join([
'a = \'"%2B%2Badd%2B%2BMyDocument"\''
]))
checker = Flake8Pep3101(None, file_path)
ret = list(checker.run())
self.assertEqual(len(ret), 0)


if __name__ == '__main__':
unittest.main()

0 comments on commit 59dffdb

Please sign in to comment.