Skip to content

Commit c85b93a

Browse files
committed
Add get annotations for type
1 parent 1cb7f4d commit c85b93a

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

vm/src/builtins/type.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,32 @@ impl PyType {
523523
Ok(())
524524
}
525525

526+
#[pygetset(magic)]
527+
fn annotations(&self, vm: &VirtualMachine) -> PyResult<PyObjectRef> {
528+
if !self.slots.flags.has_feature(PyTypeFlags::HEAPTYPE) {
529+
return Err(vm.new_attribute_error(format!(
530+
"type object '{}' has no attribute '__annotations__'",
531+
self.name()
532+
)));
533+
}
534+
535+
let __annotations__ = identifier!(vm, __annotations__);
536+
let annotations = self.attributes.read().get(__annotations__).cloned();
537+
538+
let annotations = if let Some(annotations) = annotations {
539+
annotations
540+
} else {
541+
let annotations: PyObjectRef = vm.ctx.new_dict().into();
542+
let removed = self
543+
.attributes
544+
.write()
545+
.insert(__annotations__, annotations.clone());
546+
debug_assert!(removed.is_none());
547+
annotations
548+
};
549+
Ok(annotations)
550+
}
551+
526552
#[pygetset(magic)]
527553
pub fn module(&self, vm: &VirtualMachine) -> PyObjectRef {
528554
self.attributes

0 commit comments

Comments
 (0)