Home > Software engineering >  Generic type hint python
Generic type hint python

Time:10-06

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"

CodePudding user response:

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
  • Related