Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy do Python doctests require isinstance to use matching qualified/unqualified naming?
    primarykey
    data
    text
    <blockquote> <p><strong>Possible Duplicate:</strong><br> <a href="https://stackoverflow.com/questions/13181559/importing-modules-main-vs-import-as-module">Importing modules: __main__ vs import as module</a> </p> </blockquote> <p>I am aware that in most cases <code>isinstance</code> is not needed or should be avoided (e.g., <a href="https://stackoverflow.com/questions/3111611/is-this-use-of-isinstance-pythonic-good">Is this use of isinstance pythonic/&quot;good&quot;?</a>). However, sometimes it is what I need. </p> <p>In both Python 2.6, 2.7, and 3.2, however, I have noticed the following irregularity in how <code>isinstance</code> works between execution in doctests and normal execution. </p> <p>For example, I expect the following behavior, where <code>isinstance</code> is ambivalent to whether I am using qualified or unqualified names:</p> <pre><code>import collections from collections import OrderedDict assert isinstance(collections.OrderedDict(), collections.OrderedDict) == True assert isinstance(collections.OrderedDict(), OrderedDict) == True </code></pre> <p>The same code, executed within a Doctest, works just the same.</p> <p>However, if I define a class within a module named "isinstanceTest.py" and define a function within the same module that uses <code>isinstance</code> to match that class, things get more interesting. The following doctest is written to pass. I am using the <code>unittest.TextTestRunner</code> as I routinely gather and run my doctests and unittests across many modules together in one suite.</p> <pre><code>import unittest, doctest import collections class A: pass def isinstance_A(arg): ''' &gt;&gt;&gt; import isinstanceTest &gt;&gt;&gt; isinstanceTest.isinstance_A(isinstanceTest.A()) True &gt;&gt;&gt; isinstance_A(A()) True &gt;&gt;&gt; isinstanceTest.isinstance_A(A()) False &gt;&gt;&gt; isinstance_A(isinstanceTest.A()) False ''' return isinstance(arg, A) if __name__ == '__main__': l = doctest.DocTestSuite(__name__) runner = unittest.TextTestRunner() runner.run(l) </code></pre> <p>Notice that the last two doctest statements, where both qualified and unqualified names are used, produce unexpected results. </p> <p>If, while in the directory where "isinstanceTest.py" lives, I open an interpreter and execute equivalent commands, results are as expected:</p> <pre><code>Python 3.2.3 (default, May 3 2012, 15:51:42) [GCC 4.6.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. &gt;&gt;&gt; import isinstanceTest &gt;&gt;&gt; from isinstanceTest import * &gt;&gt;&gt; isinstanceTest.isinstance_A(isinstanceTest.A()) True &gt;&gt;&gt; isinstance_A(A()) True &gt;&gt;&gt; isinstanceTest.isinstance_A(A()) True &gt;&gt;&gt; isinstance_A(isinstanceTest.A()) True </code></pre> <p>So while this behavior is consistent and localized to running doctests in test suites (and I have coded around it as necessary, and generally only use qualified names in doctests anyways), the larger question is: is this a documented limitation of doctests (I have not yet found any docs that mention this), or is this a bug? </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload