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

Skip to content

[TYP/MNT] Remove unused imports from stub files #25787

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
May 16, 2023

Conversation

ksunden
Copy link
Member

@ksunden ksunden commented Apr 28, 2023

PR Summary

mostly performed by running pycln, but then manually audited

After it was brought up in #25728 (comment), I did a bit of a review, and consulted the python/typeshed repo for guidance on best practices for type stub files.
That repo has a precommit hook to run pycln -a on all files, which removes any imports that are implementation details that are not used in the public interface of the module.

Since that is the prevailing best practice of the relative authority on the matter, I decided to run pycln over our code.

The following additional changes were made after auditing the results:

  • The <...>_COLORS dictionaries are explicitly exported in the colors module
    • that they are imported is an implementation detail, but it is public within this module, as far as I can tell
  • TextPath is exported from the text module.
    • despite existing in the textpath module which is at least arguably public (at least by way of not having a prepended _)
    • TextPath is documented in text and the textpath module is undocumented
  • A few others I questioned, but decided were not intended to be public api:
    • Helper functions in font_manager
    • legend_handler in the legend module (legend_handler is its own public module, should be imported directly)
    • Helper functions for layout_manager (even the ones that are documented contain a warning that they are considered private)
  • mathtext reexports values from _mathtext
  • A few import foo as foo are removed as those are holdout from generating the stub files and are not intended to be exported
  • All changes to .py files were excluded, though perhaps some should be removed
    • Files that would have changes:
      • galleries/examples/statistics/time_series_histogram.py
      • galleries/examples/ticks/date_formatters_locators.py
      • lib/matplotlib/init.py
      • lib/matplotlib/axes/_axes.py
      • lib/matplotlib/backends/backend_template.py
      • lib/matplotlib/backends/qt_compat.py
      • lib/matplotlib/pylab.py
      • lib/matplotlib/pyplot.py
      • lib/matplotlib/tests/test_determinism.py
    • many of these are actually intended to be exported, cause side effects, or are used via eval (in galleries)
    • Unused imports are generally caught by flake8 ci, but have several instances of per-file-ignores for that error

pycln could be added as a pre-commit hook here if we were interested, but would need to be properly configured, and current personal leaning is that it is not a big enough boon to do so, though checking when the topic comes up like it did here may be warranted.

PR Checklist

Linked Issue

  • [N/A] Added "closes #0000" in the PR description to link it to the original issue.

Documentation and Tests

  • [N/A] Has pytest style unit tests (and pytest passes)
  • [N/A] Documentation is sphinx and numpydoc compliant (the docs should build without error).
  • [N/A] New plotting related features are documented with examples.

Release Notes

  • [N/A] New features are marked with a .. versionadded:: directive in the docstring and documented in doc/users/next_whats_new/
  • [N/A] API changes are marked with a .. versionchanged:: directive in the docstring and documented in doc/api/next_api_changes/
  • [N/A] Release notes conform with instructions in next_whats_new/README.rst or next_api_changes/README.rst

@ksunden ksunden changed the title Remove unused imports from stub files [TYP/MNT] Remove unused imports from stub files Apr 28, 2023
@oscargus
Copy link
Member

Should we run isort on these as well?

@ksunden
Copy link
Member Author

ksunden commented Apr 28, 2023

We could do so, though may wish to add config to e.g. group the mpl.typing, typing (stdlib), and numpy.typing imports together (potentially with collections.abc as well, as that is also rather related)

Not opposed, but also don't really see it as a huge gain.

from matplotlib import cbook, ft2font
from matplotlib._fontconfig_pattern import (
generate_fontconfig_pattern,
parse_fontconfig_pattern,
Copy link
Member

Choose a reason for hiding this comment

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

Weirdly, this one is public enough to appear in API change notes, but not public enough to be in API documentation.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, I was debating on this one, current leaning is to leave it out of stubs until and unless someone complains, which I kind of doubt will happen.

mostly performed by running pycln, but then manually audited
@ksunden
Copy link
Member Author

ksunden commented May 15, 2023

Gentle ping on this one

@tacaswell tacaswell merged commit 9b13a56 into matplotlib:main May 16, 2023
@ksunden ksunden deleted the pycln branch May 16, 2023 21:32
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