pycharm - Python inspector ignores property return hint when using TypeVar - Stack Overflow

from abc import ABC, abstractmethodfrom typing import TypeVarT = TypeVar('T', bound='Ab

from abc import ABC, abstractmethod
from typing import TypeVar

T = TypeVar('T', bound='Abs')


class A:
    val: int = 10


class Abs(ABC):
    @property
    @abstractmethod
    def a(self) -> A:
        ...


class MyClass(Abs):
    _a: A = A()

    @property
    def a(self) -> A:
        return self._a


def foo(obj: T):
    print(obj.a.val)

In this example the code inspector highlights obj.a.val with Unresolved attribute reference 'val' for class 'property'.

Is that me incorrectly using the TypeVar, or maybe the problem is with PyCharm inspector? Is it possible in the first place to infer that a has val for sure?

from abc import ABC, abstractmethod
from typing import TypeVar

T = TypeVar('T', bound='Abs')


class A:
    val: int = 10


class Abs(ABC):
    @property
    @abstractmethod
    def a(self) -> A:
        ...


class MyClass(Abs):
    _a: A = A()

    @property
    def a(self) -> A:
        return self._a


def foo(obj: T):
    print(obj.a.val)

In this example the code inspector highlights obj.a.val with Unresolved attribute reference 'val' for class 'property'.

Is that me incorrectly using the TypeVar, or maybe the problem is with PyCharm inspector? Is it possible in the first place to infer that a has val for sure?

Share Improve this question edited Jan 31 at 15:58 InSync 11.1k4 gold badges18 silver badges56 bronze badges asked Jan 31 at 11:46 Anton ZeleninAnton Zelenin 331 silver badge5 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

Your code is fine. The inspector is handling it wrong. mypy accepts your code without complaint, and your code behaves correctly at runtime too.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745264112a4619347.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信