That's a recursive function, so to make it fully general a recursive type can be used:
type CompoundInt = int | list[CompoundInt]
def fooify(x: CompoundInt) -> CompoundInt:
if isinstance(x, int):
return x * 2
else:
return list(map(fooify, x))
print(fooify([1, [3, 4, 5], [6, 7, 8], [[[4, 4, 4]]]]))
This uses the `type` keyword introduced in 3.12. Unfortunately Mypy doesn't support it yet :( so this workaround can be used instead: from __future__ import annotations
from typing import TYPE_CHECKING
if TYPE_CHECKING:
CompoundInt = int | list[CompoundInt]