commit: b8a182852d72e49d4342c0cd4a5855be2badf19e
Author: Brian Harring <ferringb <AT> gmail <DOT> com>
AuthorDate: Mon Oct 27 17:58:00 2025 +0000
Commit: Arthur Zamarin <arthurzam <AT> gentoo <DOT> org>
CommitDate: Mon Oct 27 21:28:03 2025 +0000
URL:
https://gitweb.gentoo.org/proj/pkgcore/snakeoil.git/commit/?id=b8a18285
chore: add deprecation suppression, _util -> deprecation
Signed-off-by: Brian Harring <ferringb <AT> gmail.com>
src/snakeoil/_util.py | 16 ----------------
src/snakeoil/deprecation.py | 39 +++++++++++++++++++++++++++++++++++++++
src/snakeoil/klass/__init__.py | 2 +-
src/snakeoil/klass/deprecated.py | 5 +++--
src/snakeoil/klass/properties.py | 2 +-
tests/klass/test_init.py | 31 ++++++++++++++++++++-----------
6 files changed, 64 insertions(+), 31 deletions(-)
diff --git a/src/snakeoil/_util.py b/src/snakeoil/_util.py
deleted file mode 100644
index cdac209..0000000
--- a/src/snakeoil/_util.py
+++ /dev/null
@@ -1,16 +0,0 @@
-__all__ = ("deprecated",)
-
-try:
- from warnings import deprecated # pyright: ignore[reportAssignmentType]
-except ImportError:
-
- def deprecated(_message):
- """
- This is a noop; deprecation warnings are disabled for pre python
- 3.13.
- """
-
- def f(thing):
- return thing
-
- return f
diff --git a/src/snakeoil/deprecation.py b/src/snakeoil/deprecation.py
new file mode 100644
index 0000000..e0b85a9
--- /dev/null
+++ b/src/snakeoil/deprecation.py
@@ -0,0 +1,39 @@
+__all__ = ("deprecated",)
+
+import warnings
+from contextlib import contextmanager
+
+_import_failed = False
+try:
+ from warnings import deprecated # pyright: ignore[reportAssignmentType]
+except ImportError:
+ _import_failed = True
+
+ def deprecated(_message):
+ """
+ This is a noop; deprecation warnings are disabled for pre python
+ 3.13.
+ """
+
+ def f(thing):
+ return thing
+
+ return f
+
+
+@contextmanager
+def suppress_deprecation_warning():
+ """
+ Used for suppressing all deprecation warnings beneath this
+
+ Use this for known deprecated code that is already addressed, but
+ just waiting to die. Deprecated code calling deprecated code,
specifically.
+ """
+ if _import_failed:
+ # noop.
+ yield
+ else:
+ # see
https://docs.python.org/3/library/warnings.html#temporarily-suppressing-warnings
+ with warnings.catch_warnings():
+ warnings.simplefilter(action="ignore", category=DeprecationWarning)
+ yield
diff --git a/src/snakeoil/klass/__init__.py b/src/snakeoil/klass/__init__.py
index 5fa24a2..ca9ae9f 100644
--- a/src/snakeoil/klass/__init__.py
+++ b/src/snakeoil/klass/__init__.py
@@ -42,7 +42,7 @@ from functools import partial, wraps
from importlib import import_module
from operator import attrgetter
-from snakeoil._util import deprecated as warn_deprecated
+from snakeoil.deprecation import deprecated as warn_deprecated
from ..caching import WeakInstMeta
from .deprecated import ImmutableInstance, immutable_instance,
inject_immutable_instance
diff --git a/src/snakeoil/klass/deprecated.py b/src/snakeoil/klass/deprecated.py
index a9503cb..cb7811a 100644
--- a/src/snakeoil/klass/deprecated.py
+++ b/src/snakeoil/klass/deprecated.py
@@ -4,7 +4,7 @@ __all__ = ("immutable_instance", "inject_immutable_instance",
"ImmutableInstance
import typing
-from snakeoil._util import deprecated
+from snakeoil.deprecation import deprecated, suppress_deprecation_warning
@deprecated("Use snakeoil.klass.meta.Immutable* metaclasses instead")
@@ -16,7 +16,8 @@ def immutable_instance(
It still is possible to do object.__setattr__ to get around it during
initialization, but usage of this class effectively prevents accidental
modification, instead requiring explicit modification."""
- inject_immutable_instance(scope)
+ with suppress_deprecation_warning():
+ inject_immutable_instance(scope)
return real_type(name, bases, scope)
diff --git a/src/snakeoil/klass/properties.py b/src/snakeoil/klass/properties.py
index 1da6042..0ebc373 100644
--- a/src/snakeoil/klass/properties.py
+++ b/src/snakeoil/klass/properties.py
@@ -12,7 +12,7 @@ __all__ = (
import operator
import typing
-from snakeoil._util import deprecated
+from snakeoil.deprecation import deprecated
from ..currying import post_curry
diff --git a/tests/klass/test_init.py b/tests/klass/test_init.py
index 529fefb..bd5e6ce 100644
--- a/tests/klass/test_init.py
+++ b/tests/klass/test_init.py
@@ -141,15 +141,23 @@ class Test_get:
class Test_chained_getter:
- kls = klass.chained_getter
+ @staticmethod
+ def kls(*args, **kwargs):
+ with pytest.deprecated_call():
+ kwargs.setdefault("disable_inst_caching", True)
+ return klass.chained_getter(*args, **kwargs)
def test_hash(self):
assert hash(self.kls("foon")) == hash("foon")
assert hash(self.kls("foon.dar")) == hash("foon.dar")
def test_caching(self):
- l = [id(self.kls("fa2341f%s" % x)) for x in "abcdefghij"] # noqa: E741
- assert id(self.kls("fa2341fa")) == l[0]
+ # since it caches, it'll only trigger the warning the *first* time,
thus
+ # invoke this ourselves directly
+ with pytest.deprecated_call():
+ assert klass.chained_getter(
+ "asdf", disable_inst_caching=False
+ ) is klass.chained_getter("asdf", disable_inst_caching=False)
def test_eq(self):
assert self.kls("asdf", disable_inst_caching=True) == self.kls(
@@ -170,14 +178,13 @@ class Test_chained_getter:
d = {}
m = maze(d)
- f = self.kls
- assert f("foon")(m) == m
+ assert self.kls("foon")(m) == m
d["foon"] = 1
- assert f("foon")(m) == 1
- assert f("dar.foon")(m) == 1
- assert f(".".join(["blah"] * 10))(m) == m
+ assert self.kls("foon")(m) == 1
+ assert self.kls("dar.foon")(m) == 1
+ assert self.kls(".".join(["blah"] * 10))(m) == m
with pytest.raises(AttributeError):
- f("foon.dar")(m)
+ self.kls("foon.dar")(m)
class Test_jit_attr:
@@ -504,11 +511,13 @@ class Test_reflective_hash:
class TestImmutableInstance:
def test_metaclass(self):
- self.common_test(lambda x: x, metaclass=klass.immutable_instance)
+ with pytest.deprecated_call():
+ self.common_test(lambda x: x, metaclass=klass.immutable_instance)
def test_injection(self):
def f(scope):
- klass.inject_immutable_instance(scope)
+ with pytest.deprecated_call():
+ klass.inject_immutable_instance(scope)
self.common_test(f)