装饰实例方法的问题
Typer 尝试以Grettings().aloha() 调用回调。
这将在 Python 中失败并出现错误:
TypeError: hallo() 缺少 1 个必需的位置参数:'self'
Typer 中的命令调用演示
请看以下 Python shell 中记录的演示:
第 1 部分:它是如何工作的(使用静态函数,没有 self 参数)
>>> import typer
>>> app = typer.Typer()
>>> app
<typer.main.Typer object at 0x7f0713f59c18>
>>> app.__dict__
{'_add_completion': True, 'info': <typer.models.TyperInfo object at 0x7f0713f59c50>, 'registered_groups': [], 'registered_commands': [], 'registered_callback': None}
>>> @app.command()
... def hello():
... typer.echo('hello')
...
>>> app.__dict__['registered_commands']
[<typer.models.CommandInfo object at 0x7f0711e69cf8>]
>>> app.__dict__['registered_commands'][0].cls
<class 'typer.core.TyperCommand'>
>>> app.__dict__['registered_commands'][0].callback
<function hello at 0x7f070f539378>
>>> app.__dict__['registered_commands'][0].callback()
hello
第 2 部分:它是如何工作的(使用实例方法,需要 self 参数)
>>> class German:
... @app.command()
... def hallo(self):
... typer.echo('Hallo')
...
>>> app.__dict__['registered_commands'][1]
<typer.models.CommandInfo object at 0x7f070f59ccf8>
>>> app.__dict__['registered_commands'][1].callback
<function German.hallo at 0x7f070f539158>
>>> app.__dict__['registered_commands'][1].callback()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: hallo() missing 1 required positional argument: 'self'
>>> app.__dict__['registered_commands'][1].callback(German())
Hallo
注意:在最后一条语句中,我将一个新实例作为参数 self 传递给回调,调用成功并获得预期输出。
固定代码
我改变了三件事:
- 将您的班级
Grettings 重命名为 Greetings(拼写)
- 将 2 个现有方法重新定义为静态类方法,例如 Barmar's comment suggested。
- 另外我添加了一个新的实例方法
nihao(self)来演示失败。
import typer
app = typer.Typer()
@app.command()
def hello(name):
typer.echo(f'Hello!')
@app.command()
def goodbye():
typer.echo(f'Goodbye.')
class Greetings:
@app.command()
def aloha(): # function or class-method (implicitly static)
typer.echo('Aloha!')
@staticmethod # explicitly static
@app.command()
def bonjour(): # no self argument!
typer.echo('Bonjour!')
@app.command()
def nihao(self): # callback invocation fails because missing self argument
typer.echo('Nihao!')
if __name__ == '__main__':
app()
行为和输出符合预期
尽管提供的命令仍将nihao 列为可用,但调用它的失败与您所经历的一样。
但是现在可以调用命令修饰的静态方法。
$ python3 SO_typer.py --help
Usage: SO_typer.py [OPTIONS] COMMAND [ARGS]...
Options:
--install-completion [bash|zsh|fish|powershell|pwsh]
Install completion for the specified shell.
--show-completion [bash|zsh|fish|powershell|pwsh]
Show completion for the specified shell, to
copy it or customize the installation.
--help Show this message and exit.
Commands:
aloha
bonjour
goodbye
hello
nihao
??️ 中文问候语失败,因为没有参数 self 通过调用传递:
$ python3 SO_typer.py nihao
Usage: SO_typer.py nihao [OPTIONS] SELF
Try 'SO_typer.py nihao --help' for help.
Error: Missing argument 'SELF'.
??????夏威夷问候语有效,因为现在可以进行静态调用:
$ python3 SO_typer.py aloha
Aloha!
另见