@@ -828,16 +828,11 @@ def _get_field(cls, a_name, a_type, default_kw_only):
828828 # default_kw_only is the value of kw_only to use if there isn't a field()
829829 # that defines it.
830830
831- # If the default value isn't derived from Field, then it's only a
832- # normal default value. Convert it to a Field().
833- default = getattr (cls , a_name , MISSING )
834- if isinstance (default , Field ):
835- f = default
831+ member = vars (cls ).get (a_name , MISSING )
832+ if isinstance (member , Field ):
833+ f = member
836834 else :
837- if isinstance (default , types .MemberDescriptorType ):
838- # This is a field in __slots__, so it has no default value.
839- default = MISSING
840- f = field (default = default )
835+ f = field ()
841836
842837 # Only at this point do we know the name and the type. Set them.
843838 f .name = a_name
@@ -899,6 +894,17 @@ def _get_field(cls, a_name, a_type, default_kw_only):
899894
900895 # kw_only validation and assignment.
901896 if f ._field_type in (_FIELD , _FIELD_INITVAR ):
897+ # If the default value isn't derived from Field, then it's only a
898+ # normal default value.
899+ default = getattr (cls , a_name , MISSING )
900+
901+ # This is a field in __slots__, so it has no default value.
902+ if isinstance (default , types .MemberDescriptorType ):
903+ default = MISSING
904+
905+ if not isinstance (default , Field ):
906+ f .default = default
907+
902908 # For real and InitVar fields, if kw_only wasn't specified use the
903909 # default value.
904910 if f .kw_only is MISSING :
@@ -1072,6 +1078,14 @@ def _process_class(cls, init, repr, eq, order, unsafe_hash, frozen,
10721078 # field) exists and is of type 'Field', replace it with the
10731079 # real default. This is so that normal class introspection
10741080 # sees a real default value, not a Field.
1081+
1082+ # Class variables cannot be removed from the class.
1083+ if f ._field_type is _FIELD_CLASSVAR :
1084+ if f .default is not MISSING :
1085+ setattr (cls , f .name , f .default )
1086+ continue
1087+
1088+ # Regular fields can be set or removed as necessary.
10751089 if isinstance (getattr (cls , f .name , None ), Field ):
10761090 if f .default is MISSING :
10771091 # If there's no default, delete the class attribute.
0 commit comments