OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

`TypeVar`: parametric type as a value for `bound` parameter

  • Thread starter Thread starter nme
  • Start date Start date
N

nme

Guest
I'd like to implement a generic class like this:

Code:
S = TypeVar("S")
T = TypeVar("T", bound=OtherParametrizedClass)

class MyClass(Generic[T[S]]):
    def some_method(param: S) -> None:
        pass

I've already tried the following:

Code:
S = TypeVar("S")
T = TypeVar("T", bound=OtherParametrizedClass)

class MyClass(Generic[S, T[S]]):
    def some_method(param: S) -> None:
        pass
    def other_method(param: T) -> None:
        pass

It works as expected with MyPy. However, when Python interpreter runs this code, it gives me the following error:

Code:
TypeError: 'TypeVar' object is not subscriptable.

As I found, this means that TypeVar has no [] operator implemented.

Does anyone have an idea on how to obtain the solution satisfying both mypy and Python interpreter?

EDIT: I have also tried the following:

Code:
S = TypeVar("S")
T = TypeVar("T", bound=OtherParametrizedClass[S])

class MyClass(Generic[T]):
    def some_method(param: S) -> None:
        pass
    def other_method(param: T) -> None:
        pass

Python interpreter doesn't give any errors/warnings. However mypy is complaining about the second line:

Code:
Invalid type "S"
<p>I'd like to implement a generic class like this:</p>

<pre class="lang-py prettyprint-override"><code>S = TypeVar("S")
T = TypeVar("T", bound=OtherParametrizedClass)

class MyClass(Generic[T]):
def some_method(param: S) -> None:
pass
</code></pre>

<p>I've already tried the following:</p>

<pre class="lang-py prettyprint-override"><code>S = TypeVar("S")
T = TypeVar("T", bound=OtherParametrizedClass)

class MyClass(Generic[S, T]):
def some_method(param: S) -> None:
pass
def other_method(param: T) -> None:
pass
</code></pre>

<p>It works as expected with MyPy. However, when Python interpreter runs this code, it gives me the following error:</p>

<pre><code>TypeError: 'TypeVar' object is not subscriptable.
</code></pre>

<p>As I found, this means that <code>TypeVar</code> has no <code>[]</code> operator implemented.</p>

<p>Does anyone have an idea on how to obtain the solution satisfying both mypy and Python interpreter?</p>

<p>EDIT:
I have also tried the following:
</p>

<pre><code>S = TypeVar("S")
T = TypeVar("T", bound=OtherParametrizedClass)

class MyClass(Generic[T]):
def some_method(param: S) -> None:
pass
def other_method(param: T) -> None:
pass
</code></pre>

<p>Python interpreter doesn't give any errors/warnings. However mypy is complaining about the second line:</p>

<pre><code>Invalid type "S"
</code></pre>
 

Latest posts

Top