-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
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
Conversation
There was a problem hiding this 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.
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) |
likely related to a transient bug with pyparsing (they made a new release); should be fixed now, relaunched the build. |
lib/matplotlib/fontconfig_pattern.py
Outdated
the input escape function to make the values into legal font config | ||
strings. The result is returned as a string. | ||
""" | ||
if isinstance(val, list): |
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! 👍
…601-on-v3.2.x Backport PR #15601 on branch v3.2.x (Fix FontProperties conversion to/from strings)
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