PyCharm 在有效代码上显示“无法读取属性”

iiN*_*ori 5 python typing pycharm

我们看下面的代码:

import typing

def make_getter(field: str) -> typing.Callable[['A'], int]:
    def getter(self: 'A') -> int:
        return int(self.dict[field])
    return getter

def make_setter(field: str) -> typing.Callable[['A', int], None]:
    def setter(self: 'A', value: int):
        self.dict[field] = str(value)
    return setter

class A:
    def __init__(self, d: dict):
        super().__init__()
        self.dict = d

    get_x = make_getter('foo')
    set_x = make_setter('foo')
    x = property(get_x, set_x)

    def get_y(self) -> int:
        return int(self.dict['bar'])
    def set_y(self, value: int):
        self.dict['bar'] = str(value)
    y = property(get_y, set_y)
Run Code Online (Sandbox Code Playgroud)

我定义了 2 个属性:xy。两者都应该工作正常,没有任何问题,两者应该具有相同的行为。接下来,代码如下:

a = A(dict())
a.x = 10
print(a.x)
a.y = 20
print(a.y)
Run Code Online (Sandbox Code Playgroud)

PyCharm 编辑器显示:“无法读取属性a.x。但这段代码执行得很好,没有任何问题。

第一个想法是 PyCharm 错误地推断了类型。但看看我录制的这个短视频。我看不出类型有任何问题。

还:

print(repr(a.get_x), repr(a.get_y))
print(repr(A.get_x), repr(A.get_y))
print(repr(A.x), repr(A.y))
Run Code Online (Sandbox Code Playgroud)

它的输出:

<bound method make_getter.<locals>.getter of <__main__.A object at 0x7f7d25145f28>> <bound method A.get_y of <__main__.A object at 0x7f7d25145f28>>
<function make_getter.<locals>.getter at 0x7f7d25132e18> <function A.get_y at 0x7f7d25132f28>
<property object at 0x7f7d25143c78> <property object at 0x7f7d25143cc8>
Run Code Online (Sandbox Code Playgroud)

...所以x几乎y等同。

为什么 PyCharm 这么说?我做错了什么或者这是一种错误?如何修复它(不禁用此类警告)?

jef*_*upp 1

我不确定为什么 PyCharm 需要这个,但明确给出返回类型__init__使警告消失:

def __init__(self, d: dict) -> object: