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
ClericPy
V2EX  ›  Python

Python 3.7+ 想验证一个对象是否符合 typing.Dict[str, int] 应该怎么做?

  •  
  •   ClericPy ·
    ClericPy · 2019-10-09 20:40:44 +08:00 · 4871 次点击
    这是一个创建于 1632 天前的主题,其中的信息可能已经有所发展或是发生改变。

    主要想做的就是

    1. 拿到一个对象, cast 出来这个对象是否符合 typing 里那些奇怪类型, Union Optional 等, 尤其是 Dict[str, int] 时候要判断每一个 key value 的类型
    2. 拿到不符合的类型时候, 自动转换一下, 比如 '1.1' 需要 float 类型, 自动转成 1.1

    找过一大些资料都失败了, 目前结论如下

    1. mypy 没法直接看源码, 然后看文档也没找到那种代码里 import mypy 然后做 cast 的功能

    2. typing.cast 在标准库那个是预留给 linter 用的, 实际用不上

    3. 目前已经尝试过 __origin____args__ 来做了, 效果还算不错, 但是感觉自己一点点拆包有点傻, 尤其是自己把不符合的先 _alias, 然后对 _GenericAlias 做上述俩魔术方法, 遇到了 _SpecialForm 直接凉

    mypy 官网提到的 MonkeyType (Python 3) and PyAnnotate (type comments only). 还没试

    第 1 条附言  ·  2019-10-09 21:33:50 +08:00
    问题已解决
    pydantic 已经实现了上述各种问题的解决方案, 怪只怪我一开始看了一眼 fastapi 那边的用法就以为它功能就那么点。。。
    第 2 条附言  ·  2020-03-18 20:59:38 +08:00
    16 条回复    2019-10-11 14:42:25 +08:00
    ipwx
        1
    ipwx  
       2019-10-09 20:44:15 +08:00   ❤️ 1
    1、你是想要做什么呢?如果是想要在每个函数的入口这么干,就太影响程序的性能了。
    2、如果是想要对用户的输入(比如 API parameters )进行验证,可以使用 pydantic。

    https://pydantic-docs.helpmanual.io/
    ClericPy
        2
    ClericPy  
    OP
       2019-10-09 20:46:56 +08:00
    @ipwx pydantic 看过了, 不过当时只看了它的那些 BaseSchema 用法, 就是自定义, 不过我要处理的是 typing 里的内置类型(或者复合类型)
    我只是想对某些值做一下类型校验, mypy 上没找到, 官方文档也是说 typing.cast 太影响性能所以什么都不做, 只留给 linters 做猴子补丁
    我看看那些 mypy 以外的有没有什么办法吧
    ClericPy
        3
    ClericPy  
    OP
       2019-10-09 20:49:12 +08:00
    @ipwx 好吧, 我傻了, 还是用 pydantic 吧
    说白了其实我就是想知道下 typing 里那么多复杂的类型, 怎么做验证, 学习的目的
    skinny
        4
    skinny  
       2019-10-09 20:50:56 +08:00
    那是给 IDE 用的
    ClericPy
        5
    ClericPy  
    OP
       2019-10-09 20:52:09 +08:00
    @skinny 我就是打算研究研究有什么自带的内置方法可以让我验证么, 毕竟 isinstance 是报错的
    ClericPy
        6
    ClericPy  
    OP
       2019-10-09 20:56:56 +08:00
    我先研究研究用 type 动态构造 BaseModel 子类

    from pydantic import BaseModel
    import typing
    import inspect


    def test(a: int, b: typing.Dict[str, int]):
    pass


    sigs = inspect.signature(test)

    kwargs = {p.name: p.annotation for p in sigs.parameters.values()}
    Trim21
        7
    Trim21  
       2019-10-09 21:19:55 +08:00   ❤️ 1
    看了 pydantic 源码,他是这么干的

    https://gist.github.com/Trim21/910601a17fbeaa07bd203a93afce6131

    我没仔细研究 field.validate 的第二个参数是干啥用的
    ClericPy
        8
    ClericPy  
    OP
       2019-10-09 21:32:44 +08:00
    @Trim21 我之前第一选择是 pydantic , 后来因为看文档不仔细给弃用了, 结果仔细看了下文档, 觉得真香, 把我 3 个多小时写的垃圾全弃用了...

    gist 我这边污染打不开, 好容易换 192.30.253.118 结果说 404..
    Trim21
        9
    Trim21  
       2019-10-09 21:34:00 +08:00   ❤️ 1
    @ClericPy #8


    import pydantic.validators
    from pydantic.fields import Field

    from typing import Dict, Any

    field = Field(name='d',
    type_=Dict[str, int],
    class_validators=None,
    model_config=pydantic.BaseConfig)
    raw, errs = field.validate({'key': 'value'}, {'a': '1'}, loc='loc')
    print(raw, errs)
    # {'key': 'value'}, [<pydantic.error_wrappers.ErrorWrapper object at 0x00000199F1B9BE58>]
    ClericPy
        10
    ClericPy  
    OP
       2019-10-09 21:38:58 +08:00
    @Trim21 感谢,找了半天没找到,vscode 对这些相对路径的跳转太差了。。。已解决
    lolizeppelin
        11
    lolizeppelin  
       2019-10-09 21:58:42 +08:00
    你们思维真是死板啊

    这种标准类型直接 jsonschema 校验不就完了 非要纠结到语言的新功能上
    ClericPy
        12
    ClericPy  
    OP
       2019-10-09 22:11:43 +08:00
    @lolizeppelin 感谢提醒, 我都快忘了那个库了, 刚听说 jsonschema 可以支持 Union Optional Dict[str,int]。
    呃,show me your code?
    so1n
        13
    so1n  
       2019-10-09 23:25:53 +08:00
    只处理过简单的,要__annotations__属性或者 inspect.signature
    ClericPy
        14
    ClericPy  
    OP
       2019-10-09 23:52:21 +08:00
    @so1n 我上面提到的 `__origin__ 和 __args__` 就是...
    watsy0007
        15
    watsy0007  
       2019-10-11 13:34:34 +08:00
    @ClericPy 改用 pydantic 搞定很多类型严重和转换问题.
    ClericPy
        16
    ClericPy  
    OP
       2019-10-11 14:42:25 +08:00
    @watsy0007 库是好库, 这两天看他们家源码是真特么看的想吐...
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1023 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 19:36 · PVG 03:36 · LAX 12:36 · JFK 15:36
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.