繁体   English   中英

通用类型提示 python

[英]Generic type hint python

我在 python 中有以下代码。是否可以在 Visual Studio Code 中获得智能感知?

from typing import TypeVar

T = TypeVar('T')

# Example class for a possible type T
class MyT:
    def name():
        return "I'm T"

class FooBar:
    def __init__(self, something: T) -> None:
        self.its_t = something

foo_bar = FooBar(MyT())
intellisense_test = foo_bar.its_t

视觉工作室代码说

所以不识别类型,没有智能感知,也没有对“名称”的建议

您可能需要将FooBar class 声明为Generic w.r.t。 T

from typing import Generic, TypeVar

T = TypeVar('T')

class FooBar(Generic[T]):
    def __init__(self, something: T) -> None:
        self.its_t = something

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM