Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Convert FontEntry to a data class #20118

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 2 commits into from
May 14, 2021
Merged

Convert FontEntry to a data class #20118

merged 2 commits into from
May 14, 2021

Conversation

QuLogic
Copy link
Member

@QuLogic QuLogic commented Apr 30, 2021

PR Summary

With Python 3.7, we now have data classes. I'm not sure if we want to start moving towards those, so I just converted one small one for discussion purposes.

(Plus a little cleanup to drop a flake8 exception)

PR Checklist

  • Has pytest style unit tests (and pytest passes).
  • Is Flake 8 compliant (run flake8 on changed files to check).
  • [n/a] New features are documented, with examples if plot related.
  • Documentation is sphinx and numpydoc compliant (the docs should build without error).
  • Conforms to Matplotlib style conventions (install flake8-docstrings and run flake8 --docstring-convention=all).
  • [n/a] New features have an entry in doc/users/next_whats_new/ (follow instructions in README.rst there).
  • [?] API changes documented in doc/api/next_api_changes/ (follow instructions in README.rst there).

@QuLogic QuLogic added this to the v3.5.0 milestone Apr 30, 2021
Comment on lines -366 to -368
try:
self.size = str(float(size))
except ValueError:
self.size = size
Copy link
Member Author

@QuLogic QuLogic Apr 30, 2021

Choose a reason for hiding this comment

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

Note this is only called in two places, always with size='scalable', so there's no point to this conversion.

@anntzer
Copy link
Contributor

anntzer commented May 1, 2021

I would vaguely prefer using make_dataclass (as I did in #18517), possibly together with bases or namespace, because I don't like type annotations, but perhaps I'm just being old and grumpy here...

@QuLogic
Copy link
Member Author

QuLogic commented May 8, 2021

I did so, though I'm not sure it's quite as nice.

@tacaswell
Copy link
Member

This appears to have actually broken the docs


/home/circleci/project/lib/matplotlib/font_manager.py:docstring of matplotlib.font_manager.afmFontProperty:25: WARNING: py:obj reference target not found: FontEntry
/home/circleci/project/lib/matplotlib/font_manager.py:docstring of matplotlib.font_manager.ttfFontProperty:25: WARNING: py:obj reference target not found: FontEntry

@tacaswell
Copy link
Member

The explicit construction is probably a bit more resilient to future changes CPython, but I suspect the type annotation version is more readable.

On the other hand, we do not (to my knowledge) have any other type annotations in the code base and given that annotations are still in flux (see the discussion about PEP563) I see the reasoning to keep it that way.

@QuLogic
Copy link
Member Author

QuLogic commented May 12, 2021

Strange, it does appear to have a docstring; Sphinx just doesn't appear to collect it.

QuLogic added 2 commits May 11, 2021 20:33
No need to split over multiple lines and align equals signs.
@QuLogic
Copy link
Member Author

QuLogic commented May 12, 2021

OK, I added an explicit entry for it, and that should fix the docs. I guess we could drop that if we switch to the class style.

Copy link
Member

@jklymak jklymak left a comment

Choose a reason for hiding this comment

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

I mean this is fine - I don't quite understand the point though. Why is this better than a normal class? It seems identical, but with an extra song and dance for the docstring?

@QuLogic
Copy link
Member Author

QuLogic commented May 12, 2021

Well, tbh, it was a more effective looking change when it was written with annotations. But really I want to get rid of more flake8 exceptions.

@tacaswell tacaswell merged commit b5a206e into matplotlib:master May 14, 2021
@QuLogic QuLogic deleted the fe-dc branch May 14, 2021 03:33
@QuLogic QuLogic removed the status: needs comment/discussion needs consensus on next step label May 14, 2021
QuLogic added a commit to QuLogic/matplotlib that referenced this pull request Dec 1, 2023
When these were added in matplotlib#20118, we had no type annotations, so it made
sense to use the functional form. Now that we do, there's no reason not
to use the class form.

Also, as `FontEntry` has gained more methods, the functional form looks
less clear.
@QuLogic QuLogic mentioned this pull request Dec 1, 2023
2 tasks
QuLogic added a commit to QuLogic/matplotlib that referenced this pull request Jan 13, 2024
When these were added in matplotlib#20118, we had no type annotations, so it made
sense to use the functional form. Now that we do, there's no reason not
to use the class form.

Also, as `FontEntry` has gained more methods, the functional form looks
less clear.
QuLogic added a commit to QuLogic/matplotlib that referenced this pull request Jan 13, 2024
When these were added in matplotlib#20118, we had no type annotations, so it made
sense to use the functional form. Now that we do, there's no reason not
to use the class form.

Also, as `FontEntry` has gained more methods, the functional form looks
less clear.
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