通常,如果您尝试为同一关键字参数传递多个值,您会收到 TypeError:
In [1]: dict(id=1, **{'id': 2})
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Input In [1], in <cell line: 1>()
----> 1 dict(id=1, **{'id': 2})
TypeError: dict() got multiple values for keyword argument 'id'
但是,如果您在处理另一个异常时执行此操作,则会收到 KeyError:
In [2]: try:
...: raise ValueError('foo') # no matter what kind of exception
...: except:
...: dict(id=1, **{'id': 2}) # raises: KeyError: 'id'
...:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Input In [2], in <cell line: 1>()
1 try:
----> 2 raise ValueError('foo') # no matter what kind of exception
3 except:
ValueError: foo
During handling of the above exception, another exception occurred:
KeyError Traceback (most recent call last)
Input In [2], in <cell line: 1>()
2 raise ValueError('foo') # no matter what kind of exception
3 except:
----> 4 dict(id=1, **{'id': 2})
KeyError: 'id'
这里发生了什么?一个完全不相关的异常如何影响dict(id=1, **{'id': 2})
抛出什么样的异常?
对于上下文,我在调查以下错误报告时发现了此行为:https://github.com/tortoise/tortoise-orm/issues/1583
这已在 CPython 3.11.8、3.10.5 和 3.9.5 上重现。