This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Strange behavior under doctest: staticmethods have different __globals__
Type: enhancement Stage:
Components: Library (Lib) Versions:
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: jneb
Priority: normal Keywords:

Created on 2015-09-10 08:10 by jneb, last changed 2022-04-11 14:58 by admin.

Files
File name Uploaded Description Edit
t.py jneb, 2015-09-10 08:10 staticmethod globals under doctest
Messages (1)
msg250355 - (view) Author: Jurjen N.E. Bos (jneb) * Date: 2015-09-10 08:10
I found a pretty obscure bug/documentation issue.
It only happens if you use global variables in static methods under doctest.
The thing is that this code fails under doctest, while anyone would expect it to work at first sight:

class foo:
    @staticmethod
    def bar():
        """
        >>> GLOBAL = 5; foo.bar()
        5
        """
        print(GLOBAL)

The cause of the problem is that the static method has a __globals__ that for reasons beyond my understanding is not equal to the globals when run under doctest.
This might be a doctest bug, or there might be a reason for it that I don't get: than it must be documented, before it stupifies others.
The behaviour is the same under python 2 and 3.
The attached file shows all (written for Python 3, but can be trivially adapted for python 2), including the workaround.
History
Date User Action Args
2022-04-11 14:58:20adminsetgithub: 69237
2015-09-10 08:10:44jnebcreate