Replaced temporary lists used for passing arguments with iterables.

This commit is contained in:
Jon Dufresne 2017-04-25 21:04:10 -07:00 committed by Tim Graham
parent 309c10c2cb
commit 92bc727271
5 changed files with 43 additions and 40 deletions

View File

@ -87,10 +87,10 @@ class BaseModelAdminChecks:
if not isinstance(obj.raw_id_fields, (list, tuple)): if not isinstance(obj.raw_id_fields, (list, tuple)):
return must_be('a list or tuple', option='raw_id_fields', obj=obj, id='admin.E001') return must_be('a list or tuple', option='raw_id_fields', obj=obj, id='admin.E001')
else: else:
return list(chain(*[ return list(chain.from_iterable(
self._check_raw_id_fields_item(obj, obj.model, field_name, 'raw_id_fields[%d]' % index) self._check_raw_id_fields_item(obj, obj.model, field_name, 'raw_id_fields[%d]' % index)
for index, field_name in enumerate(obj.raw_id_fields) for index, field_name in enumerate(obj.raw_id_fields)
])) ))
def _check_raw_id_fields_item(self, obj, model, field_name, label): def _check_raw_id_fields_item(self, obj, model, field_name, label):
""" Check an item of `raw_id_fields`, i.e. check that field named """ Check an item of `raw_id_fields`, i.e. check that field named
@ -136,10 +136,10 @@ class BaseModelAdminChecks:
) )
] ]
return list(chain(*[ return list(chain.from_iterable(
self._check_field_spec(obj, obj.model, field_name, 'fields') self._check_field_spec(obj, obj.model, field_name, 'fields')
for field_name in obj.fields for field_name in obj.fields
])) ))
def _check_fieldsets(self, obj): def _check_fieldsets(self, obj):
""" Check that fieldsets is properly formatted and doesn't contain """ Check that fieldsets is properly formatted and doesn't contain
@ -150,10 +150,10 @@ class BaseModelAdminChecks:
elif not isinstance(obj.fieldsets, (list, tuple)): elif not isinstance(obj.fieldsets, (list, tuple)):
return must_be('a list or tuple', option='fieldsets', obj=obj, id='admin.E007') return must_be('a list or tuple', option='fieldsets', obj=obj, id='admin.E007')
else: else:
return list(chain(*[ return list(chain.from_iterable(
self._check_fieldsets_item(obj, obj.model, fieldset, 'fieldsets[%d]' % index) self._check_fieldsets_item(obj, obj.model, fieldset, 'fieldsets[%d]' % index)
for index, fieldset in enumerate(obj.fieldsets) for index, fieldset in enumerate(obj.fieldsets)
])) ))
def _check_fieldsets_item(self, obj, model, fieldset, label): def _check_fieldsets_item(self, obj, model, fieldset, label):
""" Check an item of `fieldsets`, i.e. check that this is a pair of a """ Check an item of `fieldsets`, i.e. check that this is a pair of a
@ -185,10 +185,10 @@ class BaseModelAdminChecks:
id='admin.E012', id='admin.E012',
) )
] ]
return list(chain(*[ return list(chain.from_iterable(
self._check_field_spec(obj, model, fieldset_fields, '%s[1]["fields"]' % label) self._check_field_spec(obj, model, fieldset_fields, '%s[1]["fields"]' % label)
for fieldset_fields in fieldset[1]['fields'] for fieldset_fields in fieldset[1]['fields']
])) ))
def _check_field_spec(self, obj, model, fields, label): def _check_field_spec(self, obj, model, fields, label):
""" `fields` should be an item of `fields` or an item of """ `fields` should be an item of `fields` or an item of
@ -196,10 +196,10 @@ class BaseModelAdminChecks:
field name or a tuple of field names. """ field name or a tuple of field names. """
if isinstance(fields, tuple): if isinstance(fields, tuple):
return list(chain(*[ return list(chain.from_iterable(
self._check_field_spec_item(obj, model, field_name, "%s[%d]" % (label, index)) self._check_field_spec_item(obj, model, field_name, "%s[%d]" % (label, index))
for index, field_name in enumerate(fields) for index, field_name in enumerate(fields)
])) ))
else: else:
return self._check_field_spec_item(obj, model, fields, label) return self._check_field_spec_item(obj, model, fields, label)
@ -266,10 +266,10 @@ class BaseModelAdminChecks:
elif not isinstance(obj.filter_vertical, (list, tuple)): elif not isinstance(obj.filter_vertical, (list, tuple)):
return must_be('a list or tuple', option='filter_vertical', obj=obj, id='admin.E017') return must_be('a list or tuple', option='filter_vertical', obj=obj, id='admin.E017')
else: else:
return list(chain(*[ return list(chain.from_iterable(
self._check_filter_item(obj, obj.model, field_name, "filter_vertical[%d]" % index) self._check_filter_item(obj, obj.model, field_name, "filter_vertical[%d]" % index)
for index, field_name in enumerate(obj.filter_vertical) for index, field_name in enumerate(obj.filter_vertical)
])) ))
def _check_filter_horizontal(self, obj): def _check_filter_horizontal(self, obj):
""" Check that filter_horizontal is a sequence of field names. """ """ Check that filter_horizontal is a sequence of field names. """
@ -279,10 +279,10 @@ class BaseModelAdminChecks:
elif not isinstance(obj.filter_horizontal, (list, tuple)): elif not isinstance(obj.filter_horizontal, (list, tuple)):
return must_be('a list or tuple', option='filter_horizontal', obj=obj, id='admin.E018') return must_be('a list or tuple', option='filter_horizontal', obj=obj, id='admin.E018')
else: else:
return list(chain(*[ return list(chain.from_iterable(
self._check_filter_item(obj, obj.model, field_name, "filter_horizontal[%d]" % index) self._check_filter_item(obj, obj.model, field_name, "filter_horizontal[%d]" % index)
for index, field_name in enumerate(obj.filter_horizontal) for index, field_name in enumerate(obj.filter_horizontal)
])) ))
def _check_filter_item(self, obj, model, field_name, label): def _check_filter_item(self, obj, model, field_name, label):
""" Check one item of `filter_vertical` or `filter_horizontal`, i.e. """ Check one item of `filter_vertical` or `filter_horizontal`, i.e.
@ -307,11 +307,11 @@ class BaseModelAdminChecks:
elif not isinstance(obj.radio_fields, dict): elif not isinstance(obj.radio_fields, dict):
return must_be('a dictionary', option='radio_fields', obj=obj, id='admin.E021') return must_be('a dictionary', option='radio_fields', obj=obj, id='admin.E021')
else: else:
return list(chain(*[ return list(chain.from_iterable(
self._check_radio_fields_key(obj, obj.model, field_name, 'radio_fields') + self._check_radio_fields_key(obj, obj.model, field_name, 'radio_fields') +
self._check_radio_fields_value(obj, val, 'radio_fields["%s"]' % field_name) self._check_radio_fields_value(obj, val, 'radio_fields["%s"]' % field_name)
for field_name, val in obj.radio_fields.items() for field_name, val in obj.radio_fields.items()
])) ))
def _check_radio_fields_key(self, obj, model, field_name, label): def _check_radio_fields_key(self, obj, model, field_name, label):
""" Check that a key of `radio_fields` dictionary is name of existing """ Check that a key of `radio_fields` dictionary is name of existing
@ -377,11 +377,11 @@ class BaseModelAdminChecks:
elif not isinstance(obj.prepopulated_fields, dict): elif not isinstance(obj.prepopulated_fields, dict):
return must_be('a dictionary', option='prepopulated_fields', obj=obj, id='admin.E026') return must_be('a dictionary', option='prepopulated_fields', obj=obj, id='admin.E026')
else: else:
return list(chain(*[ return list(chain.from_iterable(
self._check_prepopulated_fields_key(obj, obj.model, field_name, 'prepopulated_fields') + self._check_prepopulated_fields_key(obj, obj.model, field_name, 'prepopulated_fields') +
self._check_prepopulated_fields_value(obj, obj.model, val, 'prepopulated_fields["%s"]' % field_name) self._check_prepopulated_fields_value(obj, obj.model, val, 'prepopulated_fields["%s"]' % field_name)
for field_name, val in obj.prepopulated_fields.items() for field_name, val in obj.prepopulated_fields.items()
])) ))
def _check_prepopulated_fields_key(self, obj, model, field_name, label): def _check_prepopulated_fields_key(self, obj, model, field_name, label):
""" Check a key of `prepopulated_fields` dictionary, i.e. check that it """ Check a key of `prepopulated_fields` dictionary, i.e. check that it
@ -413,10 +413,10 @@ class BaseModelAdminChecks:
if not isinstance(val, (list, tuple)): if not isinstance(val, (list, tuple)):
return must_be('a list or tuple', option=label, obj=obj, id='admin.E029') return must_be('a list or tuple', option=label, obj=obj, id='admin.E029')
else: else:
return list(chain(*[ return list(chain.from_iterable(
self._check_prepopulated_fields_value_item(obj, model, subfield_name, "%s[%r]" % (label, index)) self._check_prepopulated_fields_value_item(obj, model, subfield_name, "%s[%r]" % (label, index))
for index, subfield_name in enumerate(val) for index, subfield_name in enumerate(val)
])) ))
def _check_prepopulated_fields_value_item(self, obj, model, field_name, label): def _check_prepopulated_fields_value_item(self, obj, model, field_name, label):
""" For `prepopulated_fields` equal to {"slug": ("title",)}, """ For `prepopulated_fields` equal to {"slug": ("title",)},
@ -438,10 +438,10 @@ class BaseModelAdminChecks:
elif not isinstance(obj.ordering, (list, tuple)): elif not isinstance(obj.ordering, (list, tuple)):
return must_be('a list or tuple', option='ordering', obj=obj, id='admin.E031') return must_be('a list or tuple', option='ordering', obj=obj, id='admin.E031')
else: else:
return list(chain(*[ return list(chain.from_iterable(
self._check_ordering_item(obj, obj.model, field_name, 'ordering[%d]' % index) self._check_ordering_item(obj, obj.model, field_name, 'ordering[%d]' % index)
for index, field_name in enumerate(obj.ordering) for index, field_name in enumerate(obj.ordering)
])) ))
def _check_ordering_item(self, obj, model, field_name, label): def _check_ordering_item(self, obj, model, field_name, label):
""" Check that `ordering` refers to existing fields. """ """ Check that `ordering` refers to existing fields. """
@ -482,10 +482,10 @@ class BaseModelAdminChecks:
elif not isinstance(obj.readonly_fields, (list, tuple)): elif not isinstance(obj.readonly_fields, (list, tuple)):
return must_be('a list or tuple', option='readonly_fields', obj=obj, id='admin.E034') return must_be('a list or tuple', option='readonly_fields', obj=obj, id='admin.E034')
else: else:
return list(chain(*[ return list(chain.from_iterable(
self._check_readonly_fields_item(obj, obj.model, field_name, "readonly_fields[%d]" % index) self._check_readonly_fields_item(obj, obj.model, field_name, "readonly_fields[%d]" % index)
for index, field_name in enumerate(obj.readonly_fields) for index, field_name in enumerate(obj.readonly_fields)
])) ))
def _check_readonly_fields_item(self, obj, model, field_name, label): def _check_readonly_fields_item(self, obj, model, field_name, label):
if callable(field_name): if callable(field_name):
@ -553,10 +553,10 @@ class ModelAdminChecks(BaseModelAdminChecks):
if not isinstance(obj.inlines, (list, tuple)): if not isinstance(obj.inlines, (list, tuple)):
return must_be('a list or tuple', option='inlines', obj=obj, id='admin.E103') return must_be('a list or tuple', option='inlines', obj=obj, id='admin.E103')
else: else:
return list(chain(*[ return list(chain.from_iterable(
self._check_inlines_item(obj, obj.model, item, "inlines[%d]" % index) self._check_inlines_item(obj, obj.model, item, "inlines[%d]" % index)
for index, item in enumerate(obj.inlines) for index, item in enumerate(obj.inlines)
])) ))
def _check_inlines_item(self, obj, model, inline, label): def _check_inlines_item(self, obj, model, inline, label):
""" Check one inline model admin. """ """ Check one inline model admin. """
@ -592,10 +592,10 @@ class ModelAdminChecks(BaseModelAdminChecks):
if not isinstance(obj.list_display, (list, tuple)): if not isinstance(obj.list_display, (list, tuple)):
return must_be('a list or tuple', option='list_display', obj=obj, id='admin.E107') return must_be('a list or tuple', option='list_display', obj=obj, id='admin.E107')
else: else:
return list(chain(*[ return list(chain.from_iterable(
self._check_list_display_item(obj, obj.model, item, "list_display[%d]" % index) self._check_list_display_item(obj, obj.model, item, "list_display[%d]" % index)
for index, item in enumerate(obj.list_display) for index, item in enumerate(obj.list_display)
])) ))
def _check_list_display_item(self, obj, model, item, label): def _check_list_display_item(self, obj, model, item, label):
if callable(item): if callable(item):
@ -663,10 +663,10 @@ class ModelAdminChecks(BaseModelAdminChecks):
return must_be('a list, a tuple, or None', option='list_display_links', obj=obj, id='admin.E110') return must_be('a list, a tuple, or None', option='list_display_links', obj=obj, id='admin.E110')
# Check only if ModelAdmin.get_list_display() isn't overridden. # Check only if ModelAdmin.get_list_display() isn't overridden.
elif obj.get_list_display.__func__ is ModelAdmin.get_list_display: elif obj.get_list_display.__func__ is ModelAdmin.get_list_display:
return list(chain(*[ return list(chain.from_iterable(
self._check_list_display_links_item(obj, field_name, "list_display_links[%d]" % index) self._check_list_display_links_item(obj, field_name, "list_display_links[%d]" % index)
for index, field_name in enumerate(obj.list_display_links) for index, field_name in enumerate(obj.list_display_links)
])) ))
return [] return []
def _check_list_display_links_item(self, obj, field_name, label): def _check_list_display_links_item(self, obj, field_name, label):
@ -687,10 +687,10 @@ class ModelAdminChecks(BaseModelAdminChecks):
if not isinstance(obj.list_filter, (list, tuple)): if not isinstance(obj.list_filter, (list, tuple)):
return must_be('a list or tuple', option='list_filter', obj=obj, id='admin.E112') return must_be('a list or tuple', option='list_filter', obj=obj, id='admin.E112')
else: else:
return list(chain(*[ return list(chain.from_iterable(
self._check_list_filter_item(obj, obj.model, item, "list_filter[%d]" % index) self._check_list_filter_item(obj, obj.model, item, "list_filter[%d]" % index)
for index, item in enumerate(obj.list_filter) for index, item in enumerate(obj.list_filter)
])) ))
def _check_list_filter_item(self, obj, model, item, label): def _check_list_filter_item(self, obj, model, item, label):
""" """
@ -775,10 +775,10 @@ class ModelAdminChecks(BaseModelAdminChecks):
if not isinstance(obj.list_editable, (list, tuple)): if not isinstance(obj.list_editable, (list, tuple)):
return must_be('a list or tuple', option='list_editable', obj=obj, id='admin.E120') return must_be('a list or tuple', option='list_editable', obj=obj, id='admin.E120')
else: else:
return list(chain(*[ return list(chain.from_iterable(
self._check_list_editable_item(obj, obj.model, item, "list_editable[%d]" % index) self._check_list_editable_item(obj, obj.model, item, "list_editable[%d]" % index)
for index, item in enumerate(obj.list_editable) for index, item in enumerate(obj.list_editable)
])) ))
def _check_list_editable_item(self, obj, model, field_name, label): def _check_list_editable_item(self, obj, model, field_name, label):
try: try:

