最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - Avoid PyCharm attribute reference warning from type-hinting the base class instead of the derived one - Stack Overflow

programmeradmin4浏览0评论

I would like to avoid a PyCharm warning from unrecognized attributes. This is happening because I type-hint a base class in the function argument, however the function itself uses attributes from derived classes.

More concretely, this is my code structure:

@dataclass
class Base

@dataclass
class Derived (Base)
    a: str = 'a'

class Foo:
    def __init__(self, obj: Base):
        self._obj = obj
    def geta(self):
        return self._obj.a

The last line gives me the following warning:

Unresolved attribute reference a for class Base.

Is there any way I can avoid this warning by using type-hinting?

One potential solution is to import Union from the typing module and simply add the list of derived classes in the type hint, however I am expecting quite a few derived classes so I'd rather avoid that.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论