FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

MNT: Cleanup FontProperties __init__ API by timhoffm · Pull Request #28843 · matplotlib/matplotlib · GitHub

MNT: Cleanup FontProperties __init__ API - #28843

Merged
ksunden merged 1 commit into
matplotlib:mainfrom
timhoffm:fontproperties-init
Sep 26, 2024
Merged

ksunden merged 1 commit into
matplotlib:mainfrom
timhoffm:fontproperties-init

Conversation

timhoffm commented Sep 19, 2024 •
edited
Loading

Copy link
Copy Markdown
Member

Alternative to #28837.

Essentially, the cleanup limits the API to the intended call patterns. This will subsequently make it easier and more explicit to distinguish between the two intended call patterns "one positional argument as pattern" or "individual properties as keyword arguments" and resolves weird corner cases and unexpected behavior of the current API.

The cleanup approach is to not modify code logic during a deprecation period, but only detect deprecated call patterns through a custom decorator and warn on them. This is needed due to the somewhat particular dual signature. Standard _make_keyword_only or similar approaches fall short here.

timhoffm force-pushed the fontproperties-init branch 3 times, most recently from fc73e45 to e1952bb Compare September 19, 2024 12:51
timhoffm marked this pull request as draft September 19, 2024 12:51
timhoffm added this to the v3.10.0 milestone Sep 19, 2024
timhoffm marked this pull request as ready for review September 19, 2024 13:56
Comment thread lib/matplotlib/font_manager.py Outdated
Comment on lines +573 to +574
if (len(args) == 1 and not kwargs
and isinstance(args[0], Iterable) and not isinstance(args[0], str)):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Is this not cbook.is_scalar_or_string(args[0])?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

👍 I thought we had something in cbook, but was looking for _is_nonstring_sequence 😆.

QuLogic commented Sep 19, 2024

Copy link
Copy Markdown
Member

I guess this could do with some tests, mostly so I can see in which cases you do/don't intend to raise/warn?

timhoffm force-pushed the fontproperties-init branch 2 times, most recently from 73cbd3d to a6d8f41 Compare September 20, 2024 08:01

Copy link
Copy Markdown
Member Author

Tests added.

The cleanup approach is to not modify
code logic during a deprecation period,
but only detect deprecated call patterns
through a decorator and warn on them.
# a string is *not* a valid family.
# Since this case is not covered by docs, I've refrained from jumping
# extra hoops to detect this possible API misuse.
FontProperties(family="serif-24:style=oblique:weight=bold")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

The docstring on FontProperties says that family should be a list. However, the docstring on set_family does say it can be a list or a string and we just fall back to that internally.

If the family is passed as a str we can detect that and ask the user to either pass it positionally (if they meant it to be a pattern) or as a list (if they meant it to be a family).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

I think we need to deal with this if we want to push this as eventually we can end up with patterns coming in to family via the kwarg which will stop working with no warning.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

I would rather keep accepting a string as family, because it's consistent with set_family and it's convenient. For now, I'd still accept a pattern as family kwarg as undocumented backward-compatibility. We can decide separately whether we want to move away from that.

timhoffm Sep 23, 2024 •
edited
Loading

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

TL;DR This PR only deprecates working but undocumented and unintended behavior. After deprecation, we will have to logically separted APIs (positional pattern or kwargs). We do change the officially documented API. - That could be done in a later step if desired (and will become simpler though this PR).

This would be a good use of the FontProperties.from_pattern(...) but it is probably not worth the effort to push.

That was my initial approach at #28837. I think it's eventually still a good idea. In the current apporach, I'm going less far in that I narrow the API to the documented behavior. Users, who have used FontPropertie as documented will not need to change anything. This approach already seperates the API logically (mostly if you still support the special case of family=pattern). The nice thing here is that we do the deprecation solely by a decorator, i.e. don't run into the danger of accidentally breaking an currently valid code through singature changes. We can later deprecate FontProperties(pattern) in favor of FontProperties.from_pattern, but that can be a separate discussion.

Copy link
Copy Markdown
Member

This would be a good use of the FontProperties.from_pattern(...) but it is probably not worth the effort to push.

ksunden merged commit 13380e1 into matplotlib:main Sep 26, 2024
timhoffm deleted the fontproperties-init branch September 26, 2024 22:17
kyracho pushed a commit to kyracho/matplotlib that referenced this pull request Oct 10, 2024
The cleanup approach is to not modify
code logic during a deprecation period,
but only detect deprecated call patterns
through a decorator and warn on them.
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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants


Back | FazBrowse Home | New Git URL