View File

@ -86,7 +86,10 @@ class CheckRegistry:
return tag in self.tags_available(include_deployment_checks) return tag in self.tags_available(include_deployment_checks)
def tags_available(self, deployment_checks=False): def tags_available(self, deployment_checks=False):
return set(chain(*[check.tags for check in self.get_checks(deployment_checks) if hasattr(check, 'tags')])) return set(chain.from_iterable(
check.tags for check in self.get_checks(deployment_checks)
if hasattr(check, 'tags')
))
def get_checks(self, include_deployment_checks=False): def get_checks(self, include_deployment_checks=False):
checks = list(self.registered_checks) checks = list(self.registered_checks)

View File

@ -244,7 +244,7 @@ class Command(BaseCommand):
def all_items_equal(seq): def all_items_equal(seq):
return all(item == seq[0] for item in seq[1:]) return all(item == seq[0] for item in seq[1:])
merge_migrations_generations = zip(*[m.ancestry for m in merge_migrations]) merge_migrations_generations = zip(*(m.ancestry for m in merge_migrations))
common_ancestor_count = sum(1 for common_ancestor_generation common_ancestor_count = sum(1 for common_ancestor_generation
in takewhile(all_items_equal, merge_migrations_generations)) in takewhile(all_items_equal, merge_migrations_generations))
if not common_ancestor_count: if not common_ancestor_count:

