Skip to content

Commit 6ac6bec

Browse files
committed
Fix validate_colors_dict, convert_dict_colors_to_same_type and convert_colorscale_to_rgb modifying their input
1 parent a679fa1 commit 6ac6bec

3 files changed

Lines changed: 24 additions & 1 deletion

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +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` [[#5836](https://github.com/plotly/plotly.py/pull/5836)], with thanks to @Enesuygurs for the contribution!
24+
- Fix `plotly.colors.validate_colors`, `plotly.colors.convert_colors_to_same_type`, `plotly.colors.validate_colors_dict`, `plotly.colors.convert_dict_colors_to_same_type` and `plotly.colors.convert_colorscale_to_rgb` modifying the colors or colorscale 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` [[#5836](https://github.com/plotly/plotly.py/pull/5836)], with thanks to @Enesuygurs for the contribution!
2525

2626
## [7.1.0] - 2026-09-15
2727

‎_plotly_utils/colors/__init__.py‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,9 @@ def validate_colors_dict(colors, colortype="tuple"):
382382
"""
383383
Validates dictionary of color(s)
384384
"""
385+
# copy the dict so that the caller's dict is not modified in place below
386+
colors = dict(colors)
387+
385388
# validate each color element in the dictionary
386389
for key in colors:
387390
if "rgb" in colors[key]:
@@ -503,6 +506,9 @@ def convert_dict_colors_to_same_type(colors_dict, colortype="rgb"):
503506
504507
:param (dict) colors_dict: a dictionary whose values are single colors
505508
"""
509+
# copy the dict so that the caller's dict is not modified in place below
510+
colors_dict = dict(colors_dict)
511+
506512
for key in colors_dict:
507513
if "#" in colors_dict[key]:
508514
colors_dict[key] = color_parser(colors_dict[key], hex_to_rgb)
@@ -823,6 +829,10 @@ def convert_colorscale_to_rgb(colorscale):
823829
converting a colorscale with tuple colors (each coordinate between 0
824830
and 1) into a colorscale with the colors transformed into rgb colors
825831
"""
832+
# copy the colorscale and its [scale, color] pairs so that the caller's
833+
# colorscale is not modified in place below
834+
colorscale = [list(color) for color in colorscale]
835+
826836
for color in colorscale:
827837
color[1] = convert_to_RGB_255(color[1])
828838

‎tests/test_core/test_colors/test_colors.py‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,19 @@ def test_color_conversions_do_not_modify_input(self):
7272
colors.convert_colors_to_same_type(color_list, colortype="tuple")
7373
self.assertEqual(color_list, ["rgb(255, 0, 0)", "#0000ff"])
7474

75+
color_dict = {"a": "rgb(255, 0, 0)", "b": "#0000ff", "c": (0.5, 0.5, 0.5)}
76+
expected_dict = dict(color_dict)
77+
78+
colors.validate_colors_dict(color_dict, colortype="rgb")
79+
self.assertEqual(color_dict, expected_dict)
80+
81+
colors.convert_dict_colors_to_same_type(color_dict, colortype="tuple")
82+
self.assertEqual(color_dict, expected_dict)
83+
84+
colorscale = [[0, (0.0, 0.0, 0.0)], [1, (1.0, 1.0, 1.0)]]
85+
colors.convert_colorscale_to_rgb(colorscale)
86+
self.assertEqual(colorscale, [[0, (0.0, 0.0, 0.0)], [1, (1.0, 1.0, 1.0)]])
87+
7588
def test_convert_dict_colors_to_same_type(self):
7689
# test colortype
7790
color_dict = dict(apple="rgb(1, 1, 1)")

0 commit comments

Comments
 (0)