Introduce ArtistList for FigureBase - #31746
Conversation
|
It seems I have discovered something that only works at py314 🧐 |
This comment was marked as outdated.
This comment was marked as outdated.
527f6fe to
81d29da
Compare
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
| A sublist of Figure children based on their type. This subclass exists only to | ||
| provide deprecation warnings. When the deprecations expire, use ArtistList |
There was a problem hiding this comment.
Wouldn't it be simpler to add
if isinstance(parent, FigureBase):
_api.warn_deprecated(...)or even
self.warn_deprecated_if_on_figure(...)to the regular ArtistList methods?
There was a problem hiding this comment.
ArtistList is already immutable. It doesn’t have these methods.
There was a problem hiding this comment.
Ok, I see. There's a lot of unforeseen complexity in the migration of ArtistList from AxesBase to artist, which gives me a bit of a headeache overlooking the complete PR. In hindsight it would likely have been better to either split this in two PRs / commits: (1) move ArtistList (2) use ArtistList in Figure; or alternatively just duplicate the 70 lines of ArtistList in Figure, and when the Figure deprecations run out and both classes become identical merge them back together. But I'm somewhat reluctant to request that now.
There was a problem hiding this comment.
Ah sorry. It did occur to me when I was moving it that splitting into separate commits for moving and modifying might help the review, but at the time I couldn't quite see how to split it. I will have a go at just moving it in a separate branch and if it works I'll make a prequel PR.
There was a problem hiding this comment.
... and now the difference appears.
|
Hid the discussion about doc-failure debugging as that is now fixed in #31794 and summarised at #31794 (comment). |
| # children. Adding images and text is usually achieved with the | ||
| # `~.Figure.figimage` and `~.Figure.text` methods. Other artists may be added | ||
| # with the `~.Figure.add_artist` method. |
There was a problem hiding this comment.
To me, there is not enough distinction between the factory methods and add_artist. You are using "add" with different semantics: For the factory methods it's a high-level logical add ("add a text to the figure") whereas for add_artist it's a technical "add an Artists instance to the figure".
| # children. Adding images and text is usually achieved with the | |
| # `~.Figure.figimage` and `~.Figure.text` methods. Other artists may be added | |
| # with the `~.Figure.add_artist` method. | |
| # children. Artists are added to theses lists via `~.Figure.add_artist`. |
Then put the example (which now does not contain transforms.
Then add
# As a convenience for images and text, the helper methods
# `~.Figure.figimage` and `~.Figure.text` conveniently create the
# respective Artists and internally call `~.Figure.add_artist`.
to set the stage for images and text.
There was a problem hiding this comment.
I tweaked your suggestion slightly as currently figimage and text are not calling add_artist but directly appending to _children. I did go back and forth on that:
- Obviously using
add_artistwould mean less code duplication. - Using
_children.appendkept the change within those functions minimal. - If
figimageusedadd_artistthen it would need to explicitly set theIdentityTransformto preventadd_artistsettingtransSubfigure. This might arguably be a good thing to do (and would fix [Bug]: Figure.add_artist() fails to re-render an image previously added via Figure.figimage() after .remove() #31730) but it created an extra thing to think about and there was already more than enough in this PR! - Possibly some other consideration that I have since forgotten...
| return _FigureArtistList(self, 'texts', valid_types=Text) | ||
|
|
||
|
|
||
|
|
||
| def _get_draw_artists(self, renderer): |
There was a problem hiding this comment.
Too many blank lines.
| return _FigureArtistList(self, 'texts', valid_types=Text) | |
| def _get_draw_artists(self, renderer): | |
| return _FigureArtistList(self, 'texts', valid_types=Text) | |
| def _get_draw_artists(self, renderer): |
There was a problem hiding this comment.
Huh. I wonder why ruff doesn't catch that.
There was a problem hiding this comment.
Even though we enable E rules, we also have explicit-preview-rules=true, so those blank line ones are not enabled yet. #32126
Following matplotlib#18216 for Axes artists, combine all figure artists except axes and subfigures into a single list and deprecate modifying the lists directly.
|
So I'm looking at type hint errors, and I noticed that this doesn't mark the |
|
Let’s not jump extra hoops. This will become immutable in the future, so already flaggingbitbin typing is ok. |
PR summary
At #31730 (comment) Tom said
This PR generalises the
ArtistListso it can work for both figures and axes. The new_FigureArtistListsubclasses it just to add the deprecated methods. I adapted the deprecated methods and their tests from #18216, where this was originally done forAxes. Note thatfig.subfiguresis still a plain list. I do not think subfigures belong infig._children, but rather should have their own handling similar tofig.axes. That can be done separately.AI Disclosure
I asked duck.ai how to get a class as a string, to use in the
ArtistListreprself._parent.__class__.__name__.PR checklist