python - 当作为参数传递给 range() 时,如何告诉 PyCharm 类实例可以通过 __index__ 方法解释为整数?

标签 python class pycharm integer warnings

考虑以下示例:

import random


class Class:
    def __index__(self):
        return random.randint(-100, 100)


c = Class()
print(range(c, c, c))

它运行正确并打印类似 range(11, -29, -77) 的内容。但 PyCharm 警告我,在作为参数传递给 range 的每个 c 上,预期类型“int”,获得“Class” 3 次。我想通过向 PyCharm 解释将 Class 实例作为参数传递给 range 来消除此警告。我试图将 Class 声明为 numbers.Integral 的子类,以表明 Class 确实实现了整数类型,但它没有帮助,修改了示例:

import random
import numbers


class Class(numbers.Integral):
    def __index__(self):
        return random.randint(-100, 100)

    __int__ = __index__  # it doesn't help either

    __abs__ = None  # they all must be overloaded because they are abstract in numbers.Integral
    __add__ = None
    __and__ = None
    __ceil__ = None
    __eq__ = None
    __floor__ = None
    __floordiv__ = None
    __invert__ = None
    __le__ = None
    __lshift__ = None
    __lt__ = None
    __mod__ = None
    __mul__ = None
    __neg__ = None
    __or__ = None
    __pos__ = None
    __pow__ = None
    __radd__ = None
    __rand__ = None
    __rfloordiv__ = None
    __rlshift__ = None
    __rmod__ = None
    __rmul__ = None
    __ror__ = None
    __round__ = None
    __rpow__ = None
    __rrshift__ = None
    __rshift__ = None
    __rtruediv__ = None
    __rxor__ = None
    __truediv__ = None
    __trunc__ = None
    __xor__ = None


c = Class()
print(range(c, c, c))

警告仍然存在。您知道如何处理吗?

最佳答案

根据[Python 3.Docs]: Data model - object.__index__(self) :

Note: In order to have a coherent integer type class, when __index__() is defined __int__() should also be defined, and both should return the same value.

但从问题中可以看出,这并不是新信息。下面,我尝试了不同的替代方案。

1。代码更改

code00.py:

#!/usr/bin/env python3

import sys
import random
from typing import SupportsInt


def _randint():
    return random.randint(-100, 100)


class C0:
    def __index__(self):
        return _randint()


class C1:
    def __index__(self):
        return _randint()

    __int__ = __index__


class C2(SupportsInt):
    def __index__(self):
        return _randint()

    __int__ = __index__


class C3(int):
    def __index__(self):
        return _randint()

    __int__ = __index__


def main():
    c0 = C0()
    c1 = C1()
    c2 = C2()
    c3 = C3()

    print(c1.__class__.__name__, list(range(int(c1), c1)))
    print(c2.__class__.__name__, list(range(int(c2), c2)))
    print(c3.__class__.__name__, list(range(int(c3), c3)))
    print(c0.__class__.__name__, list(range(int(c0), c0)))


if __name__ == "__main__":
    print("Python {0:s} {1:d}bit on {2:s}\n".format(" ".join(item.strip() for item in sys.version.split("\n")), 64 if sys.maxsize > 0x100000000 else 32, sys.platform))
    main()
    print("\nDone.")

Img0

注释:

  • 显然,PyCharm代码检查器没有进行足够深入的检查
  • 将对象显式转换为int。就我个人而言,我认为这是一种解决方法,就我而言,警告的缺失并不能证明它是合理的。无论如何,它不适用于 C0 对象,这会引发异常
  • 扩展int就可以了。但同样,我不知道是否值得这样做只是为了摆脱警告

2。 PyCharm 设置

Img1

禁用Python -> 类型检查器检查。正如所见,它也能达到目的。但它也隐藏了一些真正的错误(例如,int(c0)不再突出显示,但它是错误的)。因此,就我而言,这弊大于利。
也许您可以将其保留为事件状态,但请调整其设置以缩小其效果。更多详情请参阅[JetBrains]: Code inspections .

关于python - 当作为参数传递给 range() 时,如何告诉 PyCharm 类实例可以通过 __index__ 方法解释为整数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57922380/

相关文章:

python - 无法在 Python 中获取 Beautiful Soup 中的 div 标签,

python - 设置开发环境: PyCharm, python-gtk,windows

python - `data` 数据集(scikit learn)中的 "Labelled Faces in the Wild"字段的性质是什么?

python - 将一个数据框中的列添加到另一个 python pandas

css - 如何在自定义 css 框 wordpress 中隐藏主题功能

c# - 在 C# 中调用类的 protected 构造函数

c++ - 一个包含 3 个对象的类,它们有指向彼此的指针,以及一个用于传递对这些对象的引用的初始化列表。错误

ide - pycharm中.css文件的装订线中没有CSS颜色预览图标

python - 如何强制 Django 每次重新创建 .pyc 文件?

python - 在虚拟环境中使用 pip 构建 uwsgi 时出错