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

Merged
merged 2 commits into from Jun 22, 2015

Projects

None yet

3 participants

@jbeezley
Contributor

Fixes #116.

@Nurdok
Member
Nurdok commented Jun 3, 2015

This PR looks good, but I'd like to see some tests for this.

@jbeezley
Contributor
jbeezley commented Jun 3, 2015

I originally put in some tests, but ur is a syntax error in python 3, so there needs to be some mechanism for adding a test file that is only executed on python 2. Do you have any suggestions for how to do this?

@Nurdok
Member
Nurdok commented Jun 19, 2015

@jbeezley It's possible to check if the current Python is 2.x or 3.x. I think the "Pythonic" way is to check this specific syntax for SyntaxError, like so:

try:
   eval('ur"foo"')
except SyntaxError:
    print "Python 3"
else:
    print "Python 2"
@jbeezley
Contributor

You're right, you can catch syntax errors in an eval, but I tried various iterations of it without success.

These raise missing docstring.

def raw_unicode():
    pass
raw_unicode.__doc__ = eval(r'''ur"""Test unicode \xe9 and raw \\."""''')
def raw_unicode():
    eval(r'''ur"""Test unicode \xe9 and raw \\."""''')

and you can't eval a function definition like this.

eval(r'''def raw_unicode(): ur"""Test unicode \xe9 and raw \\."""''')

You can exec a definition, but that doesn't work with your tests. I.e.

exec('def no_docstring(): pass')

doesn't raise an error.

@Nurdok
Member
Nurdok commented Jun 21, 2015

Instead of adding a test in test.py, I suggest you do it in test_pep257.py, where you can do something like this:

def test_unicode_raw():
    if sys.version_info[0] >= 3:
        return
    with Pep257Env() as env:
        with env.open('example.py', 'wt') as example:
            example.write(textwrap.dedent("""\
                def foo():
                    ur"""docstring"""
                    pass
            """))

            # assert stuff
@jbeezley
Contributor

@Nurdok Thanks for the hint. I added a test.

@Nurdok Nurdok merged commit dda1584 into PyCQA:master Jun 22, 2015

1 check passed

continuous-integration/travis-ci/pr The Travis CI build passed
Details
@Nurdok
Member
Nurdok commented Jun 22, 2015

@jbeezley Merged! Thank you so much!

@jquast
jquast commented Jun 22, 2015

Thank you @jbeezley !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment