From cc8c3529fbf528da8eadadf2ff61389db9adf767 Mon Sep 17 00:00:00 2001 From: wisp3rwind <17089248+wisp3rwind@users.noreply.github.com> Date: Mon, 31 Jan 2022 21:26:32 +0100 Subject: [PATCH] confit: Improve deprecation warning Show the actual origin of the import statement, cf. #4024 --- beets/util/confit.py | 8 +++++++- test/test_util.py | 15 +++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/beets/util/confit.py b/beets/util/confit.py index dd912c444..927a9f087 100644 --- a/beets/util/confit.py +++ b/beets/util/confit.py @@ -16,7 +16,13 @@ import confuse import warnings -warnings.warn("beets.util.confit is deprecated; use confuse instead") +warnings.warn( + "beets.util.confit is deprecated; use confuse instead", + # Show the location of the `import confit` statement as the warning's + # source, rather than this file, such that the offending module can be + # identified easily. + stacklevel=2, +) # Import everything from the confuse module into this module. for key, value in confuse.__dict__.items(): diff --git a/test/test_util.py b/test/test_util.py index 32614ab72..fcaf9f5ce 100644 --- a/test/test_util.py +++ b/test/test_util.py @@ -182,6 +182,21 @@ class PathTruncationTest(_common.TestCase): self.assertEqual(p, 'abcde/f.ext') +class ConfitDeprecationTest(_common.TestCase): + def test_confit_deprecattion_warning_origin(self): + """Test that importing `confit` raises a warning. + + In addition, ensure that the warning originates from the actual + import statement, not the `confit` module. + """ + # See https://github.com/beetbox/beets/discussions/4024 + with self.assertWarns(UserWarning) as w: + import beets.util.confit # noqa: F401 + + self.assertIn(__file__, w.filename) + self.assertNotIn("confit.py", w.filename) + + def suite(): return unittest.TestLoader().loadTestsFromName(__name__)