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

Skip to content

Clean up unused imports #12562

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

Closed
tkivisik opened this issue Oct 18, 2018 · 9 comments · Fixed by #13090
Closed

Clean up unused imports #12562

tkivisik opened this issue Oct 18, 2018 · 9 comments · Fixed by #13090
Milestone

Comments

@tkivisik
Copy link

Bug report

There are 41 unused imports according to:
https://lgtm.com/projects/g/matplotlib/matplotlib/alerts/?mode=list

They should be removed.

Matplotlib version - latest from the master branch.

@tkivisik
Copy link
Author

I will remove them.

@timhoffm
Copy link
Member

Please check carefully, what you are removing. There are some unused imports that can actually be removed, however, there are others which are intentionally in place (e.g. for making code elements accessible to the user from another namespace).

@WeatherGod
Copy link
Member

WeatherGod commented Oct 18, 2018 via email

@tkivisik
Copy link
Author

Thank you for the warnings. In that case retract from claiming this for myself and I leave this up for someone with more intimate knowledge of the project.

When some eager person would like to take this on, which approach would make you confident that no necessary imports are removed?

@timhoffm
Copy link
Member

I've removed some obviously unused imports.

As a rule of thumb:

  • don't change imports in pylab
  • it's mostly ok to remove module imports
  • removing imported methods or classes require careful consideration (in particular in widely used namespaces like matplotlib or pyplot). For other namespaces it depends if this could be considered part of the public API.

@tkivisik
Copy link
Author

Thanks for explaining the guidelines and removing unused imports.

@tacaswell
Copy link
Member

To add to @timhoffm 's list,

  • removing standard library / third-party imports is usually ok, removing internal imports usually is not.

@tacaswell tacaswell added this to the v3.1 milestone Oct 20, 2018
@WeatherGod
Copy link
Member

WeatherGod commented Oct 20, 2018 via email

@timhoffm
Copy link
Member

__all__ is just a very mild limitation. Basically, it

  • excludes non-listed elements from a wildcard import
  • defines the intention of a public interface (but essentially only visible to the reader of the source code)

As a result, most people won't notice the effect of __all__ anyway (you shouldn't do wildcard imports anyway and a library user does usually not check the source code).

In particular __all__ does neither prevent an explicit import nor does it exclude elements from dir() and thus completion.

For an explanation see e.g. http://xion.io/post/code/python-all-wild-imports.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants