请教 type annotations 如何在运行时检查? - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
aheadlead

请教 type annotations 如何在运行时检查?

  •  
  •   aheadlead Oct 9, 2017 2588 views
    This topic created in 3125 days ago, the information mentioned may be changed or developed.

    对 Python 的 annotations 一直耿耿于怀,有些时候指定类型还是很便利的。

    PEP 484 -- Type Hints 中有一段:

    This PEP aims to provide a standard syntax for type annotations, opening up Python code to easier static analysis and refactoring, potential runtime type checking, and (perhaps, in some contexts) code generation utilizing type information. 

    其中提到了 potential runtime type checking, 问题就在这,如何才能实现运行时检查类型。


    (碎碎念:说来也怪,高中那年看到 python 不需要指定类型,一度震惊无比,
    这么写了几年 python 后,觉得有的地方还是指定类型更合适)

    12 replies    2017-10-10 15:48:39 +08:00
    yonka
        1
    yonka  
       Oct 9, 2017
    你的碎碎念是对的。
    xrlin
        2
    xrlin  
       Oct 9, 2017
    用 assert ? 所以现在我又想用回编译型静态类型语言.
    Kilerd
        3
    Kilerd  
       Oct 9, 2017   1
    你需要 mypy
    BBCCBB
        4
    BBCCBB  
       Oct 9, 2017   1
    you need mypy+1
    ManjusakaL
        5
    ManjusakaL  
       Oct 9, 2017   1
    运行时检查,inspect 库可以帮忙
    aheadlead
        6
    aheadlead  
    OP
       Oct 9, 2017
    @xrlin 我现在就是这么用的 写了几百行 assert 终于感觉我自己太 sb 了

    @Kilerd
    @BBCCBB 感谢!

    @ManjusakaL 愿闻其详

    此外,我还搜到了 enforce 和 typeguard 这俩玩意,不过上班没时间研究,有玩过的朋友可以讲讲吗?

    https://github.com/RussBaz/enforce

    https://github.com/agronholm/typeguard
    xrlin
        7
    xrlin  
       Oct 9, 2017
    @aheadlead 什么时候出个 TypePython 就完美了。
    ManjusakaL
        8
    ManjusakaL  
       Oct 9, 2017
    @aheadlead 484 里面已经说了,annotation 是函数 signature 的一部分,inspect 是可以获取的
    可以看看文档
    https://docs.python.org/3/library/inspect.html#inspect.getfullargspec
    ManjusakaL
        9
    ManjusakaL  
       Oct 9, 2017
    而且楼上建议的 mypy 还是略废材,可以试试
    https://github.com/google/pytype
    huntzhan
        10
    huntzhan  
       Oct 9, 2017
    我两年前写过一个运行时检查类型的: https://github.com/huntzhan/magic-constraints
    不过我认为在 Python 里做类型检查价值不大
    lolizeppelin
        11
    lolizeppelin  
       Oct 10, 2017 via Android
    首先设计上要避免需要参数检查

    一定要检查的的地方用装饰器模式实现会让代码简洁阅读性更好
    lolizeppelin
        12
    lolizeppelin  
       Oct 10, 2017 via Android
    inspect 的应用可以参考 openstack 里 taskflow 是怎么反射参数并处理的
    About     Help     Advertise     Blog     API     FAQ     Solana     2493 Online   Highest 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 55ms UTC 15:40 PVG 23:40 LAX 08:40 JFK 11:40
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86