V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
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
V2EX  ›  Python

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

  •  
  •   aheadlead · 2017-10-09 16:11:46 +08:00 · 2082 次点击
    这是一个创建于 2389 天前的主题,其中的信息可能已经有所发展或是发生改变。

    对 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 条回复    2017-10-10 15:48:39 +08:00
    yonka
        1
    yonka  
       2017-10-09 16:14:19 +08:00
    你的碎碎念是对的。
    xrlin
        2
    xrlin  
       2017-10-09 17:42:44 +08:00
    用 assert ? 所以现在我又想用回编译型静态类型语言.
    Kilerd
        3
    Kilerd  
       2017-10-09 17:46:56 +08:00   ❤️ 1
    你需要 mypy
    BBCCBB
        4
    BBCCBB  
       2017-10-09 17:47:44 +08:00   ❤️ 1
    you need mypy+1
    ManjusakaL
        5
    ManjusakaL  
       2017-10-09 18:16:55 +08:00   ❤️ 1
    运行时检查,inspect 库可以帮忙
    aheadlead
        6
    aheadlead  
    OP
       2017-10-09 18:26:51 +08:00
    @xrlin 我现在就是这么用的 写了几百行 assert 终于感觉我自己太 sb 了

    @Kilerd
    @BBCCBB 感谢!

    @ManjusakaL 愿闻其详

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

    https://github.com/RussBaz/enforce

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

    一定要检查的的地方用装饰器模式实现会让代码简洁阅读性更好
    lolizeppelin
        12
    lolizeppelin  
       2017-10-10 15:48:39 +08:00 via Android
    inspect 的应用可以参考 openstack 里 taskflow 是怎么反射参数并处理的
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   4313 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 05:31 · PVG 13:31 · LAX 22:31 · JFK 01:31
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.