Skip to content

Cleanup initialization in text() #12215

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 23, 2018
Merged

Conversation

timhoffm
Copy link
Member

@timhoffm timhoffm commented Sep 22, 2018

PR Summary

Axes.text() created a default Text and then called Text.update() three times to adapt to the passed kwargs. This is a bit inefficient and cumbersome.

This PR changes the code to create effective kwargs and directly pass them to the Text() constructor. only update once.

This should not have any effect except being a little faster.

I also had to fix TextWithDash to accept all the kwargs of Text in the constructor. This is a bugfix as TextWithDash claims to be a drop-in replacement for Text.

Turns out, making TextWithDash fully API compatible with Text is a bit more cumbersome. Just passing the kwargs on is not enough. There is an issue with the call sequence TextWithDashes.__init__ -> Text.__init__ -> TextWithDashes.set_transform. I.e. the super init calling a child function before all the attributes of the child are set up.

Options are:

  1. Still delay the kwargs update (but just do one update instead of three).
  2. Resolve the initialization issue. It should work if super().__init__() is the last command in TextWithDashes.__init__ instead of the first. However, that seems just like a workaround as well. See Init calls subclass method pattern #12220 for further discussion.

For now, I've resorted to the defensive option 1.

@anntzer
Copy link
Contributor

anntzer commented Sep 22, 2018

In this specific case, I'd just deprecate TextWithDash in favor of Annotation (possibly after checking that the latter indeed replaces (reasonable...) use cases of the former).

@timhoffm
Copy link
Member Author

Even the scaled back version with a single update does yield different plots with python 3.5 on travis. There's something fishy going on if three sequential updates have a different effect compared to one combined update.

Marking as WIP because this needs further investigation.

@tacaswell
Copy link
Member

Ah, dict iteration order is random in 3.5.

This fix may have to wait until we drop 3.5 (which I suspect will be for the next release, but someone needs to turn the crank on making that decision).

@tacaswell tacaswell added this to the needs sorting milestone Sep 23, 2018
@timhoffm
Copy link
Member Author

Thanks, that's it.

Looking forward to drop 3.5, then we can also use f-strings 😄.

@timhoffm
Copy link
Member Author

Rebased. Should hopefully work now that were're on Python 3.6+.

Can be squashed.

Copy link
Contributor

@anntzer anntzer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

modulo ci

@anntzer
Copy link
Contributor

anntzer commented Dec 17, 2018

want to squash?

@timhoffm
Copy link
Member Author

Squashed.

@QuLogic
Copy link
Member

QuLogic commented Dec 23, 2018

So this is still not going to do the full constructor change even on 3.6+?

@timhoffm
Copy link
Member Author

@QuLogic Whst do you mean by „full constructor change“?

This is a basic simplification to just do a single update instead of updating multiple times.

@QuLogic
Copy link
Member

QuLogic commented Dec 23, 2018

I mean the original description:

This PR changes the code to create effective kwargs and directly pass them to the Text() constructor.

@timhoffm
Copy link
Member Author

Oh, that 😄 (too long ago to remember). Because of

Turns out, making TextWithDash fully API compatible with Text is a bit more cumbersome. Just passing the kwargs on is not enough. There is an issue with the call sequence TextWithDashes.init -> Text.init -> TextWithDashes.set_transform. I.e. the super init calling a child function before all the attributes of the child are set up.

I've restricted myself to

Option 1: Still delay the kwargs update (but just do one update instead of three).

The call sequence issue is not solved with python 3.6+. Also @anntzer proposed to deprecate TextWithDash. This whole topic would need more effort than I'm currently willing to spend on.

TL;DR

No, this won't do the full constructor change.

@timhoffm timhoffm changed the title Create Texts directly with all kwargs Cleanup initialization in text() Dec 23, 2018
@timhoffm
Copy link
Member Author

PR title and original description updated.

@QuLogic QuLogic merged commit 66b3b83 into matplotlib:master Dec 23, 2018
@timhoffm timhoffm deleted the text-kwargs branch December 23, 2018 22:42
@anntzer anntzer mentioned this pull request Feb 28, 2019
6 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants