Ejemplo n.º 1
0
    def test_member_deprecation(self):
        from deprecation_example import ExampleClass

        def base_deprecation():
            warnings.warn(
                "Non-drake warning", category=DeprecationWarning, stacklevel=2)

        # At this point, no other deprecations should have been thrown, so we
        # will test with the default `once` filter.
        with warnings.catch_warnings(record=True) as w:
            if six.PY3:
                # Recreate warning environment.
                warnings.simplefilter('ignore', DeprecationWarning)
                warnings.simplefilter('once', DrakeDeprecationWarning)
            # TODO(eric.cousineau): Also different behavior here...
            # Is `unittest` setting a non-standard warning filter???
            base_deprecation()  # Should not appear.
            obj = ExampleClass()
            # Call each deprecated method / property repeatedly; it should only
            # warn once per unique line of source code.
            # - Method.
            for _ in range(3):
                method = ExampleClass.deprecated_method
                self.assertEqual(obj.deprecated_method(), 1)
                # The next line will not show a warning.
                self.assertEqual(method(obj), 1)
            self.assertEqual(method.__doc__, ExampleClass.doc_method)
            # - Property.
            for _ in range(3):
                prop = ExampleClass.deprecated_prop
                self.assertEqual(obj.deprecated_prop, 2)
                # The next line will not show a warning.
                self.assertEqual(prop.__get__(obj), 2)
            self.assertEqual(prop.__doc__, ExampleClass.doc_prop)
            # Check warnings.
            self.assertEqual(len(w), 2, "\n".join(map(str, w)))
            self._check_warning(w[0], ExampleClass.message_method)
            self._check_warning(w[1], ExampleClass.message_prop)

        # Because `once` uses a (somehow opaque) registry (not accessible via
        # `warnings.once*registry` or `_warnings.once_registry`), we must
        # change the filter to test behavior. `reload`ing `warnings` and/or
        # `_warnings` also does not work.
        # See caveat here with `catch_warnings`:
        # https://docs.python.org/2/library/warnings.html#testing-warnings

        # Enable deprecation warnings as well.
        with warnings.catch_warnings(record=True) as w:
            # N.B. This also overrides `DrakeDeprecationWarning` settings
            # because of ordering. We can redefine a filter for
            # `DrakeDeprecationWarning` if so desired.
            warnings.simplefilter("default", DeprecationWarning)
            base_deprecation()
            method = ExampleClass.deprecated_method
            self.assertEqual(len(w), 2)
            self._check_warning(
                w[0], "Non-drake warning", type=DeprecationWarning)
            self._check_warning(w[1], ExampleClass.message_method)

        # Edit the following flags to manually inspect the warnings generated.
        show_warnings = False
        if show_warnings:
            include_base_deprecations = False
            if include_base_deprecations:
                warnings.simplefilter("default", DeprecationWarning)
            else:
                # See above notes for why we have to set this.
                warnings.simplefilter("default", DrakeDeprecationWarning)
            for _ in range(3):
                base_deprecation()
                method = ExampleClass.deprecated_method
                method_extra = ExampleClass.deprecated_method
                prop = ExampleClass.deprecated_prop
                prop_extra = ExampleClass.deprecated_prop
            # N.B. `help(<module>)` is super verbose.
            print("Help text:\n{}".format(
                pydoc.getdoc(pydrake.util.deprecation)))
            # Manually set this back to `once`.
            warnings.simplefilter("ignored", DeprecationWarning)
            warnings.simplefilter("once", DrakeDeprecationWarning)
Ejemplo n.º 2
0
    def test_member_deprecation(self):
        from deprecation_example import ExampleClass

        def base_deprecation():
            warnings.warn("Non-drake warning",
                          category=DeprecationWarning,
                          stacklevel=2)

        # At this point, no other deprecations should have been thrown, so we
        # will test with the default `once` filter.
        with warnings.catch_warnings(record=True) as w:
            base_deprecation()  # Should not appear.
            obj = ExampleClass()
            # Call each deprecated method / propery repeatedly; it should only
            # warn once per unique line of source code.
            # - Method.
            for _ in range(3):
                method = ExampleClass.deprecated_method
                self.assertEqual(obj.deprecated_method(), 1)
                # The next line will not show a warning.
                self.assertEqual(method(obj), 1)
            self.assertEqual(method.__doc__, ExampleClass.doc_method)
            # - Property.
            for _ in range(3):
                prop = ExampleClass.deprecated_prop
                self.assertEqual(obj.deprecated_prop, 2)
                # The next line will not show a warning.
                self.assertEqual(prop.__get__(obj), 2)
            self.assertEqual(prop.__doc__, ExampleClass.doc_prop)
            # Check warnings.
            self.assertEqual(len(w), 2)
            self._check_warning(w[0], ExampleClass.message_method)
            self._check_warning(w[1], ExampleClass.message_prop)

        # Because `once` uses a (somehow opaque) registry (not accessible via
        # `warnings.once*registry` or `_warnings.once_registry`), we must
        # change the filter to test behavior. `reload`ing `warnings` and/or
        # `_warnings` also does not work.
        # See caveat here with `catch_warnings`:
        # https://docs.python.org/2/library/warnings.html#testing-warnings

        # Enable deprecation warnings as well.
        with warnings.catch_warnings(record=True) as w:
            # N.B. This also overrides `DrakeDeprecationWarning` settings
            # because of ordering. We can redefine a filter for
            # `DrakeDeprecationWarning` if so desired.
            warnings.simplefilter("default", DeprecationWarning)
            base_deprecation()
            method = ExampleClass.deprecated_method
            self.assertEqual(len(w), 2)
            self._check_warning(w[0],
                                "Non-drake warning",
                                type=DeprecationWarning)
            self._check_warning(w[1], ExampleClass.message_method)

        # Edit the following flags to manually inspect the warnings generated.
        show_warnings = False
        if show_warnings:
            include_base_deprecations = False
            if include_base_deprecations:
                warnings.simplefilter("default", DeprecationWarning)
            else:
                # See above notes for why we have to set this.
                warnings.simplefilter("default", DrakeDeprecationWarning)
            for _ in range(3):
                base_deprecation()
                method = ExampleClass.deprecated_method
                method_extra = ExampleClass.deprecated_method
                prop = ExampleClass.deprecated_prop
                prop_extra = ExampleClass.deprecated_prop
            # N.B. `help(<module>)` is super verbose.
            print("Help text:\n{}".format(
                pydoc.getdoc(pydrake.util.deprecation)))
            # Manually set this back to `once`.
            warnings.simplefilter("ignored", DeprecationWarning)
            warnings.simplefilter("once", DrakeDeprecationWarning)
