通过装饰器零入侵性的,对 fire-python
的类型约束进行拓展,此外其还适用于任何函数对象进行基于注解的类型转换
pip install typefire
- 支持注解类型转换
- 支持字符串类型注解
- 支持 async/await
- 零入侵性
- 支持任意对象
from pathlib import Path
import fire
def test_fire(path: Path):
print(type(path))
print(path)
if __name__ == "__main__":
fire.Fire(test_fire)
'''
python test_fire.py "/some_path_string"
<class 'str'>
/some_path_string
'''
ps: fire #260
from typefire import Switch, TypeFire, typefire, typeswitch
import fire
import pathlib
TypeFire.add_switch(Switch(str, pathlib.Path, lambda p: pathlib.Path(p)))
@typeswitch
def main(path: pathlib.Path, *args, **kwargs):
print(path, type(path))
fire.Fire(main)
'''
python test_fire.py "/some_path_string"
/some_path_string <class 'pathlib.WindowsPath'>
'''
from typefire import Switch, TypeFire, typefire, typeswitch
import fire
import pathlib
TypeFire.add_switch(Switch(str, pathlib.Path, lambda p: pathlib.Path(p)))
@typefire
def main(path: pathlib.Path, *args, **kwargs):
print(path, type(path))
main("/some_path_string")
main("--path=/some_path_string")
案例一:
from typing import Union
def main(data: Union[bytes, Path, str]) -> bytes:
if isinstance(data, bytes):
...
elif isinstance(data, Path):
...
elif isinstance(data, str):
...
案例二:
from typefire import Switch, typeswitch, TypeFire
from pathlib import Path
TypeFire.add_switch(Switch(str, bytes, lambda x: open(x, 'rb').read()))
TypeFire.add_switch(Switch(Path, bytes, lambda x: open(x, 'rb').read()))
@typeswitch()
def main(data: bytes):
print(type(data))
main(Path('test.py'))
main('test.py')
main(open('test.py', 'rb').read())
'''
<class 'bytes'>
<class 'bytes'>
<class 'bytes'>
'''
显而易见 使用 typeswitch
显的更优雅,复用性更强
qs: test