Просмотр исходного кода

Fixes #23498: Track warned templates instead of relying on the warnings registry

Changing the warning filters anywhere in the process clears the warnings registry, so the default filter repeated the stored finalize warning after any catch_warnings() or filterwarnings() call. A module-level set of warned templates keeps it to once per template per process.
Jason Novinger 1 день назад
Родитель
Сommit
7514ff398a
2 измененных файлов с 24 добавлено и 26 удалено
  1. 12 7
      netbox/extras/models/mixins.py
  2. 12 19
      netbox/extras/tests/test_models.py

+ 12 - 7
netbox/extras/models/mixins.py

@@ -27,6 +27,9 @@ __all__ = (
 
 logger = logging.getLogger(__name__)
 
+# (model name, pk) of templates that have already emitted the stored 'finalize' deprecation warning (#23498)
+_stored_finalize_warned = set()
+
 
 class CustomStoragesLoader(importlib.abc.Loader):
     """
@@ -254,13 +257,15 @@ class RenderTemplateMixin(models.Model):
                 type(self).__name__, self.pk,
             )
             return {key: value for key, value in params.items() if key != 'finalize'}
-        # The pk makes each template's text unique, so the default filter warns once per template.
-        # Do not set stacklevel: a caller's line would repeat the warning for each caller.
-        warnings.warn(
-            f'The "finalize" environment parameter stored on {type(self).__name__} (pk={self.pk}) is deprecated and '
-            'will be removed in NetBox v5.0. Move its logic into the template code.',
-            FutureWarning,
-        )
+        # Track warned templates here: the warnings registry resets whenever any code changes the warning filters.
+        warned_key = (type(self).__name__, self.pk)
+        if warned_key not in _stored_finalize_warned:
+            _stored_finalize_warned.add(warned_key)
+            warnings.warn(
+                f'The "finalize" environment parameter stored on {type(self).__name__} (pk={self.pk}) is deprecated '
+                'and will be removed in NetBox v5.0. Move its logic into the template code.',
+                FutureWarning,
+            )
         return {**params, 'finalize': stored_finalize}
 
     def get_environment_params(self):

+ 12 - 19
netbox/extras/tests/test_models.py

@@ -1718,6 +1718,10 @@ class StoredFinalizeDeprecationWarningTestCase(TestCase):
     TEMPLATE_CODE = '{{ "netbox" }}'
     SHARED_PK = 9001
 
+    def setUp(self):
+        # Start each test with no templates marked as already warned
+        self.enterContext(patch('extras.models.mixins._stored_finalize_warned', set()))
+
     def _create(self, model, name, **kwargs):
         # create() does not call clean(). This matches a value stored before clean() blocked it.
         return model.objects.create(name=name, template_code=self.TEMPLATE_CODE, **kwargs)
@@ -1727,10 +1731,10 @@ class StoredFinalizeDeprecationWarningTestCase(TestCase):
 
     def _render_recording(self, *templates):
         """
-        Use the default filter, not 'always', so the once-per-template dedupe is exercised.
+        Use the 'always' filter so any dedupe comes from the template mixin, not from the warnings registry.
         """
         with warnings.catch_warnings(record=True) as caught:
-            warnings.simplefilter('default')
+            warnings.simplefilter('always')
             for template in templates:
                 template.render()
         return [w for w in caught if issubclass(w.category, FutureWarning)]
@@ -1767,27 +1771,16 @@ class StoredFinalizeDeprecationWarningTestCase(TestCase):
         self.assertIn(f'pk={first.pk}', str(caught[0].message))
         self.assertIn(f'pk={second.pk}', str(caught[1].message))
 
-    def test_warns_once_across_different_callers(self):
+    def test_warns_once_when_warning_filters_change_between_renders(self):
         """
-        Callers render from different source lines (views, API, scripts). The warning must be attributed to the
-        same site regardless, or the default filter would warn once per caller instead of once per template.
+        Changing the warning filters (catch_warnings(), filterwarnings(), urllib3.disable_warnings()) clears the
+        warnings registry. The once-per-template dedupe must not depend on it.
         """
         t = self._create_stored(ExportTemplate)
-
-        def render_a():
-            t.render()
-
-        def render_b():
-            t.render()
-
-        with warnings.catch_warnings(record=True) as caught:
-            warnings.simplefilter('default')
-            render_a()
-            render_b()
-            render_a()
-        caught = [w for w in caught if issubclass(w.category, FutureWarning)]
+        caught = []
+        for _ in range(3):
+            caught += self._render_recording(t)
         self.assertEqual(len(caught), 1)
-        self.assertTrue(caught[0].filename.endswith('extras/models/mixins.py'), caught[0].filename)
 
     def test_warns_separately_for_models_sharing_a_pk(self):
         params = {'finalize': 'builtins.len'}