Skip to content

bpo-30296 Remove unnecessary tuples, lists, sets, and dicts #1489

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 18, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Lib/_weakrefset.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,19 +157,19 @@ def issubset(self, other):
__le__ = issubset

def __lt__(self, other):
return self.data < set(ref(item) for item in other)
return self.data < set(map(ref, other))

def issuperset(self, other):
return self.data.issuperset(ref(item) for item in other)
__ge__ = issuperset

def __gt__(self, other):
return self.data > set(ref(item) for item in other)
return self.data > set(map(ref, other))

def __eq__(self, other):
if not isinstance(other, self.__class__):
return NotImplemented
return self.data == set(ref(item) for item in other)
return self.data == set(map(ref, other))

def symmetric_difference(self, other):
newset = self.copy()
Expand Down
2 changes: 1 addition & 1 deletion Lib/distutils/msvc9compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ def query_vcvarsall(version, arch="x86"):
"""Launch vcvarsall.bat and read the settings from its environment
"""
vcvarsall = find_vcvarsall(version)
interesting = set(("include", "lib", "libpath", "path"))
interesting = {"include", "lib", "libpath", "path"}
Copy link
Contributor

Choose a reason for hiding this comment

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

Yes to this one. Set literals are always a win.

result = {}

if vcvarsall is None:
Expand Down
4 changes: 2 additions & 2 deletions Lib/email/headerregistry.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,8 +369,8 @@ def groups(self):
@property
def addresses(self):
if self._addresses is None:
self._addresses = tuple([address for group in self._groups
for address in group.addresses])
self._addresses = tuple(address for group in self._groups
for address in group.addresses)
return self._addresses

Copy link
Contributor

Choose a reason for hiding this comment

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

This is okay.

Copy link
Member

Choose a reason for hiding this comment

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

The original variant, with list comprehension, is faster than the variant with a generator. The difference is up to 2 times for simple tests.

I would keep the original code.


Expand Down
2 changes: 1 addition & 1 deletion Lib/inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ def classify_class_attrs(cls):

mro = getmro(cls)
metamro = getmro(type(cls)) # for attributes stored in the metaclass
metamro = tuple([cls for cls in metamro if cls not in (type, object)])
metamro = tuple(cls for cls in metamro if cls not in (type, object))
Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, this one is fine.

Copy link
Member

Choose a reason for hiding this comment

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

I would keep the original code.

class_bases = (cls,) + mro
all_bases = class_bases + metamro
names = dir(cls)
Expand Down
4 changes: 2 additions & 2 deletions Lib/logging/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@ def configure_custom(self, config):
c = self.resolve(c)
props = config.pop('.', None)
# Check for valid identifiers
kwargs = dict([(k, config[k]) for k in config if valid_ident(k)])
kwargs = dict((k, config[k]) for k in config if valid_ident(k))
Copy link
Member

Choose a reason for hiding this comment

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

I would use a dict comprehension.

And the same for other change in this file.

result = c(**kwargs)
if props:
for name, value in props.items():
Expand Down Expand Up @@ -726,7 +726,7 @@ def configure_handler(self, config):
config['address'] = self.as_tuple(config['address'])
factory = klass
props = config.pop('.', None)
kwargs = dict([(k, config[k]) for k in config if valid_ident(k)])
kwargs = dict((k, config[k]) for k in config if valid_ident(k))
try:
result = factory(**kwargs)
except TypeError as te:
Expand Down
2 changes: 1 addition & 1 deletion Lib/multiprocessing/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ def get_all_start_methods(self):
else:
return ['fork', 'spawn']

DefaultContext.__all__ = list(x for x in dir(DefaultContext) if x[0] != '_')
DefaultContext.__all__ = [x for x in dir(DefaultContext) if x[0] != '_']
Copy link
Contributor

Choose a reason for hiding this comment

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

This is okay.


#
# Context types for fixed start method
Expand Down
3 changes: 1 addition & 2 deletions Lib/multiprocessing/forkserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,7 @@ def ensure_running(self):
if self._preload_modules:
desired_keys = {'main_path', 'sys_path'}
data = spawn.get_preparation_data('ignore')
data = dict((x,y) for (x,y) in data.items()
if x in desired_keys)
data = {x: y for x, y in data.items() if x in desired_keys}
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm ambivalent about this one.

else:
data = {}

Expand Down
2 changes: 1 addition & 1 deletion Lib/multiprocessing/sharedctypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def synchronized(obj, lock=None, ctx=None):
scls = class_cache[cls]
except KeyError:
names = [field[0] for field in cls._fields_]
d = dict((name, make_property(name)) for name in names)
d = {name: make_property(name) for name in names}
Copy link
Contributor

Choose a reason for hiding this comment

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

This is okay.

