python type_error ai_generated true

TypeError: assertRaises() 缺少 1 个必需的位置参数: 'callable'

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

ID: python/unittest-assertraises-no-context

其他格式: JSON · Markdown 中文 · English
80%修复率
89%置信度
0证据数
2024-03-06首次发现

版本兼容性

版本状态引入弃用备注
3.9 active — — —
3.12 active — — —

根因分析

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

English

`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

解决方案

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

无效尝试

常见但无效的做法:

  1. 80% 失败

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

  2. 70% 失败

    Catches all exceptions and hides real bugs.