Allow using a non-toal dict to init Options objects

This commit is contained in:
Kovid Goyal 2021-08-04 17:06:50 +05:30
parent 7090c24321
commit e50c26d1b9
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
3 changed files with 12 additions and 3 deletions

View File

@ -72,8 +72,11 @@ class Options:
def __init__(self, options_dict: typing.Optional[typing.Dict[str, typing.Any]] = None) -> None:
if options_dict is not None:
null = object()
for key in option_names:
setattr(self, key, options_dict[key])
val = options_dict.get(key, null)
if val is not null:
setattr(self, key, val)
@property
def _fields(self) -> typing.Tuple[str, ...]:

View File

@ -185,8 +185,11 @@ def generate_class(defn: Definition, loc: str) -> Tuple[str, str]:
if defn.has_color_table:
a(' self.color_table = array(self.color_table.typecode, self.color_table)')
a(' if options_dict is not None:')
a(' null = object()')
a(' for key in option_names:')
a(' setattr(self, key, options_dict[key])')
a(' val = options_dict.get(key, null)')
a(' if val is not null:')
a(' setattr(self, key, val)')
a('')
a(' @property')

View File

@ -608,8 +608,11 @@ class Options:
def __init__(self, options_dict: typing.Optional[typing.Dict[str, typing.Any]] = None) -> None:
self.color_table = array(self.color_table.typecode, self.color_table)
if options_dict is not None:
null = object()
for key in option_names:
setattr(self, key, options_dict[key])
val = options_dict.get(key, null)
if val is not null:
setattr(self, key, val)
@property
def _fields(self) -> typing.Tuple[str, ...]: