Title: doctest forgets previous imports
Type: behavior Stage: test needed
Components: Library (Lib) Versions: Python 3.10
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: ethan.furman, sobolevn, xtreak
Priority: normal Keywords:

Created on 2021-03-31 14:25 by ethan.furman, last changed 2022-01-10 23:17 by sobolevn.

Messages (3)
msg389903 - (view) Author: Ethan Furman (ethan.furman) * (Python committer) Date: 2021-03-31 14:25
In the Python 3.10 Doc/library/enum.rst file was the following:

.. class:: FlagBoundary

   *FlagBoundary* controls how out-of-range values are handled in *Flag* and its

   .. attribute:: STRICT

      Out-of-range values cause a :exc:`ValueError` to be raised.  This is the
      default for :class:`Flag`::

         >>> from enum import STRICT
         >>> class StrictFlag(Flag, boundary=STRICT):
         ...     RED = auto()
         ...     GREEN = auto()
         ...     BLUE = auto()
         >>> StrictFlag(2**2 + 2**4)
         Traceback (most recent call last):
         ValueError: StrictFlag: invalid value: 20
             given 0b0 10100
           allowed 0b0 00111

   .. attribute:: CONFORM

      Out-of-range values have invalid values removed, leaving a valid *Flag*

         >>> from enum import CONFORM
         >>> class ConformFlag(Flag, boundary=CONFORM):
         ...     RED = auto()
         ...     GREEN = auto()
         ...     BLUE = auto()
         >>> ConformFlag(2**2 + 2**4)

   .. attribute:: EJECT

      Out-of-range values lose their *Flag* membership and revert to :class:`int`.
      This is the default for :class:`IntFlag`::

         >>> from enum import EJECT
         >>> class EjectFlag(Flag, boundary=EJECT):
         ...     RED = auto()
         ...     GREEN = auto()
         ...     BLUE = auto()
         >>> EjectFlag(2**2 + 2**4)

   .. attribute:: KEEP

      Out-of-range values are kept, and the *Flag* membership is kept.  This is
      used for some stdlib flags:

         >>> from enum import KEEP
         >>> class KeepFlag(Flag, boundary=KEEP):
         ...     RED = auto()
         ...     GREEN = auto()
         ...     BLUE = auto()
         >>> KeepFlag(2**2 + 2**4)

All four tests are relying on a previous `from enum import Flag`, but only the three tests pass -- the fourth raises:

    Traceback (most recent call last):
      File "/home/runner/work/cpython/cpython/Lib/", line 1337, in __run
        exec(compile(example.source, filename, "single",
      File "<doctest default[1]>", line 1, in <module>
        class KeepFlag(Flag, boundary=KEEP):
    NameError: name 'Flag' is not defined
msg389904 - (view) Author: Ethan Furman (ethan.furman) * (Python committer) Date: 2021-03-31 14:27
Note that this only appears to be a problem under CI.
msg410263 - (view) Author: Nikita Sobolev (sobolevn) * (Python triager) Date: 2022-01-10 23:17
Ethan, I've tried to reproduce this, but it seems that example you attached is not valid.

`Flag` is never imported at all. The same with `auto`.

Should it be:

>>> from enum import auto, Flag, STRICT

the first time?

As you said, it only happens in rare envs. For me the sample above works completely fine.

I also can't find any historical logs from the CI failures :(

Date User Action Args
2022-01-10 23:17:59sobolevnsetnosy: + sobolevn
messages: + msg410263
2021-03-31 14:38:55xtreaksetnosy: + xtreak
2021-03-31 14:27:31ethan.furmansetmessages: + msg389904
2021-03-31 14:25:05ethan.furmancreate