简体   繁体   English

如何让 IPython 自动调用 pdb 进行警告?

[英]How do I make IPython automatically call pdb for warnings?

Automatic pdb calling works for errors by toggling %pdb in IPython.自动 pdb 调用通过在 IPython 中切换 %pdb 来解决错误。 Is there an equivalent operation (or workaround) to automatically call pdb on warnings?是否有等效的操作(或解决方法)在警告时自动调用 pdb?

Adding pdb.set_trace() to the reported line number is not ideal because the warning is inside aa library file that's called from my code.将 pdb.set_trace() 添加到报告的行号并不理想,因为警告位于从我的代码调用的库文件中。

"python -m pdb myscript.py" will do this, but I'm inside an ipython repl, not the regular python repl. “python -m pdb myscript.py”将执行此操作,但我在 ipython repl 中,而不是常规 python repl。

The use case here is to find type hinting errors, which are reporteed as warnings like this "RuntimeWarning: invalid value encountered in equal"这里的用例是查找类型提示错误,这些错误被报告为这样的警告“RuntimeWarning:在相等中遇到无效值”

By setting a simple filter to turn warnings into 'error' :通过设置一个简单的过滤器将警告转换为'error'

from warnings import warn, simplefilter
simplefilter('error')

this seems to be working just fine, eg a sample function:这似乎工作得很好,例如一个示例函数:

def foo():
    a = '20'
    warn("Horrible Things", RuntimeWarning)

which when called now triggers pdb :现在调用时会触发pdb

foo()
> <ipython-input-78-f33d9e580240>(4)foo()
      1 from warnings import warn
      2 def foo():
      3     a = '20'
----> 4     warn("Horrible Things", RuntimeWarning)


ipdb> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM