From 28ff467eeeab767d16e1b48ab6c89c16c581efc5 Mon Sep 17 00:00:00 2001 From: MMGen Date: Fri, 18 Oct 2019 16:53:57 +0000 Subject: [PATCH] MMGenImmutableAttr: allow setting attrs to None with 'set_none_ok' --- mmgen/obj.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/mmgen/obj.py b/mmgen/obj.py index 86510f9b..68297a29 100755 --- a/mmgen/obj.py +++ b/mmgen/obj.py @@ -190,9 +190,10 @@ class Int(int,Hilite): pass # Reassignment and deletion forbidden class MMGenImmutableAttr(object): # Descriptor - def __init__(self,name,dtype,typeconv=True,no_type_check=False): + def __init__(self,name,dtype,typeconv=True,no_type_check=False,set_none_ok=False): self.typeconv = typeconv self.no_type_check = no_type_check + self.set_none_ok = set_none_ok assert isinstance(dtype,(str,type,type(None))),'{!r}: invalid dtype arg'.format(dtype) self.name = name self.dtype = dtype @@ -209,7 +210,9 @@ class MMGenImmutableAttr(object): # Descriptor if not self.set_attr_ok(instance): m = "Attribute '{}' of {} instance cannot be reassigned" raise AttributeError(m.format(self.name,type(instance))) - if self.typeconv: # convert type + if self.set_none_ok and value == None: + instance.__dict__[self.name] = None + elif self.typeconv: # convert type instance.__dict__[self.name] = \ globals()[self.dtype](value,on_fail='raise') if type(self.dtype) == str else self.dtype(value) else: # check type