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

Commit e39f80b

Browse files
committed
Add extensive tests
1 parent f2a944f commit e39f80b

File tree

4 files changed

+58
-15
lines changed

4 files changed

+58
-15
lines changed

src/pydocstyle/checker.py

+6-14
Original file line numberDiff line numberDiff line change
@@ -46,20 +46,12 @@ def decorator(f):
4646
return decorator
4747

4848

49-
FSTRING_RE = re(r'^[rR]?[fF]')
49+
_FSTRING_REGEX = re(r'^[rR]?[fF]')
5050

5151

52-
def is_fstring(docstring):
53-
r"""Return True if docstring is an f-string.
54-
55-
>>> is_fstring('rf"abc"')
56-
True
57-
>>> is_fstring('F"abc"')
58-
True
59-
>>> is_fstring("u'''abc'''")
60-
False
61-
"""
62-
return FSTRING_RE.match(str(docstring))
52+
def _is_fstring(docstring):
53+
"""Return True if docstring is an f-string."""
54+
return _FSTRING_REGEX.match(str(docstring))
6355

6456

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

210202
@check_for(Definition, terminal=True)
@@ -221,7 +213,7 @@ def check_docstring_missing(self, definition, docstring):
221213
with a single underscore.
222214
223215
"""
224-
if is_fstring(docstring):
216+
if _is_fstring(docstring):
225217
return # checked above in check_docstring_fstring
226218

227219
if (

src/pydocstyle/violations.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,10 @@ def to_rst(cls) -> str:
312312
'D302',
313313
'Deprecated: Use u""" for Unicode docstrings',
314314
)
315-
D303 = D3xx.create_error('D303', 'f-strings are not valid as docstrings')
315+
D303 = D3xx.create_error(
316+
'D303',
317+
'f-strings are not valid as docstrings',
318+
)
316319

317320
D4xx = ErrorRegistry.create_group('D4', 'Docstring Content Issues')
318321
D400 = D4xx.create_error(

src/tests/test_cases/fstrings.py

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
"""Test for warning about f-strings as docstrings."""
2+
3+
from .expected import Expectation
4+
5+
expectation = Expectation()
6+
expect = expectation.expect
7+
_GIZMO = "gizmo"
8+
D303 = expect("D303: f-strings are not valid as docstrings")
9+
10+
11+
@D303
12+
def fstring():
13+
f"""Toggle the gizmo."""
14+
15+
16+
@D303
17+
def another_fstring():
18+
F"""Toggle the gizmo."""
19+
20+
21+
@D303
22+
def fstring_with_raw():
23+
rF"""Toggle the gizmo."""
24+
25+
26+
@D303
27+
def fstring_with_raw_caps():
28+
RF"""Toggle the gizmo."""
29+
30+
31+
@D303
32+
def fstring_with_raw_variable():
33+
RF"""Toggle the {_GIZMO}."""
34+
35+
36+
@D303
37+
def fstring_with_variable():
38+
f"""Toggle the {_GIZMO.upper()}."""
39+
40+
41+
@D303
42+
def fstring_with_other_errors(arg=1, missing_arg=2):
43+
f"""Toggle the {_GIZMO.upper()}
44+
45+
This should not raise any other errors since fstrings
46+
are a terminal check.
47+
"""

src/tests/test_definitions.py

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
'noqa',
2121
'sections',
2222
'functions',
23+
'fstrings',
2324
'canonical_google_examples',
2425
'canonical_numpy_examples',
2526
'canonical_pep257_examples',

0 commit comments

Comments
 (0)