Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from Jun 22, 2015

Conversation

Projects
None yet
3 participants
@jbeezley
Copy link
Contributor

commented May 31, 2015

Fixes #116.

@jbeezley jbeezley force-pushed the jbeezley:raw-unicode-docstrings branch from 0d967d4 to 9524fb4 May 31, 2015

@jbeezley jbeezley force-pushed the jbeezley:raw-unicode-docstrings branch from 9524fb4 to 6039dc6 May 31, 2015

@Nurdok

This comment has been minimized.

Copy link
Member

commented Jun 3, 2015

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

@jbeezley

This comment has been minimized.

Copy link
Contributor Author

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

This comment has been minimized.

Copy link
Member

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

This comment has been minimized.

Copy link
Contributor Author

commented Jun 19, 2015

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

This comment has been minimized.

Copy link
Member

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 jbeezley force-pushed the jbeezley:raw-unicode-docstrings branch from 4c8208d to 98c230b Jun 22, 2015

@jbeezley

This comment has been minimized.

Copy link
Contributor Author

commented Jun 22, 2015

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

Nurdok added a commit that referenced this pull request Jun 22, 2015

Merge pull request #119 from jbeezley/raw-unicode-docstrings
Allow docstrings given as raw unicode (ur""")

@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

This comment has been minimized.

Copy link
Member

commented Jun 22, 2015

@jbeezley Merged! Thank you so much!

@jquast

This comment has been minimized.

Copy link

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
You can’t perform that action at this time.