小编Tec*_*oft的帖子

Mixin 方法 super() 调用在 PyCharm 中生成未解析的属性引用

在 PyCharm 中,我有一些看起来像这样的代码:

class Mixin:
    def get_form_kwargs(self):
        kwargs = super().get_form_kwargs()
        kwargs.update({'foo': 'bar'})
        return kwargs
Run Code Online (Sandbox Code Playgroud)

短绒Unresolved attribute reference 'get_form_kwargs' for class 'object'super()电话中提出了一个问题。这个问题 100% 正确,但对 mixin 没有帮助。我们有很多具有这种super()模式的mixin 。

我知道属性的解决方案,但不知道您可以为这些未定义的类属性声明类型的方法。例如:

class Mixin:
    foo:str
Run Code Online (Sandbox Code Playgroud)

我很好奇是否有任何类似的方法可以帮助 linter 识别 mixin。

谢谢!

python typing pycharm

6
推荐指数
1
解决办法
202
查看次数

带有诅咒的 Python 类型提示

我想弄清楚在这个函数顶部的类型注释中应该放什么。

我有以下简单的例子:

import curses

def main(stdscr):
    stdscr.clear()

    stdscr.addstr(2, 0, "What is the type of stdscr?")
    stdscr.addstr(5, 0, "It is: {}".format(type(stdscr)))

    stdscr.refresh()
    stdscr.getkey()

curses.wrapper(main)
Run Code Online (Sandbox Code Playgroud)

这返回<type '_curses.curses window'>. 这似乎不适用于类型提示,因为它有一个空格。 预期结果将WindowObject列在文档中。我在curses 模块本身中找不到WindowObject 的路径。编辑:此处的文档不正确。

如何使用准确的类型注释编写 main ?

python type-hinting python-curses python-3.5 python-3.6

5
推荐指数
1
解决办法
665
查看次数