File tree Expand file tree Collapse file tree 2 files changed +29
-0
lines changed Expand file tree Collapse file tree 2 files changed +29
-0
lines changed Original file line number Diff line number Diff line change @@ -549,6 +549,34 @@ impl PyType {
549
549
Ok ( annotations)
550
550
}
551
551
552
+ #[ pygetset( magic, setter) ]
553
+ fn set_annotations ( & self , value : Option < PyObjectRef > , vm : & VirtualMachine ) -> PyResult < ( ) > {
554
+ if self . slots . flags . has_feature ( PyTypeFlags :: IMMUTABLETYPE ) {
555
+ return Err ( vm. new_type_error ( format ! (
556
+ "cannot set '__annotations__' attribute of immutable type '{}'" ,
557
+ self . name( )
558
+ ) ) ) ;
559
+ }
560
+
561
+ let __annotations__ = identifier ! ( vm, __annotations__) ;
562
+ if let Some ( value) = value {
563
+ self . attributes . write ( ) . insert ( __annotations__, value) ;
564
+ } else {
565
+ self . attributes
566
+ . read ( )
567
+ . get ( __annotations__)
568
+ . cloned ( )
569
+ . ok_or_else ( || {
570
+ vm. new_attribute_error ( format ! (
571
+ "'{}' object has no attribute '__annotations__'" ,
572
+ self . name( )
573
+ ) )
574
+ } ) ?;
575
+ }
576
+
577
+ Ok ( ( ) )
578
+ }
579
+
552
580
#[ pygetset( magic) ]
553
581
pub fn module ( & self , vm : & VirtualMachine ) -> PyObjectRef {
554
582
self . attributes
Original file line number Diff line number Diff line change @@ -105,6 +105,7 @@ impl std::fmt::Debug for PyTypeSlots {
105
105
bitflags ! {
106
106
#[ non_exhaustive]
107
107
pub struct PyTypeFlags : u64 {
108
+ const IMMUTABLETYPE = 1 << 8 ;
108
109
const HEAPTYPE = 1 << 9 ;
109
110
const BASETYPE = 1 << 10 ;
110
111
const METHOD_DESCR = 1 << 17 ;
You can’t perform that action at this time.
0 commit comments