简体   繁体   English

通用类型提示 python

[英]Generic type hint python

I have following code in python. Is it possible to get here intellisense in Visual Studio Code?我在 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

Visual Studio Code says视觉工作室代码说

so does not recognize the type and has no intellisense and no suggestion for "name"所以不识别类型,没有智能感知,也没有对“名称”的建议

You probably need to declare the FooBar class as Generic w.r.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