refactor(utils): reduce code duplication

This commit is contained in:
Victor Usachev 2019-10-30 02:42:24 +03:00
parent f8d255b353
commit 9571669ca4

View file

@ -99,35 +99,27 @@ def renamed_argument(old_name: str, new_name: str, until_version: str, stackleve
"""
def decorator(func):
if asyncio.iscoroutinefunction(func):
is_coroutine = asyncio.iscoroutinefunction(func)
def _handling(kwargs):
routine_type = 'coroutine' if is_coroutine else 'function'
if old_name in kwargs:
warn_deprecated(f"In {routine_type} '{func.__name__}' argument '{old_name}' "
f"is renamed to '{new_name}' "
f"and will be removed in aiogram {until_version}",
stacklevel=stacklevel)
kwargs.update({new_name: kwargs.pop(old_name)})
return kwargs
if is_coroutine:
@functools.wraps(func)
async def wrapped(*args, **kwargs):
if old_name in kwargs:
warn_deprecated(f"In coroutine '{func.__name__}' argument '{old_name}' "
f"is renamed to '{new_name}' "
f"and will be removed in aiogram {until_version}",
stacklevel=stacklevel)
kwargs.update(
{
new_name: kwargs[old_name],
}
)
kwargs.pop(old_name)
kwargs = _handling(kwargs)
return await func(*args, **kwargs)
else:
@functools.wraps(func)
def wrapped(*args, **kwargs):
if old_name in kwargs:
warn_deprecated(f"In function `{func.__name__}` argument `{old_name}` "
f"is renamed to `{new_name}` "
f"and will be removed in aiogram {until_version}",
stacklevel=stacklevel)
kwargs.update(
{
new_name: kwargs[old_name],
}
)
kwargs.pop(old_name)
kwargs = _handling(kwargs)
return func(*args, **kwargs)
return wrapped