From: Gord Thompson Date: Fri, 24 Apr 2020 12:23:19 +0000 (-0600) Subject: Add warn_deprecated_limited feature X-Git-Tag: rel_1_3_17~9^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f9b9028dd09f9961bb98c6ef7ad6785143f7d520;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git Add warn_deprecated_limited feature Fixes: #5268 Change-Id: I2f976048af4f8d6dd03a14efa31d179bd7324ba6 (cherry picked from commit 7baf42883f177a6f666a1cb550f4357aa7606a25) --- diff --git a/lib/sqlalchemy/util/deprecations.py b/lib/sqlalchemy/util/deprecations.py index 9a9e95c39c..12a57884e4 100644 --- a/lib/sqlalchemy/util/deprecations.py +++ b/lib/sqlalchemy/util/deprecations.py @@ -12,6 +12,7 @@ import re import warnings from . import compat +from .langhelpers import _hash_limit_string from .langhelpers import decorator from .langhelpers import inject_docstring_text from .langhelpers import inject_param_text @@ -22,6 +23,16 @@ def warn_deprecated(msg, stacklevel=3): warnings.warn(msg, exc.SADeprecationWarning, stacklevel=stacklevel) +def warn_deprecated_limited(msg, args, stacklevel=3): + """Issue a deprecation warning with a parameterized string, + limiting the number of registrations. + + """ + if args: + msg = _hash_limit_string(msg, 10, args) + warnings.warn(msg, exc.SADeprecationWarning, stacklevel) + + def warn_pending_deprecation(msg, stacklevel=3): warnings.warn(msg, exc.SAPendingDeprecationWarning, stacklevel=stacklevel) diff --git a/test/base/test_warnings.py b/test/base/test_warnings.py new file mode 100644 index 0000000000..c8807df09a --- /dev/null +++ b/test/base/test_warnings.py @@ -0,0 +1,36 @@ +from sqlalchemy.testing import eq_ +from sqlalchemy.testing import expect_deprecated +from sqlalchemy.testing import fixtures +from sqlalchemy.util.deprecations import warn_deprecated_limited +from sqlalchemy.util.langhelpers import _hash_limit_string + + +class WarnDeprecatedLimitedTest(fixtures.TestBase): + __backend__ = False + + def test_warn_deprecated_limited_text(self): + with expect_deprecated("foo has been deprecated"): + warn_deprecated_limited( + "%s has been deprecated [%d]", ("foo", 1), "1.3" + ) + + def test_warn_deprecated_limited_cap(self): + """ warn_deprecated_limited() and warn_limited() use + _hash_limit_string + + actually just verifying that _hash_limit_string works as expected + """ + occurrences = 100 + cap = 10 + + printouts = set() + messages = set() + for i in range(occurrences): + message = _hash_limit_string( + "this is a unique message: %d", cap, (i,) + ) + printouts.add(str(message)) + messages.add(message) + + eq_(len(printouts), occurrences) + eq_(len(messages), cap)