Skip to content

Commit

Permalink
Rework handling of unknown kwargs in datatype classes
Browse files Browse the repository at this point in the history
kwargs may now be handled after the constructor is called, this
is needed to fix a bug with specifying the first axis args as
xaxis1 (rather than xaxis).
  • Loading branch information
Jon M. Mease committed Apr 14, 2018
1 parent c6d6540 commit a89dcb5
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 15 deletions.
9 changes: 8 additions & 1 deletion codegen/datatypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ def __init__(self""")
add_docstring(buffer, node, header=header)

buffer.write(f"""
super().__init__('{node.name_property}', **kwargs)
super().__init__('{node.name_property}')
# Import validators
# -----------------
Expand Down Expand Up @@ -227,6 +227,13 @@ def __init__(self""")
buffer.write(f"""
self._props['{lit_name}'] = '{lit_val}'""")

buffer.write(f"""
# Process unknown kwargs
# ----------------------
self._process_kwargs(**kwargs)
""")

# Return source string
# --------------------
return buffer.getvalue()
Expand Down
46 changes: 32 additions & 14 deletions plotly/basedatatypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2063,7 +2063,7 @@ def __init__(self, plotly_name, **kwargs):
"""
# Validate inputs
# ---------------
self._raise_on_invalid_property_error(*kwargs.keys())
self._process_kwargs(**kwargs)

# Store params
# ------------
Expand Down Expand Up @@ -2107,6 +2107,12 @@ def __init__(self, plotly_name, **kwargs):
# type: Dict[Tuple[Tuple[Union[str, int]]], List[Callable]]
self._change_callbacks = {}

def _process_kwargs(self, **kwargs):
"""
Process any extra kwargs that are not predefined as constructor params
"""
self._raise_on_invalid_property_error(*kwargs.keys())

@property
def plotly_name(self):
"""
Expand Down Expand Up @@ -3145,26 +3151,38 @@ def __init__(self, plotly_name, **kwargs):
# ---------------
assert plotly_name == 'layout'

# Compute invalid kwargs
# ----------------------
# Pass to parent for error handling
invalid_kwargs = {
k: v
for k, v in kwargs.items()
if not self._subplotid_prop_re.fullmatch(k)
}
super().__init__(plotly_name, **invalid_kwargs)
# Call superclass constructor
# ---------------------------
super().__init__(plotly_name)

# Initialize _subplotid_props
# ---------------------------
# This is a set storing the names of the layout's dynamic subplot
# properties
self._subplotid_props = set()

# Process subplot properties
# --------------------------
# The remaining kwargs are valid subplot properties
for prop, value in kwargs.items():
# Process kwargs
# --------------
self._process_kwargs(**kwargs)

def _process_kwargs(self, **kwargs):
"""
Process any extra kwargs that are not predefined as constructor params
"""
unknown_kwargs = {
k: v
for k, v in kwargs.items()
if not self._subplotid_prop_re.fullmatch(k)
}
super()._process_kwargs(**unknown_kwargs)

subplot_kwargs = {
k: v
for k, v in kwargs.items()
if self._subplotid_prop_re.fullmatch(k)
}

for prop, value in subplot_kwargs.items():
self._set_subplotid_prop(prop, value)

def _set_subplotid_prop(self, prop, value):
Expand Down

0 comments on commit a89dcb5

Please sign in to comment.