Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add __or__, __ror__, __ior__ methods to BasePlotlyType to support dict-like updates via pipe operator #4047

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions packages/python/plotly/plotly/basedatatypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -5118,6 +5118,69 @@ def update(self, dict1=None, overwrite=False, **kwargs):

return self

def __or__(self, other):
"""
Overload the bitwise or operator to perform a deep merge of two
objects. The other object is merged into a copy of the current
object which is returned.

Parameters
----------
other : dict | BasePlotlyType
Object to merge into the current object

Returns
-------
BasePlotlyType
Updated plotly object
"""
if not isinstance(other, (dict, BasePlotlyType)):
return NotImplemented
new = self.copy()
new.update(other)
return new

def __ror__(self, other):
"""
Overload the bitwise or operator to perform a deep merge of two
objects. The current object is merged into the other object which
is returned.

Parameters
----------
other : dict | BasePlotlyType
Object to merge the current object into

Returns
-------
dict | BasePlotlyType
Updated object
"""
if not isinstance(other, (dict, BasePlotlyType)):
return NotImplemented
new = other.copy()
new.update(self)
return new

def __ior__(self, other):
"""
Overload the bitwise or operator to perform a deep merge of two
objects. The other object is merged into the current object, modifying
it in place.

Parameters
----------
other : BasePlotlyType
Object to merge into the current object

Returns
-------
BasePlotlyType
Updated plotly object
"""
dict.update(self, other)
return self

def pop(self, key, *args):
"""
Remove the value associated with the specified key and return it
Expand Down