@@ -521,6 +521,60 @@ impl PyType {
521521 Ok ( ( ) )
522522 }
523523
524+ #[ pygetset( magic) ]
525+ fn annotations ( & self , vm : & VirtualMachine ) -> PyResult < PyObjectRef > {
526+ if !self . slots . flags . has_feature ( PyTypeFlags :: HEAPTYPE ) {
527+ return Err ( vm. new_attribute_error ( format ! (
528+ "type object '{}' has no attribute '__annotations__'" ,
529+ self . name( )
530+ ) ) ) ;
531+ }
532+
533+ let __annotations__ = identifier ! ( vm, __annotations__) ;
534+ let annotations = self . attributes . read ( ) . get ( __annotations__) . cloned ( ) ;
535+
536+ let annotations = if let Some ( annotations) = annotations {
537+ annotations
538+ } else {
539+ let annotations: PyObjectRef = vm. ctx . new_dict ( ) . into ( ) ;
540+ let removed = self
541+ . attributes
542+ . write ( )
543+ . insert ( __annotations__, annotations. clone ( ) ) ;
544+ debug_assert ! ( removed. is_none( ) ) ;
545+ annotations
546+ } ;
547+ Ok ( annotations)
548+ }
549+
550+ #[ pygetset( magic, setter) ]
551+ fn set_annotations ( & self , value : Option < PyObjectRef > , vm : & VirtualMachine ) -> PyResult < ( ) > {
552+ if self . slots . flags . has_feature ( PyTypeFlags :: IMMUTABLETYPE ) {
553+ return Err ( vm. new_type_error ( format ! (
554+ "cannot set '__annotations__' attribute of immutable type '{}'" ,
555+ self . name( )
556+ ) ) ) ;
557+ }
558+
559+ let __annotations__ = identifier ! ( vm, __annotations__) ;
560+ if let Some ( value) = value {
561+ self . attributes . write ( ) . insert ( __annotations__, value) ;
562+ } else {
563+ self . attributes
564+ . read ( )
565+ . get ( __annotations__)
566+ . cloned ( )
567+ . ok_or_else ( || {
568+ vm. new_attribute_error ( format ! (
569+ "'{}' object has no attribute '__annotations__'" ,
570+ self . name( )
571+ ) )
572+ } ) ?;
573+ }
574+
575+ Ok ( ( ) )
576+ }
577+
524578 #[ pygetset( magic) ]
525579 pub fn module ( & self , vm : & VirtualMachine ) -> PyObjectRef {
526580 self . attributes
0 commit comments