0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

python-fire: 引数の型は型ヒントでなく引数の値で決まる

Last updated at Posted at 2024-12-18

環境

概要

python-fireによって呼び出される関数において、引数の型は型ヒントでなく引数の値で決まります。

sample1.py
import fire

def hello(name: str):
    print(f"{name=}")
    print(f"{type(name)=}")

if __name__ == '__main__':
  fire.Fire(hello)
$ python sample1.py aaa
name='aaa'
type(name)=<class 'str'>

$ python sample1.py 001
name=1
type(name)=<class 'int'>

上記の例では、引数001はint型の1に変換されました。

なお、公式ドキュメントには以下の通り記載されています。

The types of the arguments are determined by their values, rather than by the function signature where they're used. You can pass any Python literal from the command line: numbers, strings, tuples, lists, dictionaries, (sets are only supported in some versions of Python). You can also nest the collections arbitrarily as long as they only contain literals. 1

string型の値001を渡す方法

string型の値を渡すには、コマンドラインの呼び出し側で引用符を用いて対処する必要があります。

example.py
import fire
fire.Fire(lambda obj: type(obj).__name__)
$ python example.py 10
int
$ python example.py "10"
int
$ python example.py '"10"'
str
$ python example.py "'10'"
str
$ python example.py \"10\"
str

https://github.com/google/python-fire/blob/master/docs/guide.md#argument-parsing 引用

変換処理

python-fireは、以下の関数でコマンドラインから渡された引数を適切な型に変換しています。

標準モジュールであるastを利用していました。

jsonargparse との比較

python-fireにインスパイアされたjsonargparseでは、型ヒントに従って値の型が決まります。

Second, the arguments are expected to have type hints, and the given values will be validated according to these. 2

sample2.py
def hello(name: str):
    print(f"{name=}")
    print(f"{type(name)=}")


if __name__ == "__main__":
    import jsonargparse

    jsonargparse.CLI()
$ python sample2.py 001
name='001'
type(name)=<class 'str'>
sample3.py
def hello(name: int):
    print(f"{name=}")
    print(f"{type(name)=}")


if __name__ == "__main__":
    import jsonargparse

    jsonargparse.CLI()
$ python sample3.py 001
name=1
type(name)=<class 'int'>
  1. https://github.com/google/python-fire/blob/master/docs/guide.md#argument-parsing

  2. https://jsonargparse.readthedocs.io/en/stable/#comparison-to-fire 引用

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?