0

I'm having troubles with unicode strings in tuples when using doctests.

This is a minimal script:

from __future__ import unicode_literals, print_function
import numpy as np

def some_api_function():
    # do stuff
    return "bla", np.array([0,1,2])

print(some_api_function())

Running with python3:

('bla', array([0, 1, 2]))

Running with python2:

(u'bla', array([0, 1, 2]))

Ultimately I would like to write a doc string example that can be checked with a doctest.

obviously this will only work with python3:

 Examples:
   >>> some_api_function()
   ('bla', array([0, 1, 2]))

I'm looking for a solution that works for both python2 and python3 and does not make the examples too obscure to understand.

EDIT: Updated the code examples and tried to make it easier to understand my issues.

cel
  • 30,017
  • 18
  • 97
  • 117
  • Thanks for pointing to this question. It is indeed very similar to mine. Unfortunately the answers suggest that there's no a simple answer for this problem. – cel May 23 '14 at 10:53
  • That's not a good reason for opening a duplicate. Not all questions have a simple answer. If you think the answers to the other question aren't acceptable comment on them/downvote them. when you have at least 75 reputation you can also open bounties to attract attention on questions. – Bakuriu May 24 '14 at 21:01
  • This said, if your test requires a specific format then you must enforce it in some way. The built-in doctest runner doesn't have any option to be compatible with different python versions. An alternative is to use the `function() == ('bla', array([0, 1, 2]))` style. – Bakuriu May 24 '14 at 21:05
  • @Bakuriu Sorry I missed that question when I put together my question - I did not ignore it on purpose. Your alternative with equals does not work for me as for numpy arrays "==" does not result in True or False, but in an array of boolean values. – cel May 25 '14 at 07:03

0 Answers0