From 38988f289f7f5708f5ea85de2d5dfe0d86b23106 Mon Sep 17 00:00:00 2001 From: Sergey Fedoseev Date: Sat, 29 Jul 2017 19:16:43 +0500 Subject: [PATCH] Avoided creation of temporary sets. --- django/contrib/admin/views/main.py | 2 +- django/contrib/sessions/backends/file.py | 2 +- django/core/checks/registry.py | 2 +- django/core/management/__init__.py | 2 +- django/core/management/commands/compilemessages.py | 2 +- django/core/management/commands/makemessages.py | 2 +- django/db/migrations/autodetector.py | 8 ++++---- django/db/models/fields/related_descriptors.py | 2 +- django/db/models/sql/query.py | 9 ++++----- django/forms/models.py | 8 ++++---- django/urls/resolvers.py | 2 +- 11 files changed, 20 insertions(+), 21 deletions(-) diff --git a/django/contrib/admin/views/main.py b/django/contrib/admin/views/main.py index 6d89ead6ca..541d1aacc4 100644 --- a/django/contrib/admin/views/main.py +++ b/django/contrib/admin/views/main.py @@ -268,7 +268,7 @@ class ChangeList: # ordering fields so we can guarantee a deterministic order across all # database backends. pk_name = self.lookup_opts.pk.name - if not (set(ordering) & {'pk', '-pk', pk_name, '-' + pk_name}): + if {'pk', '-pk', pk_name, '-' + pk_name}.isdisjoint(ordering): # The two sets do not intersect, meaning the pk isn't present. So # we add it. ordering.append('-pk') diff --git a/django/contrib/sessions/backends/file.py b/django/contrib/sessions/backends/file.py index 54ab4f2e4d..540228b790 100644 --- a/django/contrib/sessions/backends/file.py +++ b/django/contrib/sessions/backends/file.py @@ -52,7 +52,7 @@ class SessionStore(SessionBase): # Make sure we're not vulnerable to directory traversal. Session keys # should always be md5s, so they should never contain directory # components. - if not set(session_key).issubset(set(VALID_KEY_CHARS)): + if not set(session_key).issubset(VALID_KEY_CHARS): raise InvalidSessionKey( "Invalid characters in session key") diff --git a/django/core/checks/registry.py b/django/core/checks/registry.py index 4f7a9618c8..a2bb71ea37 100644 --- a/django/core/checks/registry.py +++ b/django/core/checks/registry.py @@ -67,7 +67,7 @@ class CheckRegistry: if tags is not None: checks = [check for check in checks - if hasattr(check, 'tags') and set(check.tags) & set(tags)] + if hasattr(check, 'tags') and not set(check.tags).isdisjoint(tags)] else: # By default, 'database'-tagged checks are not run as they do more # than mere static code analysis. diff --git a/django/core/management/__init__.py b/django/core/management/__init__.py index 2599114489..0a1b2e4f53 100644 --- a/django/core/management/__init__.py +++ b/django/core/management/__init__.py @@ -122,7 +122,7 @@ def call_command(command_name, *args, **options): # Raise an error if any unknown options were passed. stealth_options = set(command.base_stealth_options + command.stealth_options) dest_parameters = {action.dest for action in parser._actions} - valid_options = dest_parameters | stealth_options | set(opt_mapping) + valid_options = (dest_parameters | stealth_options).union(opt_mapping) unknown_options = set(options) - valid_options if unknown_options: raise TypeError( diff --git a/django/core/management/commands/compilemessages.py b/django/core/management/commands/compilemessages.py index ba65c0e528..fcbe24b1f3 100644 --- a/django/core/management/commands/compilemessages.py +++ b/django/core/management/commands/compilemessages.py @@ -85,7 +85,7 @@ class Command(BaseCommand): # Account for excluded locales locales = locale or all_locales - locales = set(locales) - set(exclude) + locales = set(locales).difference(exclude) for basedir in basedirs: if locales: diff --git a/django/core/management/commands/makemessages.py b/django/core/management/commands/makemessages.py index 63e52da6d9..0151644a3a 100644 --- a/django/core/management/commands/makemessages.py +++ b/django/core/management/commands/makemessages.py @@ -371,7 +371,7 @@ class Command(BaseCommand): locales = all_locales else: locales = locale or all_locales - locales = set(locales) - set(exclude) + locales = set(locales).difference(exclude) if locales: check_programs('msguniq', 'msgmerge', 'msgattrib') diff --git a/django/db/migrations/autodetector.py b/django/db/migrations/autodetector.py index 2973995d40..3c69754cdf 100644 --- a/django/db/migrations/autodetector.py +++ b/django/db/migrations/autodetector.py @@ -451,12 +451,12 @@ class MigrationAutodetector: """ self.renamed_models = {} self.renamed_models_rel = {} - added_models = set(self.new_model_keys) - set(self.old_model_keys) + added_models = set(self.new_model_keys).difference(self.old_model_keys) for app_label, model_name in sorted(added_models): model_state = self.to_state.models[app_label, model_name] model_fields_def = self.only_relation_agnostic_fields(model_state.fields) - removed_models = set(self.old_model_keys) - set(self.new_model_keys) + removed_models = set(self.old_model_keys).difference(self.new_model_keys) for rem_app_label, rem_model_name in removed_models: if rem_app_label == app_label: rem_model_state = self.from_state.models[rem_app_label, rem_model_name] @@ -642,7 +642,7 @@ class MigrationAutodetector: models it's safe to skip all the pointless field stuff and just chuck out an operation. """ - added = set(self.new_proxy_keys) - set(self.old_proxy_keys) + added = set(self.new_proxy_keys).difference(self.old_proxy_keys) for app_label, model_name in sorted(added): model_state = self.to_state.models[app_label, model_name] assert model_state.options.get("proxy") @@ -764,7 +764,7 @@ class MigrationAutodetector: def generate_deleted_proxies(self): """Make DeleteModel options for proxy models.""" - deleted = set(self.old_proxy_keys) - set(self.new_proxy_keys) + deleted = set(self.old_proxy_keys).difference(self.new_proxy_keys) for app_label, model_name in sorted(deleted): model_state = self.from_state.models[app_label, model_name] assert model_state.options.get("proxy") diff --git a/django/db/models/fields/related_descriptors.py b/django/db/models/fields/related_descriptors.py index 80eaac26d4..4371e44c0c 100644 --- a/django/db/models/fields/related_descriptors.py +++ b/django/db/models/fields/related_descriptors.py @@ -1044,7 +1044,7 @@ def create_forward_many_to_many_manager(superclass, rel, reverse): source_field_name: self.related_val[0], '%s__in' % target_field_name: new_ids, })) - new_ids = new_ids - set(vals) + new_ids.difference_update(vals) with transaction.atomic(using=db, savepoint=False): if self.reverse or source_field_name == self.source_field_name: diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py index c335d77c3c..b691fc424b 100644 --- a/django/db/models/sql/query.py +++ b/django/db/models/sql/query.py @@ -789,7 +789,7 @@ class Query: relabelling any references to them in select columns and the where clause. """ - assert set(change_map).intersection(set(change_map.values())) == set() + assert set(change_map).isdisjoint(change_map.values()) # 1. Update references in "select" (normal columns plus aliases), # "group by" and "where". @@ -975,7 +975,7 @@ class Query: def prepare_lookup_value(self, value, lookups, can_reuse, allow_joins=True): # Default lookup if none given is exact. - used_joins = [] + used_joins = set() if len(lookups) == 0: lookups = ['exact'] # Interpret '__exact=None' as the sql 'is NULL'; otherwise, reject all @@ -987,12 +987,11 @@ class Query: elif hasattr(value, 'resolve_expression'): pre_joins = self.alias_refcount.copy() value = value.resolve_expression(self, reuse=can_reuse, allow_joins=allow_joins) - used_joins = [k for k, v in self.alias_refcount.items() if v > pre_joins.get(k, 0)] + used_joins = {k for k, v in self.alias_refcount.items() if v > pre_joins.get(k, 0)} elif isinstance(value, (list, tuple)): # The items of the iterable may be expressions and therefore need # to be resolved independently. processed_values = [] - used_joins = set() for sub_value in value: if hasattr(sub_value, 'resolve_expression'): pre_joins = self.alias_refcount.copy() @@ -1172,7 +1171,7 @@ class Query: # Update used_joins before trimming since they are reused to determine # which joins could be later promoted to INNER. - used_joins = set(used_joins).union(set(join_list)) + used_joins.update(join_list) targets, alias, join_list = self.trim_joins(sources, join_list, path) if can_reuse is not None: can_reuse.update(join_list) diff --git a/django/forms/models.py b/django/forms/models.py index 37ef43b767..ffa8982506 100644 --- a/django/forms/models.py +++ b/django/forms/models.py @@ -258,8 +258,8 @@ class ModelFormMetaclass(DeclarativeFieldsMetaclass): ) # make sure opts.fields doesn't specify an invalid field - none_model_fields = [k for k, v in fields.items() if not v] - missing_fields = set(none_model_fields) - set(new_class.declared_fields) + none_model_fields = {k for k, v in fields.items() if not v} + missing_fields = none_model_fields.difference(new_class.declared_fields) if missing_fields: message = 'Unknown field(s) (%s) specified for %s' message = message % (', '.join(missing_fields), @@ -682,8 +682,8 @@ class BaseModelFormSet(BaseFormSet): for form in valid_forms: exclude = form._get_validation_exclusions() unique_checks, date_checks = form.instance._get_unique_checks(exclude=exclude) - all_unique_checks = all_unique_checks.union(set(unique_checks)) - all_date_checks = all_date_checks.union(set(date_checks)) + all_unique_checks.update(unique_checks) + all_date_checks.update(date_checks) errors = [] # Do each of the unique checks (unique and unique_together) diff --git a/django/urls/resolvers.py b/django/urls/resolvers.py index 03bac717f8..ecad10acea 100644 --- a/django/urls/resolvers.py +++ b/django/urls/resolvers.py @@ -436,7 +436,7 @@ class RegexURLResolver(LocaleRegexProvider): continue candidate_subs = dict(zip(params, text_args)) else: - if set(kwargs) | set(defaults) != set(params) | set(defaults): + if set(kwargs).symmetric_difference(params).difference(defaults): continue matches = True for k, v in defaults.items():