通用类型提示 python

Generic type hint python

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

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 代码说

所以无法识别类型,没有智能感知,也没有关于“名称”的建议

您可能需要将 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