Skip to content

[MNT]: Deprecate mutability of FontProperties #22495

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

Open
anntzer opened this issue Feb 18, 2022 · 5 comments
Open

[MNT]: Deprecate mutability of FontProperties #22495

anntzer opened this issue Feb 18, 2022 · 5 comments

Comments

@anntzer
Copy link
Contributor

anntzer commented Feb 18, 2022

Summary

Currently, FontProperties (FPs) are mutable, which makes caching based on them a bit tricky (#22323, #22487). Manipulation is also not so nice; e.g. the fonts_demo.py example needs to repeatedly copy a base FP instance and call setters on the copies. Moreover, if one writes

fp = FontProperties(...)
textobj.set_fontproperties(fp)

then later mutation of fp doesn't modify the fontproperties on textobj, because set_fontproperties explicitly makes a copy of its argument. While this kind of "isolate-from-later-changes" is something we typically aim for in Matplotlib (see recent PRs likewise isolating artists from later changes in passed-in data arrays), this also means that there is basically no functionality gain coming from mutable FPs (and at least one complication, in the case of caching). (If set_fontproperties did not copy, then mutability would provide a way for two text objects to share exactly the same FP while having to mutate only one of them.)

I would therefore argue that the various property setters on FP should be deprecated to ultimately make FPs immutable; instead, we should use with_foo() methods that return new instances with just a single property being changed (similar to the pathlib API, for example). More precisely, we can either have with_family(family), with_style(style), with_variant(variant), etc., or instead we could also have a single with_props(**kwargs) (called e.g. as with_props(family="Times", ...)); I don't have a very strong opinion there.

Proposed fix

No response

@timhoffm
Copy link
Member

Agree with immutability. Interface t.b.d. Could this even be a frozen dataclass?

@jklymak
Copy link
Member

jklymak commented Feb 19, 2022

I think you closed this accidentally?

@jklymak jklymak reopened this Feb 19, 2022
@anntzer
Copy link
Contributor Author

anntzer commented Feb 19, 2022

A frozen dataclass could possibly work, indeed.

@timhoffm
Copy link
Member

Sorry for closing, I hit the wrong button.

The change to data classes is a little larger than only removing setters. But it may be simpler to understand and use in the long run. I also don't expect that getters and setters in FontProperties are widely used, so the change could be bearable.

@tacaswell tacaswell added this to the future releases milestone Sep 18, 2024
timhoffm added a commit to timhoffm/matplotlib that referenced this issue Sep 18, 2024
Replace default initialization immediately followed by setting values
by initialization with values, i.e.

```
# before:
fp = FontProperties()
fp.set_[something](val)

# after
fp = FontProperties([something]=val)
```

This is clearer and additionally helps with the possible transition
of making FontProperties immutable, see matplotlib#22495.
@timhoffm
Copy link
Member

The with_foo() pattern is definitively an option (and we do the same for colormaps). However, we should have a multi-setter.fp.with_family(name).with_size(24).with_style('italics') is hard to read and would require a copy for each update.

The multi-setter must be the basis and should be written first. Optionally, we can then create with_foo()-type functions as a convenience layer on top. Naming fp.with_properties(family=name, size=24, style='italics') would be an option; or we ammend the existing copy() with the optional arguments: fp.copy(family=name, size=24, style='italics') - Read: "copy fp and replace family by name, size by 24, style by 'italics').

timhoffm added a commit to timhoffm/matplotlib that referenced this issue Sep 18, 2024
Replace default initialization immediately followed by setting values
by initialization with values, i.e.

```
# before:
fp = FontProperties()
fp.set_[something](val)

# after
fp = FontProperties([something]=val)
```

This is clearer and additionally helps with the possible transition
of making FontProperties immutable, see matplotlib#22495.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants