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

How to write type hints when you change type of a variable?

  • Thread starter Thread starter Barzi2001
  • Start date Start date
B

Barzi2001

Guest
Say that I have

Code:
def foo(x: Union[str, list[str]], y: Union[str, list[str]]) -> tuple[list[str], list[str]]:
     
    x = cast(Union[list[str], tuple[list[str], ...]], str2list(x))  
    y = cast(Union[list[str], tuple[list[str], ...]], str2list(y))  
    
    return x,y
     
def str2list(*args: Union[str, list[str]]) -> Union[list[str], tuple[list[str], ...]]:
    vals = []
    for x in args:
        if not isinstance(x, list):
            x = [x]
        vals.append(x)
    if len(args) == 1:
        return vals[0]
    else:
        return tuple(vals)
    

x = 'My name is x'
y = ['First element', 'Second element']

z = str2list(x,y)

As you see, there are two cast calls in foo, but still mypy complains that

Code:
error: Incompatible types in assignment (expression has type "Union[List[str], Tuple[List[str], ...]]", variable has type "Union[str, List[str]]")  [assignment]

A workaround could be to define new variables and adjust the return type of foo as it follows

Code:
def foo(
    x: Union[str, list[str]], y: Union[str, list[str]]
) -> tuple[
    Union[list[str], tuple[list[str], ...]],
    Union[list[str], tuple[list[str], ...]],
]:

    xl = cast(Union[list[str], tuple[list[str], ...]], str2list(x))
    yl = cast(Union[list[str], tuple[list[str], ...]], str2list(y))

    return xl, yl

But I would prefer to overwrite x and y rather than defining two new variables xl and yl.
<p>Say that I have</p>
<pre><code>def foo(x: Union[str, list[str]], y: Union[str, list[str]]) -> tuple[list[str], list[str]]:

x = cast(Union[list[str], tuple[list[str], ...]], str2list(x))
y = cast(Union[list[str], tuple[list[str], ...]], str2list(y))

return x,y

def str2list(*args: Union[str, list[str]]) -> Union[list[str], tuple[list[str], ...]]:
vals = []
for x in args:
if not isinstance(x, list):
x = [x]
vals.append(x)
if len(args) == 1:
return vals[0]
else:
return tuple(vals)


x = 'My name is x'
y = ['First element', 'Second element']

z = str2list(x,y)
</code></pre>
<p>As you see, there are two <code>cast</code> calls in <code>foo</code>, but still <code>mypy</code> complains that</p>
<pre><code>error: Incompatible types in assignment (expression has type "Union[List[str], Tuple[List[str], ...]]", variable has type "Union[str, List[str]]") [assignment]
</code></pre>
<p>A workaround could be to define new variables and adjust the return type of foo as it follows</p>
<pre><code>def foo(
x: Union[str, list[str]], y: Union[str, list[str]]
) -> tuple[
Union[list[str], tuple[list[str], ...]],
Union[list[str], tuple[list[str], ...]],
]:

xl = cast(Union[list[str], tuple[list[str], ...]], str2list(x))
yl = cast(Union[list[str], tuple[list[str], ...]], str2list(y))

return xl, yl
</code></pre>
<p>But I would prefer to overwrite <code>x</code> and <code>y</code> rather than defining two new variables <code>xl</code> and <code>yl</code>.</p>
 
Top