From 074a2f7f58cfab807ae72b09e634cad30a895369 Mon Sep 17 00:00:00 2001 From: Mariusz Felisiak Date: Mon, 26 Feb 2018 18:23:31 +0100 Subject: [PATCH] Refs #28909 -- Simplifed code using unpacking generalizations. --- django/db/backends/base/base.py | 10 ++++------ django/db/backends/base/creation.py | 4 +--- django/db/backends/sqlite3/creation.py | 4 +--- django/db/models/query.py | 4 +--- django/forms/widgets.py | 12 +++--------- django/template/utils.py | 12 +++++++----- django/utils/autoreload.py | 3 +-- django/utils/datastructures.py | 4 +--- 8 files changed, 19 insertions(+), 34 deletions(-) diff --git a/django/db/backends/base/base.py b/django/db/backends/base/base.py index 53c3c30063..c8c18f5b19 100644 --- a/django/db/backends/base/base.py +++ b/django/db/backends/base/base.py @@ -589,13 +589,11 @@ class BaseDatabaseWrapper: potential child threads while (or after) the test database is destroyed. Refs #10868, #17786, #16969. """ - settings_dict = self.settings_dict.copy() - settings_dict['NAME'] = None - nodb_connection = self.__class__( - settings_dict, + return self.__class__( + {**self.settings_dict, 'NAME': None}, alias=NO_DB_ALIAS, - allow_thread_sharing=False) - return nodb_connection + allow_thread_sharing=False, + ) def _start_transaction_under_autocommit(self): """ diff --git a/django/db/backends/base/creation.py b/django/db/backends/base/creation.py index 1b2542a9f5..fe1d1b4dea 100644 --- a/django/db/backends/base/creation.py +++ b/django/db/backends/base/creation.py @@ -221,9 +221,7 @@ class BaseDatabaseCreation: # already and its name has been copied to settings_dict['NAME'] so # we don't need to call _get_test_db_name. orig_settings_dict = self.connection.settings_dict - new_settings_dict = orig_settings_dict.copy() - new_settings_dict['NAME'] = '{}_{}'.format(orig_settings_dict['NAME'], suffix) - return new_settings_dict + return {**orig_settings_dict, 'NAME': '{}_{}'.format(orig_settings_dict['NAME'], suffix)} def _clone_test_db(self, suffix, verbosity, keepdb=False): """ diff --git a/django/db/backends/sqlite3/creation.py b/django/db/backends/sqlite3/creation.py index 2c45f63c2b..345954eb49 100644 --- a/django/db/backends/sqlite3/creation.py +++ b/django/db/backends/sqlite3/creation.py @@ -51,10 +51,8 @@ class DatabaseCreation(BaseDatabaseCreation): if self.is_in_memory_db(source_database_name): return orig_settings_dict else: - new_settings_dict = orig_settings_dict.copy() root, ext = os.path.splitext(orig_settings_dict['NAME']) - new_settings_dict['NAME'] = '{}_{}.{}'.format(root, suffix, ext) - return new_settings_dict + return {**orig_settings_dict, 'NAME': '{}_{}.{}'.format(root, suffix, ext)} def _clone_test_db(self, suffix, verbosity, keepdb=False): source_database_name = self.connection.settings_dict['NAME'] diff --git a/django/db/models/query.py b/django/db/models/query.py index 44bc51d0e0..71ebf660c7 100644 --- a/django/db/models/query.py +++ b/django/db/models/query.py @@ -219,9 +219,7 @@ class QuerySet: def __getstate__(self): # Force the cache to be fully populated. self._fetch_all() - obj_dict = self.__dict__.copy() - obj_dict[DJANGO_VERSION_PICKLE_KEY] = get_version() - return obj_dict + return {**self.__dict__, DJANGO_VERSION_PICKLE_KEY: get_version()} def __setstate__(self, state): msg = None diff --git a/django/forms/widgets.py b/django/forms/widgets.py index c0e3349aba..f3a6b38f46 100644 --- a/django/forms/widgets.py +++ b/django/forms/widgets.py @@ -958,35 +958,29 @@ class SelectDateWidget(Widget): year_choices = [(i, str(i)) for i in self.years] if not self.is_required: year_choices.insert(0, self.year_none_value) - year_attrs = context['widget']['attrs'].copy() year_name = self.year_field % name - year_attrs['id'] = 'id_%s' % year_name date_context['year'] = self.select_widget(attrs, choices=year_choices).get_context( name=year_name, value=context['widget']['value']['year'], - attrs=year_attrs, + attrs={**context['widget']['attrs'], 'id': 'id_%s' % year_name}, ) month_choices = list(self.months.items()) if not self.is_required: month_choices.insert(0, self.month_none_value) - month_attrs = context['widget']['attrs'].copy() month_name = self.month_field % name - month_attrs['id'] = 'id_%s' % month_name date_context['month'] = self.select_widget(attrs, choices=month_choices).get_context( name=month_name, value=context['widget']['value']['month'], - attrs=month_attrs, + attrs={**context['widget']['attrs'], 'id': 'id_%s' % month_name}, ) day_choices = [(i, i) for i in range(1, 32)] if not self.is_required: day_choices.insert(0, self.day_none_value) - day_attrs = context['widget']['attrs'].copy() day_name = self.day_field % name - day_attrs['id'] = 'id_%s' % day_name date_context['day'] = self.select_widget(attrs, choices=day_choices,).get_context( name=day_name, value=context['widget']['value']['day'], - attrs=day_attrs, + attrs={**context['widget']['attrs'], 'id': 'id_%s' % day_name}, ) subwidgets = [] for field in self._parse_date_fmt(): diff --git a/django/template/utils.py b/django/template/utils.py index fde2f64d13..03f0554c42 100644 --- a/django/template/utils.py +++ b/django/template/utils.py @@ -30,7 +30,6 @@ class EngineHandler: templates = OrderedDict() backend_names = [] for tpl in self._templates: - tpl = tpl.copy() try: # This will raise an exception if 'BACKEND' doesn't exist or # isn't a string containing at least one dot. @@ -41,10 +40,13 @@ class EngineHandler: "Invalid BACKEND for a template engine: {}. Check " "your TEMPLATES setting.".format(invalid_backend)) - tpl.setdefault('NAME', default_name) - tpl.setdefault('DIRS', []) - tpl.setdefault('APP_DIRS', False) - tpl.setdefault('OPTIONS', {}) + tpl = { + 'NAME': default_name, + 'DIRS': [], + 'APP_DIRS': False, + 'OPTIONS': {}, + **tpl, + } templates[tpl['NAME']] = tpl backend_names.append(tpl['NAME']) diff --git a/django/utils/autoreload.py b/django/utils/autoreload.py index 08ba598787..7e83a7fd8c 100644 --- a/django/utils/autoreload.py +++ b/django/utils/autoreload.py @@ -289,8 +289,7 @@ def restart_with_reloader(): args += sys.argv[1:] else: args += sys.argv - new_environ = os.environ.copy() - new_environ["RUN_MAIN"] = 'true' + new_environ = {**os.environ, 'RUN_MAIN': 'true'} exit_code = subprocess.call(args, env=new_environ) if exit_code != 3: return exit_code diff --git a/django/utils/datastructures.py b/django/utils/datastructures.py index 2f02a2c74d..9c52bd3ae2 100644 --- a/django/utils/datastructures.py +++ b/django/utils/datastructures.py @@ -100,9 +100,7 @@ class MultiValueDict(dict): return result def __getstate__(self): - obj_dict = self.__dict__.copy() - obj_dict['_data'] = {k: self._getlist(k) for k in self} - return obj_dict + return {**self.__dict__, '_data': {k: self._getlist(k) for k in self}} def __setstate__(self, obj_dict): data = obj_dict.pop('_data', {})