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__)