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

Commit dda1584

Browse files
committed
Merge pull request #119 from jbeezley/raw-unicode-docstrings
Allow docstrings given as raw unicode (ur""")
2 parents f7fad8c + 98c230b commit dda1584

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

pep257.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -996,11 +996,12 @@ def check_triple_double_quotes(self, definition, docstring):
996996
997997
'''
998998
if docstring and '"""' in eval(docstring) and docstring.startswith(
999-
("'''", "r'''", "u'''")):
999+
("'''", "r'''", "u'''", "ur'''")):
10001000
# Allow ''' quotes if docstring contains """, because otherwise """
10011001
# quotes could not be expressed inside docstring. Not in PEP 257.
10021002
return
1003-
if docstring and not docstring.startswith(('"""', 'r"""', 'u"""')):
1003+
if docstring and not docstring.startswith(
1004+
('"""', 'r"""', 'u"""', 'ur"""')):
10041005
quotes = "'''" if "'''" in docstring[:4] else "'"
10051006
return D300(quotes)
10061007

@@ -1014,7 +1015,8 @@ def check_backslashes(self, definition, docstring):
10141015
'''
10151016
# Just check that docstring is raw, check_triple_double_quotes
10161017
# ensures the correct quotes.
1017-
if docstring and '\\' in docstring and not docstring.startswith('r'):
1018+
if docstring and '\\' in docstring and not docstring.startswith(
1019+
('r', 'ur')):
10181020
return D301()
10191021

10201022
@check_for(Definition)
@@ -1027,7 +1029,8 @@ def check_unicode_docstring(self, definition, docstring):
10271029
# Just check that docstring is unicode, check_triple_double_quotes
10281030
# ensures the correct quotes.
10291031
if docstring and sys.version_info[0] <= 2:
1030-
if not is_ascii(docstring) and not docstring.startswith('u'):
1032+
if not is_ascii(docstring) and not docstring.startswith(
1033+
('u', 'ur')):
10311034
return D302()
10321035

10331036
@check_for(Definition)

test_pep257.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
from __future__ import with_statement
66

7+
import sys
78
import os
89
import mock
910
import shutil
@@ -144,3 +145,27 @@ def foo():
144145

145146
out, err = env.invoke_pep257(args='--count')
146147
assert '2' in out
148+
149+
150+
def test_unicode_raw():
151+
"""Test acceptance of unicode raw docstrings for python 2.x."""
152+
if sys.version_info[0] >= 3:
153+
return # ur"" is a syntax error in python 3.x
154+
155+
# This is all to avoid a syntax error for python 3.2
156+
from codecs import unicode_escape_decode
157+
158+
def u(x):
159+
return unicode_escape_decode(x)[0]
160+
161+
with Pep257Env() as env:
162+
with env.open('example.py', 'wt') as example:
163+
example.write(textwrap.dedent(u('''\
164+
# -*- coding: utf-8 -*-
165+
def foo():
166+
ur"""Check unicode: \u2611 and raw: \\\\\\\\."""
167+
''').encode('utf-8')))
168+
env.write_config(ignore='D100', verbose=True)
169+
out, err = env.invoke_pep257()
170+
assert 'D301' not in err
171+
assert 'D302' not in err

0 commit comments

Comments
 (0)