python runtime_error ai_generated true

Failed: DID NOT RAISE <class 'SystemExit'> UNEXPECTED EXCEPTION: SystemExit(0)

ID: python/pytest-doctest-capture-stdout

Also available as: JSON · Markdown · 中文
80%Fix Rate
83%Confidence
0Evidence
2024-12-02First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
7.x active — — —
8.x active — — —

Root Cause

A doctest that calls sys.exit() or argparse with --help exits the interpreter; doctest expects a return value, not a process exit.

generic

中文

调用 sys.exit() 或带 --help 的 argparse 的 doctest 退出了解释器;doctest 期望返回值,而不是进程退出。

Workarounds

  1. 90% success
    >>> import sys
    >>> try:
    ...     main(["--help"])
    ... except SystemExit as e:
    ...     print("exit", e.code)
    exit 0
  2. 95% success
    def main(argv=None) -> int:
        ...
        return 0
    
    if __name__ == "__main__":
        raise SystemExit(main())

Dead Ends

Common approaches that don't work:

  1. 85% fail

    Continues after failure but doesn't prevent SystemExit.

  2. 70% fail

    In doctest source, suppress changes the observable output.

  3. 80% fail

    Hides a real usability bug in the CLI.