Skip to content
This repository

Make doctests work with Python 3 #803

Closed
takluyver opened this Issue · 1 comment

1 participant

Thomas Kluyver
Thomas Kluyver
Collaborator

Making an issue so we don't forget about this. A lot of doctests currently fail on Python 3, although the code itself is OK, and this makes it easy to overlook real failures. Most of the culprits are print statements not being picked up by 2to3, because the doctests are written with IPython prompts. Other cases tend to be individual, and will need individual fixes (e.g. .write() returns a number of bytes written).

Possible solutions:

  1. Extend 2to3 to pick up on our doctests. The 'right' way to do it, but documentation is lacking and it could be complex. I suggest practicality might beat purity.
  2. Use a regex to convert print statements inside the documentation machinery. Most computationally efficient, but probably a source of confusion for the future, because docstrings will contain Python 2 syntax with no indication that it's being translated.
  3. Use decorators & helper functions to convert doctests in place. Will slightly degrade startup performance on Python 3 as docstrings are converted, but introspection will show the Python 3 code, and docstrings being converted will be clearly marked.
  4. Run the doctests with from __future__ import print_function, and write them all using Python 3 syntax. Easy, but potentially confusing to test Python 2 code with Python 3 syntax.

I think I'm slightly leaning towards option 3, but I'd like other opinions.

Thomas Kluyver
Collaborator

I've fixed this in the meantime, using option 3.

Thomas Kluyver takluyver closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.