Skip to content

Commit 6260470

Browse files
committed
Fix validate_colors and convert_colors_to_same_type modifying their input
Both helpers rewrote the list passed to them in place, which also changed DEFAULT_PLOTLY_COLORS (e.g. after create_trisurf without a colormap) and built-in sequences such as plotly.colors.sequential.Inferno. Copy the list before converting it.
1 parent 63895f5 commit 6260470

3 files changed

Lines changed: 20 additions & 1 deletion

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
2121
- Fix `convert_dash` mapping dotted lines (`linestyle=':'`) to invalid `'circle'` in Plotly, correctly converting them to `'dot'` and preserving custom dash patterns as pixel lists, [[#5719](https://github.com/plotly/plotly.py/pull/5719)] with thanks to @robertoffmoura for the contribution!
2222
- Fix `px.sunburst`, `px.treemap` and `px.icicle` listing sectors in a different order on every run when `path` is used with a Polars DataFrame; sectors now follow their order of first appearance for all dataframe backends [[#5766](https://github.com/plotly/plotly.py/pull/5766)], with thanks to @Irahan2 for the contribution!
2323
- Fix `mpl_to_plotly` conversion of matplotlib contour lines and line collections: close contour rings ending with `Z` codes, support dash styles, convert date x-axes, separate disjoint subpaths with `None` separators, hide line collection traces from the legend by default, and group consecutive same-style lines into single traces [[#5770](https://github.com/plotly/plotly.py/pull/5770)], with thanks to @robertoffmoura for the contribution!
24+
- Fix `plotly.colors.validate_colors` and `plotly.colors.convert_colors_to_same_type` modifying the list of colors passed to them in place, which also changed `plotly.colors.DEFAULT_PLOTLY_COLORS` (for example after calling `create_trisurf` without a `colormap`) and built-in color sequences such as `plotly.colors.sequential.Inferno` [[#XXXX](https://github.com/plotly/plotly.py/pull/XXXX)], with thanks to @Enesuygurs for the contribution!
2425

2526
## [7.1.0] - 2026-09-15
2627

‎_plotly_utils/colors/__init__.py‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,11 @@ def validate_colors(colors, colortype="tuple"):
338338
else:
339339
colors = list(colors)
340340

341+
else:
342+
# copy the list so that neither the caller's list nor
343+
# DEFAULT_PLOTLY_COLORS is modified in place below
344+
colors = list(colors)
345+
341346
# convert color elements in list to tuple color
342347
for j, each_color in enumerate(colors):
343348
if "rgb" in each_color:
@@ -454,7 +459,7 @@ def convert_colors_to_same_type(
454459
colors_list = list(colors)
455460

456461
elif isinstance(colors, list):
457-
colors_list = colors
462+
colors_list = list(colors)
458463

459464
# validate scale
460465
if scale is not None:

‎tests/test_core/test_colors/test_colors.py‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,19 @@ def test_convert_colors_to_same_type(self):
5959
colortype,
6060
)
6161

62+
def test_color_conversions_do_not_modify_input(self):
63+
default_colors = list(colors.DEFAULT_PLOTLY_COLORS)
64+
65+
colors.validate_colors(None)
66+
self.assertEqual(colors.DEFAULT_PLOTLY_COLORS, default_colors)
67+
68+
color_list = ["rgb(255, 0, 0)", "#0000ff"]
69+
colors.validate_colors(color_list)
70+
self.assertEqual(color_list, ["rgb(255, 0, 0)", "#0000ff"])
71+
72+
colors.convert_colors_to_same_type(color_list, colortype="tuple")
73+
self.assertEqual(color_list, ["rgb(255, 0, 0)", "#0000ff"])
74+
6275
def test_convert_dict_colors_to_same_type(self):
6376
# test colortype
6477
color_dict = dict(apple="rgb(1, 1, 1)")

0 commit comments

Comments
 (0)