admin管理员组

文章数量:1417411

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.

本文标签: pycharmPython inspector ignores property return hint when using TypeVarStack Overflow