From b2e35bc5731ecf28544d49a90051cbe11dfd4b62 Mon Sep 17 00:00:00 2001 From: Dave Halter Date: Thu, 6 Jul 2017 17:39:23 +0200 Subject: [PATCH] Add better line length checks (E501). --- parso/python/normalizer.py | 33 +++++--- test/normalizer_issue_files/E50.py | 126 +++++++++++++++++++++++++++++ 2 files changed, 150 insertions(+), 9 deletions(-) create mode 100644 test/normalizer_issue_files/E50.py diff --git a/parso/python/normalizer.py b/parso/python/normalizer.py index 8f8cec6..4659ff0 100644 --- a/parso/python/normalizer.py +++ b/parso/python/normalizer.py @@ -455,15 +455,7 @@ class PEP8Normalizer(Normalizer): self._check_spacing(leaf, spacing) self._analyse_non_prefix(leaf) - last_column = leaf.end_pos[1] - if last_column > self._config.max_characters: - self.add_issue( - 501, - 'Line too long (%s > %s characters)' % - (last_column, self._config.max_characters), - leaf - ) - + self._check_line_length(leaf, spacing) # ------------------------------- # Finalizing. Updating the state. # ------------------------------- @@ -504,6 +496,29 @@ class PEP8Normalizer(Normalizer): self._previous_spacing = spacing return value + def _check_line_length(self, leaf, spacing): + if leaf.type == 'backslash': + last_column = leaf.start_pos[1] + 1 + else: + last_column = leaf.end_pos[1] + if last_column > self._config.max_characters \ + and spacing.start_pos[1] <= self._config.max_characters : + # Special case for long URLs in multi-line docstrings or comments, + # but still report the error when the 72 first chars are whitespaces. + report = True + if leaf.type == 'comment': + splitted = leaf.value[1:].split() + if len(splitted) == 1 \ + and (leaf.end_pos[1] - len(splitted[0])) < 72: + report = False + if report: + self.add_issue( + 501, + 'Line too long (%s > %s characters)' % + (last_column, self._config.max_characters), + leaf + ) + def _check_spacing(self, leaf, spacing): def add_if_spaces(*args): if spaces: diff --git a/test/normalizer_issue_files/E50.py b/test/normalizer_issue_files/E50.py new file mode 100644 index 0000000..34a6ef4 --- /dev/null +++ b/test/normalizer_issue_files/E50.py @@ -0,0 +1,126 @@ +#: E501:4 +a = '12345678901234567890123456789012345678901234567890123456789012345678901234567890' +#: E501:80 +a = '1234567890123456789012345678901234567890123456789012345678901234567890' or \ + 6 +#: E501+1:80 +a = 7 or \ + '1234567890123456789012345678901234567890123456789012345678901234567890' or \ + 6 +#: E501+1:80 E501+2:80 +a = 7 or \ + '1234567890123456789012345678901234567890123456789012345678901234567890' or \ + '1234567890123456789012345678901234567890123456789012345678901234567890' or \ + 6 +#: E501:78 +a = '1234567890123456789012345678901234567890123456789012345678901234567890' # \ +#: E502:78 +a = ('123456789012345678901234567890123456789012345678901234567890123456789' \ + '01234567890') +#: E502+1:11 +a = ('AAA \ + BBB' \ + 'CCC') +#: E502:38 +if (foo is None and bar is "e000" and \ + blah == 'yeah'): + blah = 'yeahnah' +# +# Okay +a = ('AAA' + 'BBB') + +a = ('AAA \ + BBB' + 'CCC') + +a = 'AAA' \ + 'BBB' \ + 'CCC' + +a = ('AAA\ +BBBBBBBBB\ +CCCCCCCCC\ +DDDDDDDDD') +# +# Okay +if aaa: + pass +elif bbb or \ + ccc: + pass + +ddd = \ + ccc + +('\ + ' + ' \ +') +(''' + ''' + ' \ +') +#: E501:67 E225:21 E225:22 +very_long_identifiers=and_terrible_whitespace_habits(are_no_excuse+for_long_lines) +# +#: E501 +'''multiline string +with a long long long long long long long long long long long long long long long long line +''' +#: E501 +'''same thing, but this time without a terminal newline in the string +long long long long long long long long long long long long long long long long line''' +# +# issue 224 (unavoidable long lines in docstrings) +# Okay +""" +I'm some great documentation. Because I'm some great documentation, I'm +going to give you a reference to some valuable information about some API +that I'm calling: + + http://msdn.microsoft.com/en-us/library/windows/desktop/aa363858(v=vs.85).aspx +""" +#: E501 +""" +longnospaceslongnospaceslongnospaceslongnospaceslongnospaceslongnospaceslongnospaceslongnospaces""" + + +# Regression test for #622 +def foo(): + """Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis pulvinar vitae + """ + + +# Okay +""" +This + almost_empty_line +""" + +""" +This + almost_empty_line +""" +# A basic comment +#: E501 +# with a long long long long long long long long long long long long long long long long line + +# +# Okay +# I'm some great comment. Because I'm so great, I'm going to give you a +# reference to some valuable information about some API that I'm calling: +# +# http://msdn.microsoft.com/en-us/library/windows/desktop/aa363858(v=vs.85).aspx + +x = 3 + +# longnospaceslongnospaceslongnospaceslongnospaceslongnospaceslongnospaceslongnospaceslongnospaces + +# +# Okay +# This +# almost_empty_line + +# +#: E501+1 +# This +# almost_empty_line