Skip to content

Commit 95c672a

Browse files
jurrutiJose Urruticoechea
and
Jose Urruticoechea
authored
Support the deprecated_parameter decorator for asynchronous functions. (#6247)
* Support the deprecated_parameter decorator for asynchronous functions. * Add assert iscoroutine to async_deprecated_parameter test --------- Co-authored-by: Jose Urruticoechea <[email protected]>
1 parent 64e1a7f commit 95c672a

File tree

2 files changed

+59
-12
lines changed

2 files changed

+59
-12
lines changed

cirq-core/cirq/_compat.py

+23-11
Original file line numberDiff line numberDiff line change
@@ -400,23 +400,36 @@ def deprecated_parameter(
400400
_validate_deadline(deadline)
401401

402402
def decorator(func: Callable) -> Callable:
403+
def deprecation_warning():
404+
qualname = func.__qualname__ if func_name is None else func_name
405+
_warn_or_error(
406+
f'The {parameter_desc} parameter of {qualname} was '
407+
f'used but is deprecated.\n'
408+
f'It will be removed in cirq {deadline}.\n'
409+
f'{fix}\n'
410+
)
411+
403412
@functools.wraps(func)
404413
def decorated_func(*args, **kwargs) -> Any:
405414
if match(args, kwargs):
406415
if rewrite is not None:
407416
args, kwargs = rewrite(args, kwargs)
417+
deprecation_warning()
418+
return func(*args, **kwargs)
408419

409-
qualname = func.__qualname__ if func_name is None else func_name
410-
_warn_or_error(
411-
f'The {parameter_desc} parameter of {qualname} was '
412-
f'used but is deprecated.\n'
413-
f'It will be removed in cirq {deadline}.\n'
414-
f'{fix}\n'
415-
)
420+
@functools.wraps(func)
421+
async def async_decorated_func(*args, **kwargs) -> Any:
422+
if match(args, kwargs):
423+
if rewrite is not None:
424+
args, kwargs = rewrite(args, kwargs)
425+
deprecation_warning()
416426

417-
return func(*args, **kwargs)
427+
return await func(*args, **kwargs)
418428

419-
return decorated_func
429+
if inspect.iscoroutinefunction(func):
430+
return async_decorated_func
431+
else:
432+
return decorated_func
420433

421434
return decorator
422435

@@ -436,13 +449,12 @@ def deprecate_attributes(module_name: str, deprecated_attributes: Dict[str, Tupl
436449
will cause a warning for these deprecated attributes.
437450
"""
438451

439-
for (deadline, _) in deprecated_attributes.values():
452+
for deadline, _ in deprecated_attributes.values():
440453
_validate_deadline(deadline)
441454

442455
module = sys.modules[module_name]
443456

444457
class Wrapped(ModuleType):
445-
446458
__dict__ = module.__dict__
447459

448460
# Workaround for: https://github.com/python/mypy/issues/8083

cirq-core/cirq/_compat_test.py

+36-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import collections
1515
import dataclasses
1616
import importlib.metadata
17+
import inspect
1718
import logging
1819
import multiprocessing
1920
import os
@@ -26,7 +27,7 @@
2627
from importlib.machinery import ModuleSpec
2728
from unittest import mock
2829

29-
30+
import duet
3031
import numpy as np
3132
import pandas as pd
3233
import pytest
@@ -263,6 +264,40 @@ def f_with_badly_deprecated_param(new_count): # pragma: no cover
263264
# pylint: enable=unused-variable
264265

265266

267+
@duet.sync
268+
async def test_deprecated_parameter_async_function():
269+
@deprecated_parameter(
270+
deadline='v1.2',
271+
fix='Double it yourself.',
272+
func_name='test_func',
273+
parameter_desc='double_count',
274+
match=lambda args, kwargs: 'double_count' in kwargs,
275+
rewrite=lambda args, kwargs: (args, {'new_count': kwargs['double_count'] * 2}),
276+
)
277+
async def f(new_count):
278+
return new_count
279+
280+
assert inspect.iscoroutinefunction(f)
281+
282+
# Does not warn on usual use.
283+
with cirq.testing.assert_logs(count=0):
284+
assert await f(1) == 1
285+
assert await f(new_count=1) == 1
286+
287+
with cirq.testing.assert_deprecated(
288+
'_compat_test.py:',
289+
'double_count parameter of test_func was used',
290+
'will be removed in cirq v1.2',
291+
'Double it yourself.',
292+
deadline='v1.2',
293+
):
294+
# pylint: disable=unexpected-keyword-arg
295+
# pylint: disable=no-value-for-parameter
296+
assert await f(double_count=1) == 2
297+
# pylint: enable=no-value-for-parameter
298+
# pylint: enable=unexpected-keyword-arg
299+
300+
266301
def test_wrap_module():
267302
my_module = types.ModuleType('my_module', 'my doc string')
268303
my_module.foo = 'foo'

0 commit comments

Comments
 (0)