python type_error ai_generated true

TypeError: assertRaises() missing 1 required positional argument: 'callable'

ID: python/unittest-assertraises-no-context

Also available as: JSON · Markdown · 中文
80%Fix Rate
89%Confidence
0Evidence
2024-03-06First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.9 active — — —
3.12 active — — —

Root Cause

`assertRaises` is used as `with self.assertRaises(ValueError):` but the exception type is passed as a string or the callable form is used incorrectly.

generic

中文

`assertRaises` 以 `with self.assertRaises(ValueError):` 形式使用,但异常类型被传为字符串或可调用形式使用不当。

Workarounds

  1. 97% success
    Use the context manager form correctly:
    with self.assertRaises(ValueError):
        parse('bad')
    
    Or the callable form:
    self.assertRaises(ValueError, parse, 'bad')

Dead Ends

Common approaches that don't work:

  1. 80% fail

    Strings are not exception classes; raises TypeError or fails silently.

  2. 70% fail

    Catches all exceptions and hides real bugs.