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

@jbeezley
Copy link
Contributor

@jbeezley jbeezley commented May 31, 2015

Fixes #116.

@jbeezley jbeezley force-pushed the raw-unicode-docstrings branch from 0d967d4 to 9524fb4 May 31, 2015
@jbeezley jbeezley force-pushed the raw-unicode-docstrings branch from 9524fb4 to 6039dc6 May 31, 2015
@Nurdok
Copy link
Member

@Nurdok Nurdok commented Jun 3, 2015

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

Loading

@jbeezley
Copy link
Contributor Author

@jbeezley 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?

Loading

@Nurdok
Copy link
Member

@Nurdok 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"

Loading

@jbeezley
Copy link
Contributor Author

@jbeezley jbeezley 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.

Loading

@Nurdok
Copy link
Member

@Nurdok 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

Loading

@jbeezley jbeezley force-pushed the raw-unicode-docstrings branch from 4c8208d to 98c230b Jun 22, 2015
@jbeezley
Copy link
Contributor Author

@jbeezley jbeezley commented Jun 22, 2015

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

Loading

Nurdok added a commit that referenced this issue Jun 22, 2015
Allow docstrings given as raw unicode (ur""")
@Nurdok Nurdok merged commit dda1584 into PyCQA:master Jun 22, 2015
1 check passed
Loading
@Nurdok
Copy link
Member

@Nurdok Nurdok commented Jun 22, 2015

@jbeezley Merged! Thank you so much!

Loading

@jquast
Copy link

@jquast jquast commented Jun 22, 2015

Thank you @jbeezley !

Loading

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

3 participants