实例变量自动完成

问题描述 投票:0回答:1

我试图找到这个(看似简单)问题的答案,但找不到。我试图让 Pycharm 显示实例变量的代码完成。类型提示似乎只适用于同一函数。我还尝试在文档字符串中定义类型,但这仍然不起作用。如何让 Pycharm 向我显示实例变量的自动完成建议?

class Girl:
    gender = "female"

    def __init__(self, name:str):
        self.name = name
        self.name.capitalize()      # <- Pycharm shows me capitalize() as a method of name:str

    def capitalize_name(self):
        """
        :cvar name:str              # <-
        :type name:str              # <--- These don't
        :cvar self.name:str         # <--- help either
        :type self.name:str         # <-
        """
        self.name = self.name.....  # <- Pycharm does not know the type of self.name


alice = Girl(name="Alice")
alice.capitalize_name()
python pycharm python-typing
1个回答
0
投票

我从 2012 年就找到了这个解决方案,现在有更好的支持吗?

def capitalize_name(self):
    assert isinstance(self.name, str)
    self.name = self.name....   # <- works now

参考:https://intellij-support.jetbrains.com/hc/en-us/community/posts/205807049-How-to-improve-PyCharm-code-completion-auto-complete-

© www.soinside.com 2019 - 2024. All rights reserved.