Skip to content
This repository has been archived by the owner on Nov 3, 2023. It is now read-only.

Commit

Permalink
Add extensive tests
Browse files Browse the repository at this point in the history
  • Loading branch information
samj1912 committed Sep 6, 2020
1 parent f2a944f commit e39f80b
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 15 deletions.
20 changes: 6 additions & 14 deletions src/pydocstyle/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,20 +46,12 @@ def decorator(f):
return decorator


FSTRING_RE = re(r'^[rR]?[fF]')
_FSTRING_REGEX = re(r'^[rR]?[fF]')


def is_fstring(docstring):
r"""Return True if docstring is an f-string.
>>> is_fstring('rf"abc"')
True
>>> is_fstring('F"abc"')
True
>>> is_fstring("u'''abc'''")
False
"""
return FSTRING_RE.match(str(docstring))
def _is_fstring(docstring):
"""Return True if docstring is an f-string."""
return _FSTRING_REGEX.match(str(docstring))


class ConventionChecker:
Expand Down Expand Up @@ -204,7 +196,7 @@ def check_docstring_fstring(self, definition, docstring):
and users may attempt to use them as docstrings. This is an
outright mistake so we issue a specific error code.
"""
if is_fstring(docstring):
if _is_fstring(docstring):
return violations.D303()

@check_for(Definition, terminal=True)
Expand All @@ -221,7 +213,7 @@ def check_docstring_missing(self, definition, docstring):
with a single underscore.
"""
if is_fstring(docstring):
if _is_fstring(docstring):
return # checked above in check_docstring_fstring

if (
Expand Down
5 changes: 4 additions & 1 deletion src/pydocstyle/violations.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,10 @@ def to_rst(cls) -> str:
'D302',
'Deprecated: Use u""" for Unicode docstrings',
)
D303 = D3xx.create_error('D303', 'f-strings are not valid as docstrings')
D303 = D3xx.create_error(
'D303',
'f-strings are not valid as docstrings',
)

D4xx = ErrorRegistry.create_group('D4', 'Docstring Content Issues')
D400 = D4xx.create_error(
Expand Down
47 changes: 47 additions & 0 deletions src/tests/test_cases/fstrings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
"""Test for warning about f-strings as docstrings."""

from .expected import Expectation

expectation = Expectation()
expect = expectation.expect
_GIZMO = "gizmo"
D303 = expect("D303: f-strings are not valid as docstrings")


@D303
def fstring():
f"""Toggle the gizmo."""


@D303
def another_fstring():
F"""Toggle the gizmo."""


@D303
def fstring_with_raw():
rF"""Toggle the gizmo."""


@D303
def fstring_with_raw_caps():
RF"""Toggle the gizmo."""


@D303
def fstring_with_raw_variable():
RF"""Toggle the {_GIZMO}."""


@D303
def fstring_with_variable():
f"""Toggle the {_GIZMO.upper()}."""


@D303
def fstring_with_other_errors(arg=1, missing_arg=2):
f"""Toggle the {_GIZMO.upper()}
This should not raise any other errors since fstrings
are a terminal check.
"""
1 change: 1 addition & 0 deletions src/tests/test_definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
'noqa',
'sections',
'functions',
'fstrings',
'canonical_google_examples',
'canonical_numpy_examples',
'canonical_pep257_examples',
Expand Down

0 comments on commit e39f80b

Please sign in to comment.