-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
FontProperties initialization | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
`.FontProperties` initialization is limited to the two call patterns: | ||
|
||
- single positional parameter, interpreted as fontconfig pattern | ||
- only keyword parameters for setting individual properties | ||
|
||
All other previously supported call patterns are deprecated. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ | |
import numpy as np | ||
import pytest | ||
|
||
import matplotlib as mpl | ||
from matplotlib.font_manager import ( | ||
findfont, findSystemFonts, FontEntry, FontProperties, fontManager, | ||
json_dump, json_load, get_font, is_opentype_cff_font, | ||
|
@@ -367,3 +368,42 @@ def inner(): | |
for obj in gc.get_objects(): | ||
if isinstance(obj, SomeObject): | ||
pytest.fail("object from inner stack still alive") | ||
|
||
|
||
def test_fontproperties_init_deprecation(): | ||
""" | ||
Test the deprecated API of FontProperties.__init__. | ||
|
||
The deprecation does not change behavior, it only adds a deprecation warning | ||
via a decorator. Therefore, the purpose of this test is limited to check | ||
which calls do and do not issue deprecation warnings. Behavior is still | ||
tested via the existing regular tests. | ||
""" | ||
with pytest.warns(mpl.MatplotlibDeprecationWarning): | ||
# multiple positional arguments | ||
FontProperties("Times", "italic") | ||
|
||
with pytest.warns(mpl.MatplotlibDeprecationWarning): | ||
# Mixed positional and keyword arguments | ||
FontProperties("Times", size=10) | ||
|
||
with pytest.warns(mpl.MatplotlibDeprecationWarning): | ||
# passing a family list positionally | ||
FontProperties(["Times"]) | ||
|
||
# still accepted: | ||
FontProperties(family="Times", style="italic") | ||
FontProperties(family="Times") | ||
FontProperties("Times") # works as pattern and family | ||
FontProperties("serif-24:style=oblique:weight=bold") # pattern | ||
|
||
# also still accepted: | ||
# passing as pattern via family kwarg was not covered by the docs but | ||
# historically worked. This is left unchanged for now. | ||
# AFAICT, we cannot detect this: We can determine whether a string | ||
# works as pattern, but that doesn't help, because there are strings | ||
# that are both pattern and family. We would need to identify, whether | ||
# 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") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The docstring on 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). There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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).
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 |
Uh oh!
There was an error while loading. Please reload this page.