如何使 typer 的回溯看起来正常

huangapple go评论104阅读模式
英文:

How to make typer traceback look normal

问题

当使用 typer 解析CLI参数时,我得到了非常冗长和多彩的错误消息。如何获取普通的Python回溯?请参见以下示例回溯的屏幕截图(仅显示前几行),以了解冗长风格的示例:

 python scripts/add_priors.py
╭─────────────────────────────── Traceback (most recent call last) ────────────────────────────────╮
 /Users/corneliusromer/code/nextclade_data_workflows/sars-cov-2/scripts/add_priors.py:26 in main  
                                                                                                  
   23    import polars as pl                                                                     
   24                                                                                            
   25    priors = (                                                                              
  26       pl.scan_ndjson(ndjson, infer_schema_length=10000)                                   
   27       .select(                                                                            
   28          [                                                                               
   29             pl.col("nearestNodes"),                                                     
                                                                                                  
 ╭─────────────────────────────────────────── locals ───────────────────────────────────────────╮ 
    json = <module 'json' from                                                                  
           

(Note: The code part is not translated as per your request.)

英文:

When using typer to parse CLI arguments, I get very verbose and colorful error messages. How can I get a normal Python traceback?

See screenshot for an example traceback (just the first few lines) for illustration of the verbose style:

 python scripts/add_priors.py             
╭─────────────────────────────── Traceback (most recent call last) ────────────────────────────────╮
 /Users/corneliusromer/code/nextclade_data_workflows/sars-cov-2/scripts/add_priors.py:26 in main  
                                                                                                  
   23    import polars as pl                                                                     
   24                                                                                            
   25    priors = (                                                                              
  26       pl.scan_ndjson(ndjson, infer_schema_length=10000)                                   
   27       .select(                                                                            
   28          [                                                                               
   29             pl.col(&quot;nearestNodes&quot;),                                                     
                                                                                                  
 ╭─────────────────────────────────────────── locals ───────────────────────────────────────────╮ 
    json = &lt;module &#39;json&#39; from                                                                 │ │
           

答案1

得分: 2

如果您没有使用子命令,即只使用 typer.run(main),那么无论如何都要创建一个 typer 实例并显式运行 main 命令。
例如:

app = typer.Typer(pretty_exceptions_enable=False)
app.command()(main)
app()

注意:我本来打算将其留在接受答案的下方作为注释,但我不能在注释中添加多行,并且Python对于空格/换行符要求严格。

英文:

Also, if you're not using sub-command. i.e. using just typer.run(main) then create an instance of typer regardless and run the main command explicitly.
e.g.

app = typer.Typer(pretty_exceptions_enable=False)
app.command()(main)
app()

Note: I was going to leave it as comment below accepted answer but I can't add multiple lines in comment and python is picky about spaces/newlines.

答案2

得分: 1

你可以通过设置环境变量 _TYPER_STANDARD_TRACEBACK=1 来一次性禁用它。

在初始化 typer 时,可以通过传递参数 pretty_exceptions_enable=False 来禁用丰富的异常:

import typer

app = typer.Typer(pretty_exceptions_enable=False)

@app.command()
def main():
    raise Exception("test")

if __name__ == "__main__":
    app()

查看文档以获取更多选项。

英文:

You can disable it on a one-off basis by setting the environment variable _TYPER_STANDARD_TRACEBACK=1.

Disabling rich exceptions is possible by passing the kwarg pretty_exceptions_enable=False when initializing typer:

import typer

app = typer.Typer(pretty_exceptions_enable=False)

@app.command()
def main():
    raise Exception(&quot;test&quot;)

if __name__ == &quot;__main__&quot;:
    app()

See the documentation for more options

huangapple
  • 本文由 发表于 2023年5月31日 23:51:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76375307.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定