diff --git a/src/pydocstyle/checker.py b/src/pydocstyle/checker.py index d75e6827..29cbff9d 100644 --- a/src/pydocstyle/checker.py +++ b/src/pydocstyle/checker.py @@ -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: @@ -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) @@ -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 ( diff --git a/src/pydocstyle/violations.py b/src/pydocstyle/violations.py index 870095f0..a5bf5c1e 100644 --- a/src/pydocstyle/violations.py +++ b/src/pydocstyle/violations.py @@ -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( diff --git a/src/tests/test_cases/fstrings.py b/src/tests/test_cases/fstrings.py new file mode 100644 index 00000000..ee8756ca --- /dev/null +++ b/src/tests/test_cases/fstrings.py @@ -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. + """ diff --git a/src/tests/test_definitions.py b/src/tests/test_definitions.py index 3971f0a0..605d9bb9 100644 --- a/src/tests/test_definitions.py +++ b/src/tests/test_definitions.py @@ -20,6 +20,7 @@ 'noqa', 'sections', 'functions', + 'fstrings', 'canonical_google_examples', 'canonical_numpy_examples', 'canonical_pep257_examples',