python type_error ai_generated true

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

ID: python/unittest-testcase-assertraises-no-context

Also available as: JSON · Markdown · 中文
80%Fix Rate
90%Confidence
0Evidence
2024-04-22First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.9 active — — —
3.12 active — — —

Root Cause

The test calls `self.assertRaises(SomeError)` without the callable or without using it as a context manager. assertRaises requires either a callable as the second argument or use as `with self.assertRaises(SomeError):`.

generic

中文

测试调用 `self.assertRaises(SomeError)` 时未提供可调用对象,也未作为上下文管理器使用。assertRaises 需要第二个参数为可调用对象,或使用 `with self.assertRaises(SomeError):` 语法。

Workarounds

  1. 97% success
    Use the context manager form:
    
    with self.assertRaises(ValueError):
        parse_bad_input()
  2. 95% success
    Or pass the callable as the second argument:
    
    self.assertRaises(ValueError, parse_bad_input)

Dead Ends

Common approaches that don't work:

  1. 85% fail

    assertRaises expects an exception class or tuple of classes; an instance raises TypeError about expected exceptions.

  2. 60% fail

    Loses the assertRaises semantics and can silently pass if the wrong exception type is raised.