classname = 'Synchronized' + cls.__name__
scls = class_cache[cls] = type(classname, (SynchronizedBase,), d)
return scls(obj, lock, ctx)
Expand Down
5 changes: 1 addition & 4 deletions Lib/pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,7 @@ class _WindowsFlavour(_Flavour):

is_supported = (os.name == 'nt')

drive_letters = (
set(chr(x) for x in range(ord('a'), ord('z') + 1)) |
set(chr(x) for x in range(ord('A'), ord('Z') + 1))
)
drive_letters = set('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ')
ext_namespace_prefix = '\\\\?\\'

reserved_names = (
Expand Down
3 changes: 1 addition & 2 deletions Lib/pstats.py
Original file line number Diff line number Diff line change
Expand Up @@ -500,8 +500,7 @@ def add_callers(target, source):
if func in new_callers:
if isinstance(caller, tuple):
# format used by cProfile
new_callers[func] = tuple([i[0] + i[1] for i in
zip(caller, new_callers[func])])
new_callers[func] = tuple(i[0] + i[1] for i in zip(caller, new_callers[func]))
Copy link
Member

Choose a reason for hiding this comment

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

I would keep the original code.

Maybe unpack a tuple:

tuple([i + j for i, j in zip(caller, new_callers[func])])

else:
# format used by profile
new_callers[func] += caller
Expand Down
4 changes: 2 additions & 2 deletions Lib/symtable.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ class Function(SymbolTable):
__globals = None

def __idents_matching(self, test_func):
return tuple([ident for ident in self.get_identifiers()
if test_func(self._table.symbols[ident])])
return tuple(ident for ident in self.get_identifiers()
Copy link
Contributor

Choose a reason for hiding this comment

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

This is okay.

Copy link
Member

Choose a reason for hiding this comment

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

I would keep the original code.

if test_func(self._table.symbols[ident]))

def get_parameters(self):
if self.__params is None:
Expand Down
2 changes: 1 addition & 1 deletion Lib/tokenize.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ def _all_string_prefixes():
# 'rf'). The various permutations will be generated.
_valid_string_prefixes = ['b', 'r', 'u', 'f', 'br', 'fr']
# if we add binary f-strings, add: ['fb', 'fbr']
result = set([''])
result = {''}
Copy link
Contributor

Choose a reason for hiding this comment

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

This is okay. Set literals are almost always the right thing to do. My only misgiving is that the new code looks like an ascii emoticon.

for prefix in _valid_string_prefixes:
for t in _itertools.permutations(prefix):
# create a list with upper and lower versions of each
Expand Down
3 changes: 1 addition & 2 deletions Lib/traceback.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,8 +253,7 @@ def __init__(self, filename, lineno, name, *, lookup_line=True,
self._line = line
if lookup_line:
self.line
self.locals = \
dict((k, repr(v)) for k, v in locals.items()) if locals else None
self.locals = {k: repr(v) for k, v in locals.items()} if locals else None
Copy link
Contributor

Choose a reason for hiding this comment

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

This is okay.


def __eq__(self, other):
if isinstance(other, FrameSummary):
Expand Down
8 changes: 4 additions & 4 deletions Lib/turtle.py
Original file line number Diff line number Diff line change
Expand Up @@ -1175,7 +1175,7 @@ def _color(self, cstr):
cl = [16*int(cstr[h], 16) for h in cstr[1:]]
else:
raise TurtleGraphicsError("bad colorstring: %s" % cstr)
return tuple([c * self._colormode/255 for c in cl])
return tuple(c * self._colormode/255 for c in cl)
Copy link
Contributor

Choose a reason for hiding this comment

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

This is okay.

Copy link
Member

Choose a reason for hiding this comment

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

I would keep the original code.


def colormode(self, cmode=None):
"""Return the colormode or set it to 1.0 or 255.
Expand Down Expand Up @@ -2989,7 +2989,7 @@ def _getshapepoly(self, polygon, compound=False):
t11, t12, t21, t22 = l, 0, 0, l
elif self._resizemode == "noresize":
return polygon
return tuple([(t11*x + t12*y, t21*x + t22*y) for (x, y) in polygon])
return tuple((t11*x + t12*y, t21*x + t22*y) for (x, y) in polygon)
Copy link
Contributor

Choose a reason for hiding this comment

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

This is okay.

Copy link
Member

Choose a reason for hiding this comment

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

I would keep the original code.


def _drawturtle(self):
"""Manages the correct rendering of the turtle with respect to
Expand Down Expand Up @@ -3839,8 +3839,8 @@ def write_docstringdict(filename="turtle_docstringdict"):
docsdict[key] = eval(key).__doc__

with open("%s.py" % filename,"w") as f:
keys = sorted([x for x in docsdict.keys()
if x.split('.')[1] not in _alias_list])
keys = sorted(x for x in docsdict.keys()
Copy link
Contributor

Choose a reason for hiding this comment

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

This is okay.

Copy link
Member

Choose a reason for hiding this comment

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

.keys() can be removed. Iterate just docsdict.

if x.split('.')[1] not in _alias_list)
f.write('docsdict = {\n\n')
for key in keys[:-1]:
f.write('%s :\n' % repr(key))
Expand Down
2 changes: 1 addition & 1 deletion Lib/turtledemo/wikipedia.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def main():
sleep(1)

at = clock()
while any([t.undobufferentries() for t in s.turtles()]):
while any(t.undobufferentries() for t in s.turtles()):
Copy link
Contributor

Choose a reason for hiding this comment

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

This is okay.

for t in s.turtles():
t.undo()
et = clock()
Expand Down
11 changes: 5 additions & 6 deletions Lib/urllib/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -683,8 +683,8 @@ def redirect_request(self, req, fp, code, msg, headers, newurl):
newurl = newurl.replace(' ', '%20')

CONTENT_HEADERS = ("content-length", "content-type")
newheaders = dict((k, v) for k, v in req.headers.items()
if k.lower() not in CONTENT_HEADERS)
newheaders = {k: v for k, v in req.headers.items()
if k.lower() not in CONTENT_HEADERS}
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm ambivalent about this one.

return Request(newurl,
headers=newheaders,
origin_req_host=req.origin_req_host,
Expand Down Expand Up @@ -845,7 +845,7 @@ def add_password(self, realm, uri, user, passwd):
self.passwd[realm] = {}
for default_port in True, False:
reduced_uri = tuple(
[self.reduce_uri(u, default_port) for u in uri])
self.reduce_uri(u, default_port) for u in uri)
Copy link
Contributor

Choose a reason for hiding this comment

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

This is okay

Copy link
Member

Choose a reason for hiding this comment

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

I would keep the original code.

self.passwd[realm][reduced_uri] = (user, passwd)

def find_user_password(self, realm, authuri):
Expand Down Expand Up @@ -1286,8 +1286,7 @@ def do_open(self, http_class, req, **http_conn_args):
h.set_debuglevel(self._debuglevel)

headers = dict(req.unredirected_hdrs)
headers.update(dict((k, v) for k, v in req.headers.items()
if k not in headers))
headers.update((k, v) for k, v in req.headers.items() if k not in headers)
Copy link
Member

Choose a reason for hiding this comment

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

This changes the semantic. Updated headers is used in the generator expression. I don't know whether this is good.

I would use a dict comprehesion.


# TODO(jhylton): Should this be redesigned to handle
# persistent connections?
Expand All @@ -1299,7 +1298,7 @@ def do_open(self, http_class, req, **http_conn_args):
# So make sure the connection gets closed after the (only)
# request.
headers["Connection"] = "close"
headers = dict((name.title(), val) for name, val in headers.items())
headers = {name.title(): val for name, val in headers.items()}
Copy link
Contributor

Choose a reason for hiding this comment

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

This is okay. Sometimes the dict() form is clearer about its intention but in this case the colon form for the key/value is clearer than the (key, value) form.


if req._tunnel_host:
tunnel_headers = {}
Expand Down
4 changes: 2 additions & 2 deletions Tools/gdb/libpython.py
Original file line number Diff line number Diff line change
Expand Up @@ -1097,8 +1097,8 @@ def proxyval(self, visited):
return ProxyAlreadyVisited('(...)')
visited.add(self.as_address())

result = tuple([PyObjectPtr.from_pyobject_ptr(self[i]).proxyval(visited)
for i in safe_range(int_from_int(self.field('ob_size')))])
result = tuple(PyObjectPtr.from_pyobject_ptr(self[i]).proxyval(visited)
for i in safe_range(int_from_int(self.field('ob_size'))))
Copy link
Contributor

Choose a reason for hiding this comment

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

This is okay.

Copy link
Member

Choose a reason for hiding this comment

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

I would keep the original code.

return result

def write_repr(self, out, visited):
Expand Down
2 changes: 1 addition & 1 deletion Tools/scripts/byext.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def report(self):
columns.update(self.stats[ext])
cols = sorted(columns)
colwidth = {}
colwidth["ext"] = max([len(ext) for ext in exts])
colwidth["ext"] = max(map(len, exts))
minwidth = 6
self.stats["TOTAL"] = {}
for col in cols:
Expand Down
2 changes: 1 addition & 1 deletion Tools/unicode/makeunicodedata.py
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,7 @@ def makeunicodename(unicode, trace):
if name and name[0] != "<":
names[char] = name + chr(0)

print(len(list(n for n in names if n is not None)), "distinct names")
print(len([n for n in names if n is not None]), "distinct names")
Copy link
Contributor

Choose a reason for hiding this comment

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

This is okay


# collect unique words from names (note that we differ between
# words inside a sentence, and words ending a sentence. the
Expand Down