From faeed2cdcc8eb25f16f009e2ce4bc0f27cdb0bb7 Mon Sep 17 00:00:00 2001 From: "Jeong, YunWon" Date: Sat, 17 Jan 2026 19:05:29 +0900 Subject: [PATCH] clean up --- crates/codegen/src/compile.rs | 7 +-- crates/vm/src/builtins/function.rs | 71 +++++++++++++++--------------- crates/vm/src/vm/vm_new.rs | 2 - 3 files changed, 36 insertions(+), 44 deletions(-) diff --git a/crates/codegen/src/compile.rs b/crates/codegen/src/compile.rs index 2d3603c74..d479c0d0e 100644 --- a/crates/codegen/src/compile.rs +++ b/crates/codegen/src/compile.rs @@ -1191,12 +1191,7 @@ impl Compiler { // Jump to body if format <= 2 (comparison is false) let body_block = self.new_block(); - emit!( - self, - Instruction::PopJumpIfFalse { - target: body_block, - } - ); + emit!(self, Instruction::PopJumpIfFalse { target: body_block }); // Raise NotImplementedError let not_implemented_error = self.name("NotImplementedError"); diff --git a/crates/vm/src/builtins/function.rs b/crates/vm/src/builtins/function.rs index 55d9d0466..9297cf072 100644 --- a/crates/vm/src/builtins/function.rs +++ b/crates/vm/src/builtins/function.rs @@ -605,12 +605,10 @@ impl PyFunction { // Check for callable __annotate__ and clone it before calling let annotate_fn = { let annotate = self.annotate.lock(); - if let Some(ref func) = *annotate { - if func.is_callable() { - Some(func.clone()) - } else { - None - } + if let Some(ref func) = *annotate + && func.is_callable() + { + Some(func.clone()) } else { None } @@ -641,26 +639,24 @@ impl PyFunction { } #[pygetset(setter)] - fn set___annotations__(&self, value: PySetterValue, vm: &VirtualMachine) -> PyResult<()> { - match value { - PySetterValue::Assign(value) => { - if vm.is_none(&value) { - *self.annotations.lock() = None; - } else { - let annotations = - value.downcast::().map_err(|_| { - vm.new_type_error("__annotations__ must be set to a dict object") - })?; - *self.annotations.lock() = Some(annotations); - } - // Clear __annotate__ when __annotations__ is set - *self.annotate.lock() = None; + fn set___annotations__( + &self, + value: PySetterValue>, + vm: &VirtualMachine, + ) -> PyResult<()> { + let annotations = match value { + PySetterValue::Assign(Some(value)) => { + let annotations = value.downcast::().map_err(|_| { + vm.new_type_error("__annotations__ must be set to a dict object") + })?; + Some(annotations) } - PySetterValue::Delete => { - *self.annotations.lock() = None; - *self.annotate.lock() = None; - } - } + PySetterValue::Assign(None) | PySetterValue::Delete => None, + }; + *self.annotations.lock() = annotations; + + // Clear __annotate__ when __annotations__ is set + *self.annotate.lock() = None; Ok(()) } @@ -673,23 +669,26 @@ impl PyFunction { } #[pygetset(setter)] - fn set___annotate__(&self, value: PySetterValue, vm: &VirtualMachine) -> PyResult<()> { - match value { - PySetterValue::Assign(value) => { - if vm.is_none(&value) { - *self.annotate.lock() = Some(value); - } else if value.is_callable() { - *self.annotate.lock() = Some(value); - // Clear cached __annotations__ when __annotate__ is set - *self.annotations.lock() = None; - } else { + fn set___annotate__( + &self, + value: PySetterValue>, + vm: &VirtualMachine, + ) -> PyResult<()> { + let annotate = match value { + PySetterValue::Assign(Some(value)) => { + if !value.is_callable() { return Err(vm.new_type_error("__annotate__ must be callable or None")); } + // Clear cached __annotations__ when __annotate__ is set + *self.annotations.lock() = None; + Some(value) } + PySetterValue::Assign(None) => None, PySetterValue::Delete => { return Err(vm.new_type_error("__annotate__ cannot be deleted")); } - } + }; + *self.annotate.lock() = annotate; Ok(()) } diff --git a/crates/vm/src/vm/vm_new.rs b/crates/vm/src/vm/vm_new.rs index 517fa23aa..8fc8108b6 100644 --- a/crates/vm/src/vm/vm_new.rs +++ b/crates/vm/src/vm/vm_new.rs @@ -65,8 +65,6 @@ impl VirtualMachine { pub fn new_scope_with_main(&self) -> PyResult { let scope = self.new_scope_with_builtins(); let main_module = self.new_module("__main__", scope.globals.clone(), None); - // PEP 649: Don't automatically initialize __annotations__ - // It will be lazily created by the descriptor when accessed self.sys_module.get_attr("modules", self)?.set_item( "__main__",