Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Fix FontProperties conversion to/from strings #15601

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 4 commits into from
Dec 3, 2019

Conversation

TD22057
Copy link
Contributor

@TD22057 TD22057 commented Nov 4, 2019

PR Summary

This is a rewrite of PR#4910 which was out of date. Converting font configs (FontProperties) to and from strings doesn't work. The family name is different than the other keywords in the font config specification. It should be something like "family:key=value" not "family=family:key=value". This PR updates the FontProperties to and from string conversions to be corrects and symmetric.

FYI we ran into this because we serialize some configuration values and restore them for the user in applications and it would never work properly because the font configuration strings were incorrect.

PR Checklist

  • [X ] Has Pytest style unit tests
  • [ X] Code is Flake 8 compliant
  • New features are documented, with examples if plot related
  • Documentation is sphinx and numpydoc compliant
  • Added an entry to doc/users/next_whats_new/ if major new feature (follow instructions in README.rst there)
  • Documented in doc/api/api_changes.rst if API changed in a backward-incompatible way

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.

There are many improvements we could do to the font selection system but I think this is fine as an improvement, even if we change FontProperties later...
Please rename the test file to test_fontconfig_pattern.py for consistency with other test modules.

@TD22057
Copy link
Contributor Author

TD22057 commented Nov 5, 2019

FYI - I have no idea why the ci test failed. All I did was rename the file. I can't reproduce the errors on my local machine (though I get a lot other failing tests on my local machine on the master branch)

@anntzer
Copy link
Contributor

anntzer commented Nov 5, 2019

likely related to a transient bug with pyparsing (they made a new release); should be fixed now, relaunched the build.

the input escape function to make the values into legal font config
strings. The result is returned as a string.
"""
if isinstance(val, list):
Copy link
Member

Choose a reason for hiding this comment

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

This could be further simplified to:

    if not np.iterable(val):
        val = [val]
    return ','.join(escape_func(r'\\\1', str(x)) for x in val
                    if x is not None)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry - that doesn't work. strings are iterable so that test is incorrect.

I appreciate what you're doing here but I have a suggestion (I'm not trying to be snarky so hopefuly this doesn't come across that way). When I make a PR, I try to change as little as possible to fix the bug. I agree that you're suggestions are improvements but they are not related to the bug fix, they're just general code improvements. Rather than telling me what to do, me doing it, merging it, and waiting for more reviews, it seems like it would be simpler and more efficient to merge the PR to fix the bug and then you can make whatever code improvements you want and commit them.

Copy link
Member

@timhoffm timhoffm Dec 2, 2019

Choose a reason for hiding this comment

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

Good catch. Thanks for seeing that, and thanks for your feedback on the process!

Thinking of it, it should rather be if isinstance(val, str):, which is preferrable, because the function accepts any iterables and not just lists (even though the function is private and documented to only take lists). Additionally, the rewritten version does only have one joint code path for strings and lists. This reduces the risk of omissions in testing, and also the risk that only one of the paths will be modified some time in the future. Being defensive and picky about such things adds to the stability of the code, in particular in large projects with many users and contributors.

My philosophy of PR reviewing (skip if you're not interested):
I wouldn't have made any suggestions related to existing code. But this one is a completely new function. For new code I'd like to have it as concise and good quality as possible before merging. Merge+fix needs a second full (two approval) review. It adds clutter to the log, which makes it harder finding out later when something really changed.
Yet, for me personally it would still be faster to accept + fix. But, I believe that the review process is also a good way of learning. If PR authors contribute multiple times, their future PRs will be better. I intentionally invest time in this when reviewing.

In summary, I still would like to see the change with the str-check-variation above. Please let me know if you feel that's too cumbersome. I would then pick up from here myself. We're really grateful for your contribution. I'd also happy to hear from you if you feel this back-and-forth review is too annoying. That may help me to better judge how far to go and what to accept in future reviews.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK, fair enough. I think I was feeling that way because I felt like that function wasn't really new code, just a refactoring of the existing code.

I don't think that testing for str will work either since some of the properties are integers (font size). Those get converted to string in the non-list path. So we would need to do something like

    if not np.iterable(val) or isinstance(val, str):
        val = [val]

I've updated the code to that.

Copy link
Member

Choose a reason for hiding this comment

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

Thanks! 👍

@timhoffm timhoffm added this to the v3.2.1 milestone Dec 3, 2019
@timhoffm timhoffm merged commit 6fda79d into matplotlib:master Dec 3, 2019
meeseeksmachine pushed a commit to meeseeksmachine/matplotlib that referenced this pull request Dec 3, 2019
dstansby added a commit that referenced this pull request Dec 3, 2019
…601-on-v3.2.x

Backport PR #15601 on branch v3.2.x (Fix FontProperties conversion to/from strings)
@timhoffm timhoffm modified the milestones: v3.2.1, v3.2.0 Dec 3, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants