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

Commit 51dc757

Browse files
timhoffmNurdok
authored andcommitted
D301 exceptions (#365)
1 parent e1e84c3 commit 51dc757

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

docs/release_notes.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ Bug Fixes
2020

2121
* Remove D413 from the pep257 convention (#404).
2222
* Replace `semicolon` with `colon` in D416 messages. (#409)
23+
* D301 (Use r""" if any backslashes in a docstring) does not trigger on
24+
backslashes for line continuation or unicode literals ``\u...`` and
25+
``\N...`` anymore. These are considered intended elements of the docstring
26+
and thus should not be escaped by using a raw docstring (#365).
2327

2428
4.0.1 - August 14th, 2019
2529
-------------------------
@@ -34,6 +38,7 @@ Bug Fixes
3438
the violation code table (#396).
3539
* Fixed IndentationError when parsing function arguments (#392).
3640

41+
3742
4.0.0 - July 6th, 2019
3843
----------------------
3944

src/pydocstyle/checker.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -365,11 +365,16 @@ def check_backslashes(self, definition, docstring):
365365
Use r"""raw triple double quotes""" if you use any backslashes
366366
(\) in your docstrings.
367367
368+
Exceptions are backslashes for line-continuation and unicode escape
369+
sequences \N... and \u... These are considered intended unescaped
370+
content in docstrings.
368371
'''
369372
# Just check that docstring is raw, check_triple_double_quotes
370373
# ensures the correct quotes.
371-
if docstring and '\\' in docstring and not docstring.startswith(
372-
('r', 'ur')):
374+
375+
if (docstring
376+
and re(r'\\[^\nuN]').search(docstring)
377+
and not docstring.startswith(('r', 'ur'))):
373378
return violations.D301()
374379

375380
@check_for(Definition)

src/tests/test_cases/test.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,26 @@ def double_quotes_backslash_uppercase():
270270
R"""Sum\\mary."""
271271

272272

273+
@expect('D213: Multi-line docstring summary should start at the second line')
274+
def exceptions_of_D301():
275+
"""Exclude some backslashes from D301.
276+
277+
In particular, line continuations \
278+
and unicode literals \u0394 and \N{GREEK CAPITAL LETTER DELTA}.
279+
They are considered to be intentionally unescaped.
280+
"""
281+
282+
283+
if sys.version_info[0] <= 2:
284+
@expect('D302: Use u""" for Unicode docstrings')
285+
def unicode_unmarked():
286+
"""Юникод."""
287+
288+
@expect('D302: Use u""" for Unicode docstrings')
289+
def first_word_has_unicode_byte():
290+
"""あy."""
291+
292+
273293
@expect("D400: First line should end with a period (not 'y')")
274294
@expect("D415: First line should end with a period, question mark, "
275295
"or exclamation point (not 'y')")

0 commit comments

Comments
 (0)