Ejemplo n.º 3
0
    def test_member_deprecation(self):
        """
        Tests low-level deprecation API for members.

        Please see `deprecation_utility_test.py` for a unittest on
        higher-level API.
        """
        from deprecation_example import ExampleClass

        def base_deprecation():
            warnings.warn("Non-drake warning",
                          category=DeprecationWarning,
                          stacklevel=2)

        # At this point, no other deprecations should have been thrown, so we
        # will test with the default `once` filter.
        with warnings.catch_warnings(record=True) as w:
            # Recreate warning environment.
            warnings.simplefilter('ignore', DeprecationWarning)
            warnings.simplefilter('once', DrakeDeprecationWarning)
            # TODO(eric.cousineau): Also different behavior here...
            # Is `unittest` setting a non-standard warning filter???
            base_deprecation()  # Should not appear.
            obj = ExampleClass()
            # Call each deprecated method / property repeatedly; it should only
            # warn once per unique line of source code.
            # - Method.
            for _ in range(3):
                method = ExampleClass.deprecated_method
                self.assertEqual(obj.deprecated_method(), 1)
                # The next line will not show a warning.
                self.assertEqual(method(obj), 1)
            self.assertEqual(method.__doc__, ExampleClass.doc_method)
            # - Property.
            for _ in range(3):
                prop = ExampleClass.deprecated_prop
                self.assertEqual(obj.deprecated_prop, 2)
                # The next line will not show a warning.
                self.assertEqual(prop.__get__(obj), 2)
            self.assertEqual(prop.__doc__, ExampleClass.doc_prop)
            # Check warnings.
            self.assertEqual(len(w), 2, "\n".join(map(str, w)))
            self._check_warning(w[0], ExampleClass.message_method)
            self._check_warning(w[1], ExampleClass.message_prop)

        # Because `once` uses a (somehow opaque) registry (not accessible via
        # `warnings.once*registry` or `_warnings.once_registry`), we must
        # change the filter to test behavior. `reload`ing `warnings` and/or
        # `_warnings` also does not work.
        # See caveat here with `catch_warnings`:
        # https://docs.python.org/2/library/warnings.html#testing-warnings

        # Enable deprecation warnings as well.
        with warnings.catch_warnings(record=True) as w:
            # N.B. This also overrides `DrakeDeprecationWarning` settings
            # because of ordering. We can redefine a filter for
            # `DrakeDeprecationWarning` if so desired.
            warnings.simplefilter("default", DeprecationWarning)
            base_deprecation()
            method = ExampleClass.deprecated_method
            self.assertEqual(len(w), 2)
            self.assertEqual(w[0].category, DeprecationWarning)
            self.assertEqual(str(w[0].message), "Non-drake warning")
            self._check_warning(w[1], ExampleClass.message_method)

        # Edit the following flags to manually inspect the warnings generated.
        show_warnings = False
        if show_warnings:
            include_base_deprecations = False
            if include_base_deprecations:
                warnings.simplefilter("default", DeprecationWarning)
            else:
                # See above notes for why we have to set this.
                warnings.simplefilter("default", DrakeDeprecationWarning)
            for _ in range(3):
                base_deprecation()
                method = ExampleClass.deprecated_method
                method_extra = ExampleClass.deprecated_method
                prop = ExampleClass.deprecated_prop
                prop_extra = ExampleClass.deprecated_prop
            # Manually set this back to `once`.
            warnings.simplefilter("ignore", DeprecationWarning)
            warnings.simplefilter("once", DrakeDeprecationWarning)