I have the code below, I understand that the function takes 2 integers, so (n : int, m : int)
, and it returns a tuple with integers of any length, therefore -> tuple[int,...]
:
I think this also correct for an empty tuple T : tuple[()]
since the tuple at some point in the function has only one value.
However I get this error
main.py:14: error: Incompatible types in assignment (expression has type "Tuple[int]", variable has type "Tuple[]")
What am I missing?
def vectorise(n : int, m : int) -> tuple[int,...]:
'''creates a tuple of element n repeated m times'''
T : tuple[()] = () #empty tuple
counter : int = 0
while counter < m:
T = T + (n,) #in Python (n,) is a tuple with one element n
counter+=1
#as a result, T can be of arbitrary length
return T
L = vectorise(100, 5)
T: tuple[int, ...] = ...
.T
needs to at least be equivalent (able to encompass) the intendedreturn
type. As you have hintedT
to be an EMPTY tuple, you cannot then return a different type - hence the type incompatibility error. You could instead add the OTHER hint for the return type e.g.tuple[int,...] | tuple[()]
to cater for both eventualities.