Esempio n. 1
0
def assert_deprecated(*msgs: str, deadline: str, count: Optional[int] = 1):
    """Allows deprecated functions, classes, decorators in tests.

    It acts as a contextmanager that can be used in with statements:
    >>> with assert_deprecated("use cirq.x instead", deadline="v0.9"):
    >>>     # do something deprecated

    Args:
        msgs: messages that should match the warnings captured
        deadline: the expected deadline the feature will be deprecated by. Has to follow the format
            vX.Y (minor versions only)
        count: if None count of messages is not asserted, otherwise the number of deprecation
            messages have to equal count.
    """

    os.environ[ALLOW_DEPRECATION_IN_TEST] = 'True'
    try:
        with assert_logs(
                *(msgs + (deadline, )),
                min_level=logging.WARNING,
                max_level=logging.WARNING,
                count=count,
        ):
            yield True
    finally:
        try:
            del os.environ[ALLOW_DEPRECATION_IN_TEST]
        except:
            # this is only for nested deprecation checks
            pass
Esempio n. 2
0
 def __enter__(self):
     self.orig_exist, self.orig_value = (
         ALLOW_DEPRECATION_IN_TEST in os.environ,
         os.environ.get(ALLOW_DEPRECATION_IN_TEST, None),
     )
     os.environ[ALLOW_DEPRECATION_IN_TEST] = 'True'
     self.assert_logs = assert_logs(
         *(msgs + (deadline, )),
         min_level=logging.WARNING,
         max_level=logging.WARNING,
         count=count,
     )
     self.assert_logs.__enter__()
Esempio n. 3
0
def assert_deprecated(*msgs: str, deadline: str, allow_multiple_warnings: bool = False):
    """Allows deprecated functions, classes, decorators in tests.

    It acts as a contextmanager that can be used in with statements:
    >>> with assert_deprecated("use cirq.x instead", deadline="v0.9"):
    >>>     # do something deprecated

    Args:
        msgs: messages that should match the warnings captured
        deadline: the expected deadline the feature will be deprecated by. Has to follow the format
            vX.Y (minor versions only)
        allow_multiple_warnings: if True, multiple warnings are accepted. Typically this should not
            be used, by default it's False.
    """

    os.environ[ALLOW_DEPRECATION_IN_TEST] = 'True'
    try:
        with assert_logs(*(msgs + (deadline,)), count=None if allow_multiple_warnings else 1):
            yield True
    finally:
        del os.environ[ALLOW_DEPRECATION_IN_TEST]
Esempio n. 4
0
def assert_deprecated(*msgs: str,
                      deadline: str,
                      count: Optional[int] = 1) -> Iterator[None]:
    """Allows deprecated functions, classes, decorators in tests.

    It acts as a contextmanager that can be used in with statements:
    >>> with assert_deprecated("use cirq.x instead", deadline="v0.9"):
    >>>     # do something deprecated

    Args:
        msgs: messages that should match the warnings captured
        deadline: the expected deadline the feature will be deprecated by. Has to follow the format
            vX.Y (minor versions only)
        count: if None count of messages is not asserted, otherwise the number of deprecation
            messages have to equal count.
    """
    # Avoid circular import.
    from cirq.testing import assert_logs

    orig_exist = ALLOW_DEPRECATION_IN_TEST in os.environ
    orig_value = os.environ.get(ALLOW_DEPRECATION_IN_TEST, None)
    os.environ[ALLOW_DEPRECATION_IN_TEST] = 'True'
    try:
        with assert_logs(
                *msgs,
                deadline,
                min_level=logging.WARNING,
                max_level=logging.WARNING,
                count=count,
        ):
            yield
    finally:
        if orig_exist:
            # mypy can't resolve that orig_exist ensures that orig_value
            # of type Optional[str] can't be None
            os.environ[ALLOW_DEPRECATION_IN_TEST] = orig_value  # type: ignore
        else:
            del os.environ[ALLOW_DEPRECATION_IN_TEST]