Python 导致警告的变量类型注释

Python 导致警告的变量类型注释,python,annotations,pycharm,python-3.4,Python,Annotations,Pycharm,Python 3.4,我是Python开发的新手,正在尝试解决一些问题。我正在使用Pycharm进行开发。我目前正在尝试注释变量的类型,以便使用自动完成和建议更容易地访问。我已经尝试了代码的迭代,结果好坏参半 这是有问题的代码: path = os.path.dirname(os.path.realpath(__file__)) # type: str components = path.split(os.sep) # type: list[str] 显示的第一个问题位于第二行类型注释的左大括号处。它说: Cl

我是Python开发的新手,正在尝试解决一些问题。我正在使用Pycharm进行开发。我目前正在尝试注释变量的类型,以便使用自动完成和建议更容易地访问。我已经尝试了代码的迭代,结果好坏参半

这是有问题的代码:

path = os.path.dirname(os.path.realpath(__file__))  # type: str
components = path.split(os.sep)  # type: list[str]
显示的第一个问题位于第二行类型注释的左大括号处。它说:

Class 'type' does not define '__getitem__', so the '[]' operator cannot be used on its instances.
我在谷歌上搜索了一下,虽然问题似乎很清楚,但打开
列表
类的代码清楚地显示了一个方法
\uuu getitem\uuu

class list(object):
    """
    list() -> new empty list
    list(iterable) -> new list initialized from iterable's items
    """

....

    def __getitem__(self, y): # real signature unknown; restored from __doc__
        """ x.__getitem__(y) <==> x[y] """
        pass
现在一切都中断了:第二行现在抱怨:

Expected type 'List[str]', got 'List[str]' instead`
先前关于
\uuuu getitem\uuuu
的问题仍然存在


有没有一种方法可以在不给检查器带来问题的情况下注释这些变量?在这方面,我对Python文档不太满意,没有明确说明其内置方法的返回类型。我必须依赖Pycharm在文档弹出窗口(Ctrl+q)中提供的信息。

使用
List[str]
而不是
List[str]
是一个正确的解决方案,因为内置类型不能在类型提示中使用,相应的类型尚未被接受


您从哪个模块导入
列表
?我无法重现2019.3版的问题。

Pycharm建议
\ast.List(expr)
,这就是我使用的。请切换到
键入.List
Expected type 'List[str]', got 'List[str]' instead`