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

Allow docstrings given as raw unicode (ur""") #119

Merged
merged 2 commits into from
Jun 22, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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