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

Python list[T] not assignable to list[T | None]

  • Thread starter Thread starter zmbq
  • Start date Start date
Z

zmbq

Guest
I have a type T, a namedtuple actually:

Code:
from collections import namedtuple

T = namedtuple('T', ('a', 'b'))

I have a function that accepts a list[T | None] and a list:

Code:
def func(arg: list[T | None]):
    ...


l = [T(1, 2), T(2, 3)]
func(l)  # Pylance error

l is of type list[T].

When I pass a l to the function I get an error from Pylance, that a list[T] is incompatible with list[T | None] because T cannot be assigned to T | None.

Aside from manually specifying my list[T] is actually a list[T | None], what can I do to make this work without an error? Of course at runtime everything runs as expected.
<p>I have a type <code>T</code>, a <code>namedtuple</code> actually:</p>
<pre class="lang-py prettyprint-override"><code>from collections import namedtuple

T = namedtuple('T', ('a', 'b'))
</code></pre>
<p>I have a function that accepts a <code>list[T | None]</code> and a list:</p>
<pre class="lang-py prettyprint-override"><code>def func(arg: list[T | None]):
...


l = [T(1, 2), T(2, 3)]
func(l) # Pylance error
</code></pre>
<p><code>l</code> is of type <code>list[T]</code>.</p>
<p>When I pass a <code>l</code> to the function I get an error from Pylance, that a <code>list[T]</code> is incompatible with <code>list[T | None]</code> because <code>T cannot be assigned to T | None</code>.</p>
<p>Aside from manually specifying my <code>list[T]</code> is actually a <code>list[T | None]</code>, what can I do to make this work without an error? Of course at runtime everything runs as expected.</p>
 

Latest posts

Top