-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Cleanup unused variables and imports #11994
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
Conversation
lib/matplotlib/__init__.py
Outdated
import inspect | ||
from inspect import Parameter |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Leave this import? (I tend to import classes (and nothing else) by unqualified name, as a rule.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we have a policy on this? It seems a bit imbalanced that we do
import inspect
from inspect import Parameter
inspect.signature()
Parameter
Generally, I would recommend that we do not import individual classes if they are only rarely used. Importing classes and functions pollutes the namespace more. Also, when it's just rarely used, I usually wouldn't know what Parameter
is, whereas inspect.Parameter
is quite clear.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://www.python.org/dev/peps/pep-0008/#imports
When importing a class from a class-containing module, it's usually okay to spell this:
from myclass import MyClass
from foo.bar.yourclass import YourClass
If this spelling causes local name clashes, then spell them explicitly:
import myclass
import foo.bar.yourclass
and use "myclass.MyClass" and "foo.bar.yourclass.YourClass".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://docs.python-guide.org/writing/structure/#modules
Using
from modu import func
[...] only advantage over a simplerimport modu
is that it will save a little typing. [...]
Being able to tell immediately where a class or function comes from, as in themodu.func
idiom, greatly improves code readability and understandability in all but the simplest single file projects.
Parameter
is just used in one context (4 occurences there). To me explicitness outweighs brevity here.
lib/matplotlib/cbook/__init__.py
Outdated
@@ -29,7 +26,6 @@ | |||
import types | |||
import warnings | |||
import weakref | |||
from weakref import WeakMethod |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same as above
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just used once: IMO explicitness > brevity
lib/matplotlib/cbook/__init__.py
Outdated
pid = os.getpid() | ||
if sys.platform == 'sunos5': | ||
try: | ||
a2 = Popen(['ps', '-p', '%d' % pid, '-o', 'osz'], | ||
stdout=PIPE).stdout.readlines() | ||
a2 = subprocess.Popen( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that should use check_output...
(or more precisely I think a utility function such as report_memory should probably not support sunos5, unless someone specifically asks for it...)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Didn't want to mix the rather technical changes in the PR with other code changes, to kkep the review simpler. But since you asked for it, here it is.
@@ -2009,7 +2007,7 @@ def _warn_external(message, category=None): | |||
etc.). | |||
""" | |||
frame = sys._getframe() | |||
for stacklevel in itertools.count(1): | |||
for stacklevel in itertools.count(1): # lgtm[py/unused-loop-variable] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
really, lgtm can't see that stacklevel is used below the loop?...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not using the loop variable within the loop is often an error, even if it's used afterwards. I think this is what they want to indicate here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Frankly, I don't like peppering the code with comments to appease bad linters.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, I think that the alert is a valid concern, and a statement that stacklevel
is intentionally not used in the loop is justified here. I did not put comments in other places where the linting was just not good enough.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess I should just write something like (untested)
def recur(f, x0):
return itertools.accumulate(lambda x, _: f(x), itertools.repeat(x0))
stacklevel = 1 + next(
i for i, fr in enumerate(recur(lambda fr: fr.f_back, sys._getframe()))
if not re.match(r"\A(matplotlib|mpl_toolkits)(\Z|\.)",
fr.f_globals["name"])
and keep lgtm happy :p
(abusing accumulate to repeatedly just apply a function is actually documented as an example in https://docs.python.org/3/library/itertools.html#itertools.accumulate...)
lib/matplotlib/figure.py
Outdated
@@ -23,16 +23,9 @@ | |||
from matplotlib import get_backend | |||
|
|||
import matplotlib.artist as martist | |||
from matplotlib.artist import Artist, allow_rasterization |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as above re: importing classes.
lib/matplotlib/font_manager.py
Outdated
@@ -1293,7 +1293,6 @@ def get_font(filename, hinting_factor=None): | |||
|
|||
def fc_match(pattern, fontext): | |||
fontexts = get_fontext_synonyms(fontext) | |||
ext = "." + fontext |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or just merge #10933...
lib/matplotlib/offsetbox.py
Outdated
@@ -33,7 +33,6 @@ | |||
from matplotlib.image import BboxImage | |||
|
|||
from matplotlib.patches import bbox_artist as mbbox_artist | |||
from matplotlib.text import _AnnotationBase |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yada yada per above
setupext.py
Outdated
import glob | ||
import hashlib | ||
import importlib | ||
import multiprocessing | ||
import os | ||
import pathlib | ||
from pathlib import Path |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That doesn't work: there's a class Path
later... (even if it does that'd be quite confusing)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The one place, where I thought it was ok to import a class directly because it's in the standard library...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
:)
@@ -1012,8 +1011,6 @@ def add_flags(self, ext): | |||
ext.define_macros.append(('FREETYPE_BUILD_TYPE', 'system')) | |||
|
|||
def do_custom_build(self): | |||
from pathlib import Path |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just replace Path by pathlib.Path here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
tools/make_icons.py
Outdated
@@ -19,11 +19,10 @@ | |||
|
|||
import numpy as np | |||
|
|||
from matplotlib import pyplot as plt | |||
from matplotlib.font_manager import FontProperties |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same as above
ac728aa
to
814768f
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm okay with everything here except the the LGTM comment that @anntzer pointed out. Since you can't run LGTM locally (right?), it seems superfluous.
lib/matplotlib/__init__.py
Outdated
@@ -119,10 +119,8 @@ | |||
import contextlib | |||
import distutils.version | |||
import functools | |||
import io | |||
import importlib |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should stay here to be sorted.
e26e471
to
ed06162
Compare
For now, I've reverted the changes to class imports, so that this can get merged and #12032 can follow up. Nevertheless, I think we should adopt a consistent import policy for classes. We currently have a mixture e.g. |
lib/matplotlib/__init__.py
Outdated
@@ -1612,7 +1611,8 @@ def param(func): | |||
_has_varkwargs = True | |||
else: | |||
_arg_names.append(p.name) | |||
data_param = Parameter('data', Parameter.KEYWORD_ONLY, default=None) | |||
data_param = Parameter( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
leave as was
lib/matplotlib/offsetbox.py
Outdated
@@ -35,7 +35,6 @@ | |||
from matplotlib.patches import bbox_artist as mbbox_artist | |||
from matplotlib.text import _AnnotationBase | |||
|
|||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not to start an endless discussion about whitespace but I'd put two blank lines here :p
6a4e7d9
to
5f6a510
Compare
Hey @timhoffm . Can you rebase and ping me once rebased? |
5f6a510
to
485e0f8
Compare
Ping @NelleV |
I really wish there was a way to tell github "hey, merge this if all CI pass". |
Thanks @timhoffm |
PR Summary
Continuation of #11958
This fixes a number of lgtm alerts: