Repository navigation
Conversation
axis_spanning_shape_annotation filled in the annotation it was given (text from annotation_* kwargs, and x/y/xanchor/yanchor from the shape) in place. When the same dict or go.layout.Annotation was passed to several add_vline/add_hline/add_vrect/add_hrect calls, the first call left x/y in it, and later calls only set positions that weren't set already, so every annotation ended up at the first shape. Work on a copy of the annotation instead. Closes plotly#3547 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Link to issue
Closes #3547
Description of change
add_vline,add_hline,add_vrectandadd_hrectno longer modify the dict orgo.layout.Annotationpassed asannotation=. Before, reusing one annotation (e.g. a shared style dict) for several shapes put every annotation at the first shape's position.shapeannotation.axis_spanning_shape_annotationfilled in the annotation it was given in place. It settextand the otherannotation_*kwargs, then setx/y/xanchor/yanchorfrom the shape, but only where they weren't already set. After the first call, the caller's annotation already held the first shape'sx/y, so later calls kept those coordinates and only the text changed (the reporter's snippet prints this). It now works on acopy.copyof the annotation. That's a shallow dict copy for dicts, and forgo.layout.Annotationthe copy goes throughBasePlotlyType.__reduce__. The function only sets top-level keys, so the caller's object stays untouched.Demo
The reproducer from the issue:
On
mainthis gives[2, 2], andannotationends up as{'font_color': '#000000', 'text': 'vline 2', 'xanchor': 'left', 'yanchor': 'top', 'x': 2, 'y': 1, 'showarrow': False}. With this change it gives[2, 3]andannotationis still{'font_color': '#000000'}. Ago.layout.Annotationpassed toadd_hlinetwice behaved the same way (ys == [1, 1]) and is fixed too.Testing strategy
test_reuse_annotationintests/test_optional/test_autoshapes/test_annotated_shapes.pyis parametrized over a dict and ago.layout.Annotation. It adds two vlines with the same annotation, then checks that the annotation is unchanged and that each layout annotation has its own text, its ownxand the shared font color. Both cases fail onmainand pass with the fix.Local runs:
tests/test_optional/test_autoshapes(35 passed), plustests/test_core/test_update_objects,test_subplotsandtest_graph_objs(348 passed, 1 skipped).ruff format --checkis clean on the touched files.Guidelines
🤖 Generated with Claude Code