I have this:
from typing import Tuple
import random
a = random.randint(100) # some random number
def foo(a: int) -> Tuple:
b = []
for _ in random.randint(0, 10):
b.append(random.randint(-5, 5) # adding random numbers to b
return a, *b
And I want to write return type for this function, but I don't now how to do this properly:
I tried this:
from typing import Tuple
import random
a = random.randint(100) # some random number. It doesn't matter
def foo(a: int) -> Tuple[int, *Tuple[int, ...]]:
b = []
for _ in random.randint(0, 10):
b.append(random.randint(-5, 5) # adding random numbers to b
return a, *b
Pycharm with mypy says: foo(a: int) -> Tuple[int, Any]
But I need the function to return the type of variable that was passed to it
In a real project, it takes a generic and returns a tuple with the object and the unpacked list in it for readability
Real function:
...
def get_entities_with(self, *component_types):
for entity in self.entities.values():
require_components = [component for component in entity.components if type(component) in component_types]
if len(require_components) == len(component_types):
yield entity, *require_components
...
.pyi File:
T = TypeVar("T")
...
def get_entities_with(self, *components:Type[T]) -> Generator[Entity, *Tuple[T, ...]]: ...