_ is a valid variable name in doctests

Bug #1178096 reported by asmeurer
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
Pyflakes
Fix Released
Low
Unassigned

Bug Description

Example file:

def func():
    """
    A docstring
    >>> func()
    1
    >>> _
    1
    """
    return 1

pyflakes reports

test.py:6: undefined name '_'

But _ is a valid variable name in doctests (python -m doctest test.py passes)

Revision history for this message
asmeurer (asmeurer) wrote :

Just glancing at the code, I think it's easy to fix. How do you send patches using Launchpad?

Revision history for this message
Florent (florent.x) wrote :

thank you for the bug report.

You can propose a patch either on Launchpad or GitHub.

Launchpad way:
 * clone the repository
    : bzr branch lp:pyflakes pyflakes_fix
 * commit the fix
    : bzr commit -m "_ is a variable name in doctests" --fixes lp:1178096
 * publish the branch
    : bzr push lp:~asmeurer/pyflakes/1178096-underscore
 * visit the Launchpad website, view the new branch and click "propose for merging"
    https://code.launchpad.net/pyflakes

Revision history for this message
asmeurer (asmeurer) wrote :

Oh I didn't realize you were on github. I would have just done that in the first place if I knew.

Revision history for this message
asmeurer (asmeurer) wrote :

OK, on further inspection, this isn't as easy to fix as I thought, or at least not if I want to fix it right.

My plan was to just add _ to the list of defined names when in a doctest. But that's not entirely correct. It is defined only if a previous statement had a result. So for example, _ won't be defined if it's in the first line of the doctest. It also won't be defined if all the executions were assignments or function definitions. Is there a nice way to check this in the ast, or do I just need to enumerate all the cases?

By the way, would you actually prefer a patch on GitHub or here on Launchpad. GitHub is way easier for me, because I know git and am used to the pull request interface (and it's IMO better anyway), but I'll do it here on Launchpad if that's easier for you (assuming I can figure it out).

Revision history for this message
Florent (florent.x) wrote :

The issue reported here is fixed in trunk.

Actually _ is only defined after the first statement which returns a value not equal to None, as explained by ameurer in the previous comment.

However, the static analysis provided by the AST is not able to detect such condition.

We prefer to avoid false-positives here.

Changed in pyflakes:
importance: Undecided → Low
milestone: none → 0.8
status: New → Fix Committed
Revision history for this message
asmeurer (asmeurer) wrote :

Hmm, actually, playing around with it, it looks like the doctest runner *always* defines _. If nothing has been evaluated yet, it is set to None (this is unlike the interactive Python session where the behavior you described takes place).

Revision history for this message
Florent (florent.x) wrote :

So the fix is better than I thought.

Thank you for this update.

Florent (florent.x)
Changed in pyflakes:
status: Fix Committed → Fix Released
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.