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

Commit

Permalink
Merge pull request #119 from jbeezley/raw-unicode-docstrings
Browse files Browse the repository at this point in the history
Allow docstrings given as raw unicode (ur""")
  • Loading branch information
Nurdok committed Jun 22, 2015
2 parents f7fad8c + 98c230b commit dda1584
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
11 changes: 7 additions & 4 deletions pep257.py
Original file line number Diff line number Diff line change
Expand Up @@ -996,11 +996,12 @@ def check_triple_double_quotes(self, definition, docstring):
'''
if docstring and '"""' in eval(docstring) and docstring.startswith(
("'''", "r'''", "u'''")):
("'''", "r'''", "u'''", "ur'''")):
# Allow ''' quotes if docstring contains """, because otherwise """
# quotes could not be expressed inside docstring. Not in PEP 257.
return
if docstring and not docstring.startswith(('"""', 'r"""', 'u"""')):
if docstring and not docstring.startswith(
('"""', 'r"""', 'u"""', 'ur"""')):
quotes = "'''" if "'''" in docstring[:4] else "'"
return D300(quotes)

Expand All @@ -1014,7 +1015,8 @@ def check_backslashes(self, definition, docstring):
'''
# Just check that docstring is raw, check_triple_double_quotes
# ensures the correct quotes.
if docstring and '\\' in docstring and not docstring.startswith('r'):
if docstring and '\\' in docstring and not docstring.startswith(
('r', 'ur')):
return D301()

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

@check_for(Definition)
Expand Down
25 changes: 25 additions & 0 deletions test_pep257.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from __future__ import with_statement

import sys
import os
import mock
import shutil
Expand Down Expand Up @@ -144,3 +145,27 @@ def foo():

out, err = env.invoke_pep257(args='--count')
assert '2' in out


def test_unicode_raw():
"""Test acceptance of unicode raw docstrings for python 2.x."""
if sys.version_info[0] >= 3:
return # ur"" is a syntax error in python 3.x

# This is all to avoid a syntax error for python 3.2
from codecs import unicode_escape_decode

def u(x):
return unicode_escape_decode(x)[0]

with Pep257Env() as env:
with env.open('example.py', 'wt') as example:
example.write(textwrap.dedent(u('''\
# -*- coding: utf-8 -*-
def foo():
ur"""Check unicode: \u2611 and raw: \\\\\\\\."""
''').encode('utf-8')))
env.write_config(ignore='D100', verbose=True)
out, err = env.invoke_pep257()
assert 'D301' not in err
assert 'D302' not in err

0 comments on commit dda1584

Please sign in to comment.