Skip to content

Instantly share code, notes, and snippets.

@nekufa
Last active September 4, 2023 10:16
Show Gist options
  • Save nekufa/ba1806696db733bd5a9b6ecdc99f670b to your computer and use it in GitHub Desktop.
Save nekufa/ba1806696db733bd5a9b6ecdc99f670b to your computer and use it in GitHub Desktop.
from types import GenericAlias
from typing import get_type_hints
async def tester(param: list[str]):
return [
x + '!' for x in param
]
async def another_tester(param: str):
return param + '!'
todo = ['a', 'b', 'c', 'd']
for task in [tester, another_tester]:
params_class = list(get_type_hints(task).values())[0]
batch_handler = type(params_class) is GenericAlias
if batch_handler:
task(todo)
else:
for x in todo:
task(x)
@ice1x
Copy link

ice1x commented Sep 4, 2023

async def another_tester(param: str) -> str:
    return param + '!'


async def tester(param: str | list[str]) -> list[str]:
    if isinstance(param, str):
        return [(await another_tester(param))]
    return [
        (await another_tester(param)) for x in param
    ]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment