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

Python 类型标注如何地道地指明某类型为 comparable/orderable?

  •  
  •   littleMaple ·
    MapleCCC · 2021-01-02 00:58:44 +08:00 · 1698 次点击
    这是一个创建于 1248 天前的主题,其中的信息可能已经有所发展或是发生改变。

    例如如下可复现例子:

    from typing import TypeVar
    
    T = TypeVar("T")
    
    def is_bigger(a: T, b: T)->bool:
        return a > b
    

    类型检查库 Pyright 报错如下:

    Operator ">" not supported for types "T" and "T"
    

    类型检查库 MyPy 报错如下:

    Unsupported left operand type for > ("T")
    
    4 条回复    2021-01-02 16:06:18 +08:00
    ericls
        1
    ericls  
       2021-01-02 01:14:38 +08:00
    给一个 `__lt__`
    Trim21
        2
    Trim21  
       2021-01-02 01:36:02 +08:00   ❤️ 3
    littleMaple
        3
    littleMaple  
    OP
       2021-01-02 02:15:14 +08:00
    @Trim21 正解,谢谢大佬!看来使用 Python 类型标注需要额外封装的东西也不少啊 😂
    abersheeran
        4
    abersheeran  
       2021-01-02 16:06:18 +08:00
    @littleMaple 是的。还需慢慢发展。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   887 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 19:46 · PVG 03:46 · LAX 12:46 · JFK 15:46
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.