Skip to content

Commit

Permalink
Properly handle python single line functions in relation to docstrings (
Browse files Browse the repository at this point in the history
  • Loading branch information
facelessuser committed May 21, 2019
1 parent ea3b5d4 commit 415e497
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 3 deletions.
4 changes: 4 additions & 0 deletions docs/src/markdown/about/changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 2.3.1

- **FIX**: Properly handle docstring content and detection in files that have single line functions.

## 2.3.0

- **NEW**: Support new `wcmatch` glob feature flags and upgrade to `wcmatch` 4.0.
Expand Down
2 changes: 1 addition & 1 deletion pyspelling/__meta__.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,5 +186,5 @@ def parse_version(ver, pre=False):
return Version(major, minor, micro, release, pre, post, dev)


__version_info__ = Version(2, 3, 0, "final")
__version_info__ = Version(2, 3, 1, "final")
__version__ = __version_info__._get_canonical()
21 changes: 19 additions & 2 deletions pyspelling/filters/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,17 @@ def process_strings(self, string, docstrings=False):

return textwrap.dedent(RE_NON_PRINTABLE.sub('\n', string) if is_bytes else string), is_bytes

def catch_same_level(self, stack, indent):
"""Test if in current class or function level."""

same_level = False
last_indent = stack[-1][1]
if last_indent == indent:
if len(stack) > 1 and indent <= stack[-1][1]:
stack.pop()
same_level = True
return same_level

def _filter(self, text, context, encoding):
"""Retrieve the Python docstrings."""

Expand Down Expand Up @@ -275,6 +286,7 @@ def _filter(self, text, context, encoding):
possible_fmt_str = None
if value in ('def', 'class'):
name = value
self.catch_same_level(stack, len(indent))
elif name:
parent = stack[-1][2]
prefix = ''
Expand Down Expand Up @@ -310,9 +322,14 @@ def _filter(self, text, context, encoding):
# Capture docstrings.
# If we captured an `INDENT` or `NEWLINE` previously we probably have a docstring.
# `NL` means end of line, but not the end of the Python code line (line continuation).

same_level = self.catch_same_level(stack, len(indent))
if (
(prev_token_type in PREV_DOC_TOKENS) or
(possible_fmt_str and possible_fmt_str[0] in PREV_DOC_TOKENS)
not same_level and
(
(prev_token_type in PREV_DOC_TOKENS) or
(possible_fmt_str and possible_fmt_str[0] in PREV_DOC_TOKENS)
)
):
if self.docstrings:
value = value.strip()
Expand Down
22 changes: 22 additions & 0 deletions tests/filters/test_python.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,28 @@ def function():
self.mktemp('test.txt', template, 'utf-8')
self.assert_spellcheck('.python.yml', bad_words)

def test_same_line(self):
"""Test docstrings in environment with single line functions."""

content = self.dedent(
r'''
"""ffskdjalksd."""
class Example:
def function1(self): return False
def function2(self): return False
def function3(self):
"""wahtej."""
def function4(self): return False
"""ajsflas"""
'''
)
bad_words = ['ffskdjalksd', 'wahtej']
self.mktemp('test.txt', content, 'utf-8')
self.assert_spellcheck('.python.yml', bad_words)


class TestPythonStrings(util.PluginTestCase):
"""Test Python plugin."""
Expand Down

0 comments on commit 415e497

Please sign in to comment.