[Numpy-discussion] numpydoc for python 3?

Matthew Brett matthew.brett at gmail.com
Mon Jan 14 07:44:40 EST 2013


Hi,

On Mon, Jan 14, 2013 at 10:35 AM, Jaakko Luttinen
<jaakko.luttinen at aalto.fi> wrote:
> On 01/14/2013 12:53 AM, Matthew Brett wrote:
>> On Sun, Jan 13, 2013 at 10:46 PM, Jaakko Luttinen
>> <jaakko.luttinen at aalto.fi> wrote:
>>> I'm a bit stuck trying to make numpydoc Python 3 compatible. I made
>>> setup.py try to use distutils.command.build_py.build_py_2to3 in order to
>>> transform installed code automatically to Python 3. However, the tests
>>> (in tests folder) are not part of the package but rather package_data,
>>> so they won't get transformed. How can I automatically transform the
>>> tests too? Probably there is some easy and "right" solution to this, but
>>> I haven't been able to figure out a nice and simple solution.. Any
>>> ideas? Thanks.
>>
>> Can you add tests as a package 'numpydoc.tests' and add an __init__.py
>> file to the 'tests' directory?
>
> I thought there is some reason why the 'tests' directory is not added as
> a package 'numpydoc.tests', so I didn't want to take that route.

I think the only reason is so that people can't import
'numpydoc.tests' in case they get confused.   We (nipy.org/nipy etc)
used to use packagedata for tests, but then we lost interest in
preventing people doing the import, and started to enjoy being able to
port things across as packages, do relative imports, run 2to3 and so
on.  So, I'd just go for it.

>> You might be able to get away without 2to3, using the kind of stuff
>> that Pauli has used for scipy recently:
>>
>> https://github.com/scipy/scipy/pull/397
>
> Ok, thanks, maybe I'll try to make the tests valid in all Python
> versions. It seems there's only one line which I'm not able to transform.
>
> In doc/sphinxext/tests/test_docscrape.py, on line 559:
>     assert doc['Summary'][0] == u'öäöäöäöäöåååå'.encode('utf-8')
>
> This is invalid in Python 3.0-3.2. How could I write this in such a way
> that it is valid in all Python versions? I'm a bit lost with these
> unicode encodings in Python (and in general).. And I didn't want to add
> dependency on 'six' package.

Pierre's suggestion is good; you can also do something like this:

# -*- coding: utf8 -*-
import sys

if sys.version_info[0] >= 3:
    a = 'öäöäöäöäöåååå'
else:
    a = unicode('öäöäöäöäöåååå', 'utf8')

The 'coding' line has to be the first or second line in the file.

Best,

Matthew



More information about the NumPy-Discussion mailing list