Skip to content

MNT: Cleanup FontProperties __init__ API #28843

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
Sep 26, 2024

Conversation

timhoffm
Copy link
Member

@timhoffm timhoffm commented Sep 19, 2024

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 timhoffm force-pushed the fontproperties-init branch 3 times, most recently from fc73e45 to e1952bb Compare September 19, 2024 12:51
@timhoffm timhoffm marked this pull request as draft September 19, 2024 12:51
@timhoffm timhoffm added this to the v3.10.0 milestone Sep 19, 2024
@timhoffm timhoffm marked this pull request as ready for review September 19, 2024 13:56
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
Member

Choose a reason for hiding this comment

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

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

Copy link
Member Author

Choose a reason for hiding this comment

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

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

@QuLogic
Copy link
Member

QuLogic commented Sep 19, 2024

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 timhoffm force-pushed the fontproperties-init branch 2 times, most recently from 73cbd3d to a6d8f41 Compare September 20, 2024 08:01
@timhoffm
Copy link
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
Member

Choose a reason for hiding this comment

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

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
Member

Choose a reason for hiding this comment

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

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
Member Author

Choose a reason for hiding this comment

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

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.

Copy link
Member Author

@timhoffm timhoffm Sep 23, 2024

Choose a reason for hiding this comment

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

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.

@tacaswell
Copy link
Member

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

@ksunden ksunden merged commit 13380e1 into matplotlib:main Sep 26, 2024
41 of 43 checks passed
@timhoffm 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.
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