Klamsi
Klamsi

Reputation: 906

Generic type hint python

I have following code in python. Is it possible to get here intellisense in 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"

Upvotes: 2

Views: 1001

Answers (1)

rrobby86
rrobby86

Reputation: 1464

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

Upvotes: 4

Related Questions