Skip to content

Commit c4423ef

Browse files
BaseFigure._selector_matches accepts function selector
Any function that calls BaseFigure._selector_matches can use a function as the selector. This includes the "Figure.for_each_*" functions, the "Figure.update_*" functions, and the "Figure.select_*" functions. See test_selector_matches.py for the behaviour of the selector argument.
1 parent 4b3bd27 commit c4423ef

File tree

2 files changed

+92
-1
lines changed

2 files changed

+92
-1
lines changed

Diff for: packages/python/plotly/plotly/basedatatypes.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -814,7 +814,7 @@ def _selector_matches(obj, selector):
814814
if selector is None:
815815
return True
816816
# If selector is a dict, compare the fields
817-
if type(selector) == type(dict()):
817+
if (type(selector) == type(dict())) or isinstance(selector, BasePlotlyType):
818818
# This returns True if selector is an empty dict
819819
for k in selector:
820820
if k not in obj:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
import pytest
2+
3+
import plotly.graph_objects as go
4+
from plotly.basedatatypes import BaseFigure
5+
6+
7+
def test_selector_none():
8+
# should return True
9+
assert BaseFigure._selector_matches({}, None) == True # arbitrary,
10+
11+
12+
def test_selector_empty_dict():
13+
# should return True
14+
assert (
15+
BaseFigure._selector_matches(dict(hello="everybody"), {}) == True # arbitrary,
16+
)
17+
18+
19+
def test_selector_matches_subset_of_obj():
20+
# should return True
21+
assert (
22+
BaseFigure._selector_matches(
23+
dict(hello="everybody", today="cloudy", myiq=55),
24+
dict(myiq=55, today="cloudy"),
25+
)
26+
== True
27+
)
28+
29+
30+
def test_selector_has_nonmatching_key():
31+
# should return False
32+
assert (
33+
BaseFigure._selector_matches(
34+
dict(hello="everybody", today="cloudy", myiq=55),
35+
dict(myiq=55, cronenberg="scanners"),
36+
)
37+
== False
38+
)
39+
40+
41+
def test_selector_has_nonmatching_value():
42+
# should return False
43+
assert (
44+
BaseFigure._selector_matches(
45+
dict(hello="everybody", today="cloudy", myiq=55),
46+
dict(myiq=55, today="sunny"),
47+
)
48+
== False
49+
)
50+
51+
52+
def test_baseplotlytypes_could_match():
53+
# should return True
54+
obj = go.layout.Annotation(x=1, y=2, text="pat metheny")
55+
sel = go.layout.Annotation(x=1, y=2, text="pat metheny")
56+
assert BaseFigure._selector_matches(obj, sel) == True
57+
58+
59+
def test_baseplotlytypes_could_not_match():
60+
# should return False
61+
obj = go.layout.Annotation(x=1, y=3, text="pat metheny")
62+
sel = go.layout.Annotation(x=1, y=2, text="pat metheny")
63+
assert BaseFigure._selector_matches(obj, sel) == False
64+
65+
66+
def test_baseplotlytypes_cannot_match_subset():
67+
# should return False because "undefined" keys in sel return None, and are
68+
# compared (because "key in sel" returned True, it's value was None)
69+
obj = go.layout.Annotation(x=1, y=2, text="pat metheny")
70+
sel = go.layout.Annotation(x=1, y=2,)
71+
assert BaseFigure._selector_matches(obj, sel) == False
72+
73+
74+
def test_function_selector_could_match():
75+
# should return True
76+
obj = go.layout.Annotation(x=1, y=2, text="pat metheny")
77+
78+
def _sel(d):
79+
return d["x"] == 1 and d["y"] == 2 and d["text"] == "pat metheny"
80+
81+
assert BaseFigure._selector_matches(obj, _sel) == True
82+
83+
84+
def test_function_selector_could_not_match():
85+
# should return False
86+
obj = go.layout.Annotation(x=1, y=2, text="pat metheny")
87+
88+
def _sel(d):
89+
return d["x"] == 1 and d["y"] == 3 and d["text"] == "pat metheny"
90+
91+
assert BaseFigure._selector_matches(obj, _sel) == False

0 commit comments

Comments
 (0)