From 88b67ee361e3bca50ee0cacfce4f6186c1ebf32e Mon Sep 17 00:00:00 2001 From: dot Date: Sat, 10 Oct 2026 16:47:36 +0900 Subject: [PATCH] Honor warning instance categories before validating category arguments Extract the Python 3.14-compatible ordering fix from #8954. An existing Warning instance determines its own category, regardless of the category argument. Keep canonical invalid-category coverage and add only the missing instance case to the existing warnings snippet. Assisted-by: OpenAI Codex:model identifier unavailable --- crates/vm/src/stdlib/_warnings.rs | 11 ++++------- extra_tests/snippets/stdlib_warnings.py | 17 +++++++++++++++++ 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/crates/vm/src/stdlib/_warnings.rs b/crates/vm/src/stdlib/_warnings.rs index 8a8f92b77da..35aca82d176 100644 --- a/crates/vm/src/stdlib/_warnings.rs +++ b/crates/vm/src/stdlib/_warnings.rs @@ -100,15 +100,12 @@ mod _warnings { category: Option, vm: &VirtualMachine, ) -> PyResult> { + if message.fast_isinstance(vm.ctx.exceptions.warning) { + return Ok(Some(message.class().to_owned())); + } let cat_obj = match category { Some(c) if !vm.is_none(&c) => c, - _ => { - return Ok(if message.fast_isinstance(vm.ctx.exceptions.warning) { - Some(message.class().to_owned()) - } else { - None // will default to UserWarning in warn_explicit - }); - } + _ => return Ok(None), // will default to UserWarning in warn_explicit }; let cat = PyTypeRef::try_from_object(vm, cat_obj.clone()).map_err(|_| { diff --git a/extra_tests/snippets/stdlib_warnings.py b/extra_tests/snippets/stdlib_warnings.py index 95092e50668..73ccc512ea7 100644 --- a/extra_tests/snippets/stdlib_warnings.py +++ b/extra_tests/snippets/stdlib_warnings.py @@ -1,3 +1,20 @@ import _warnings _warnings.warn("Test") + + +import warnings + + +class CustomWarning(UserWarning): + pass + + +for category in (None, DeprecationWarning, int, 42): + message = CustomWarning("sentinel") + with warnings.catch_warnings(record=True) as caught: + warnings.simplefilter("always") + warnings.warn(message, category) + assert len(caught) == 1 + assert caught[0].message is message + assert caught[0].category is CustomWarning