View File

@ -51,7 +51,7 @@ class Media:
return self.render() return self.render()
def render(self): def render(self):
return mark_safe('\n'.join(chain(*[getattr(self, 'render_' + name)() for name in MEDIA_TYPES]))) return mark_safe('\n'.join(chain.from_iterable(getattr(self, 'render_' + name)() for name in MEDIA_TYPES)))
def render_js(self): def render_js(self):
return [ return [
@ -65,12 +65,12 @@ class Media:
# To keep rendering order consistent, we can't just iterate over items(). # To keep rendering order consistent, we can't just iterate over items().
# We need to sort the keys, and iterate over the sorted list. # We need to sort the keys, and iterate over the sorted list.
media = sorted(self._css.keys()) media = sorted(self._css.keys())
return chain(*[[ return chain.from_iterable([
format_html( format_html(
'<link href="{}" type="text/css" media="{}" rel="stylesheet" />', '<link href="{}" type="text/css" media="{}" rel="stylesheet" />',
self.absolute_path(path), medium self.absolute_path(path), medium
) for path in self._css[medium] ) for path in self._css[medium]
] for medium in media]) ] for medium in media)
def absolute_path(self, path): def absolute_path(self, path):
""" """

View File

@ -43,7 +43,7 @@ def debug(request):
# Return a lazy reference that computes connection.queries on access, # Return a lazy reference that computes connection.queries on access,
# to ensure it contains queries triggered after this function runs. # to ensure it contains queries triggered after this function runs.
context_extras['sql_queries'] = lazy( context_extras['sql_queries'] = lazy(
lambda: list(itertools.chain(*[connections[x].queries for x in connections])), lambda: list(itertools.chain.from_iterable(connections[x].queries for x in connections)),
list list
) )
return context_extras return context_extras