Set typehint for custom subclass of List - TypeError: 'type' object is not subscriptable
Set typehint for custom subclass of List - TypeError: 'type' object is not subscriptable
我正在尝试通过 typehinting.
指定返回的 List
子类的元素
from typing import Union, List, Tuple, Optional, NewType
from jira import JIRA
from jira.client import ResultList
from jira.resources import Issue
def search(...) -> ResultList[Issue]: # using 'List[Issue]:' or 'ResultList:' as return type hint work
...
但是,我运行宁在这个错误:
TypeError: 'type' object is not subscriptable
我试过 NewType 的运气,但没能达到预期的 运行。当不指定子类 ResultList[Issue]
而是使用 List[Issue]
时,它可以工作。此外,当不通过简单地使用 ResultList
提及元素类型时,它是有效的。
附加信息:
你应该看看这些:
- https://docs.python.org/3/library/typing.html#typing.Generic
- https://mypy.readthedocs.io/en/stable/generics.html
- https://github.com/python/mypy/wiki/Creating-Stubs-For-Python-Modules
- https://www.jetbrains.com/help/pycharm/stubs.html
解释:
list和typing.List是完全不同的东西
为了使用泛型,你必须继承自 typing.Generic
如果您使用第三方库代码并且无法更改 ResultList,那么存根是一个解决方案
基本上,您需要在 *.pyi 文件中为 ResultList class 定义存根,如下所示:
from typing import Generic, TypeVar, Iterable
T = TypeVar('T')
class ResultList(list, Generic[T]):
def __init__(
self, iterable: Iterable[T] = ..., _startAt: int = ..., _maxResults: int = ..., _total: int = ..., _isLast: bool = ...
) -> None:
...
...
之后你可以使用如下输入:
from unittest import TestSuite
from res import ResultList
class SomeClass:
pass
def foo() -> 'ResultList[int]':
return ResultList(iterable=[SomeClass()])
if __name__ == '__main__':
a: 'ResultList[str]' = foo()
# pycharm will highlight "unexpected type error"
不要忘记在 pycharm 中连接存根,如 link
中所示
我正在尝试通过 typehinting.
指定返回的List
子类的元素
from typing import Union, List, Tuple, Optional, NewType
from jira import JIRA
from jira.client import ResultList
from jira.resources import Issue
def search(...) -> ResultList[Issue]: # using 'List[Issue]:' or 'ResultList:' as return type hint work
...
但是,我运行宁在这个错误:
TypeError: 'type' object is not subscriptable
我试过 NewType 的运气,但没能达到预期的 运行。当不指定子类 ResultList[Issue]
而是使用 List[Issue]
时,它可以工作。此外,当不通过简单地使用 ResultList
提及元素类型时,它是有效的。
附加信息:
你应该看看这些:
- https://docs.python.org/3/library/typing.html#typing.Generic
- https://mypy.readthedocs.io/en/stable/generics.html
- https://github.com/python/mypy/wiki/Creating-Stubs-For-Python-Modules
- https://www.jetbrains.com/help/pycharm/stubs.html
解释:
list和typing.List是完全不同的东西
为了使用泛型,你必须继承自 typing.Generic
如果您使用第三方库代码并且无法更改 ResultList,那么存根是一个解决方案
基本上,您需要在 *.pyi 文件中为 ResultList class 定义存根,如下所示:
from typing import Generic, TypeVar, Iterable
T = TypeVar('T')
class ResultList(list, Generic[T]):
def __init__(
self, iterable: Iterable[T] = ..., _startAt: int = ..., _maxResults: int = ..., _total: int = ..., _isLast: bool = ...
) -> None:
...
...
之后你可以使用如下输入:
from unittest import TestSuite
from res import ResultList
class SomeClass:
pass
def foo() -> 'ResultList[int]':
return ResultList(iterable=[SomeClass()])
if __name__ == '__main__':
a: 'ResultList[str]' = foo()
# pycharm will highlight "unexpected type error"
不要忘记在 pycharm 中连接存根,如 link
中所示