运行 Python 对名为 `types.py` 的文件的 doctest

Running Python's doctest on a file named `types.py`

在 Python 2 和 3 中,我不能在名为 types.py 的文件中 运行 doctests,它是包的一部分。这是我得到的:

$ cat foo/types.py
def x():
    """do something

    >>> x()
    1
    """
    return 1

$ cp foo/types.py foo/types2.py

$ python -m doctest -v foo/types.py
1 items had no tests:
    types
0 tests in 1 items.
0 passed and 0 failed.
Test passed.

$ python -m doctest -v foo/types2.py
Trying:
    x()
Expecting:
    1
ok
1 items had no tests:
    types2
1 items passed all tests:
   1 tests in types2.x
1 tests in 2 items.
1 passed and 0 failed.
Test passed.

$ python3 -m doctest -v foo/types.py
37 items had no tests:
    types
    types.DynamicClassAttribute
    types.DynamicClassAttribute.__delete__
    types.DynamicClassAttribute.__get__
    types.DynamicClassAttribute.__init__
    types.DynamicClassAttribute.__set__
    types.DynamicClassAttribute.deleter
    types.DynamicClassAttribute.getter
    types.DynamicClassAttribute.setter
    types.SimpleNamespace
    types.SimpleNamespace.__delattr__
    types.SimpleNamespace.__eq__
    types.SimpleNamespace.__ge__
    types.SimpleNamespace.__getattribute__
    types.SimpleNamespace.__gt__
    types.SimpleNamespace.__init__
    types.SimpleNamespace.__le__
    types.SimpleNamespace.__lt__
    types.SimpleNamespace.__ne__
    types.SimpleNamespace.__reduce__
    types.SimpleNamespace.__repr__
    types.SimpleNamespace.__setattr__
    types._GeneratorWrapper
    types._GeneratorWrapper.__init__
    types._GeneratorWrapper.__iter__
    types._GeneratorWrapper.__next__
    types._GeneratorWrapper.close
    types._GeneratorWrapper.cr_await
    types._GeneratorWrapper.gi_code
    types._GeneratorWrapper.gi_frame
    types._GeneratorWrapper.gi_running
    types._GeneratorWrapper.send
    types._GeneratorWrapper.throw
    types._calculate_meta
    types.coroutine
    types.new_class
    types.prepare_class
0 tests in 37 items.
0 passed and 0 failed.
Test passed.

$ python3 -m doctest -v foo/types2.py
Trying:
    x()
Expecting:
    1
ok
1 items had no tests:
    types2
1 items passed all tests:
   1 tests in types2.x
1 tests in 2 items.
1 passed and 0 failed.
Test passed.

如您所见,所有使用 foo/types2.py 的调用都按预期工作,所有使用 foo/types.py 的调用似乎都试图加载 Python 内置 types 模块。

我也无法通过修补 PYTHONPATH 来解决这个问题:

$ PYTHONPATH=.:$PYTHONPATH python -m doctest -v foo/types.py
Traceback (most recent call last):
  File "/usr/lib/python2.7/site.py", line 68, in <module>
    import os
  File "/usr/lib/python2.7/os.py", line 400, in <module>
    import UserDict
  File "/usr/lib/python2.7/UserDict.py", line 116, in <module>
    import _abcoll
  File "/usr/lib/python2.7/_abcoll.py", line 70, in <module>
    Iterable.register(str)
  File "/usr/lib/python2.7/abc.py", line 107, in register
    if not isinstance(subclass, (type, types.ClassType)):
AttributeError: 'module' object has no attribute 'ClassType'

不幸的是,我不能简单地重命名 foo/types.py

除了围绕它编写大量样板代码之外,是否有可能 运行 从该文件进行文档测试?

我认为你不能在这里使用 python -m doctest:文档说它 “import[s the module] as a standalone module”,添加

Note that this may not work correctly if the file is part of a package and imports other submodules from that package.

这是一种奇特的说法,它使用了模块的非限定名称。当然会和标准库模块冲突。