Opened 7 years ago
Closed 7 years ago
#28795 closed Cleanup/optimization (fixed)
Remove 'not in' checks and use dict.setdefault()
Reported by: | Дилян Палаузов | Owned by: | nobody |
---|---|---|---|
Component: | Core (Other) | Version: | dev |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Ready for checkin | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
diff --git a/django/contrib/gis/apps.py b/django/contrib/gis/apps.py --- a/django/contrib/gis/apps.py +++ b/django/contrib/gis/apps.py @@ -8,5 +8,4 @@ class GISConfig(AppConfig): verbose_name = _("GIS") def ready(self): - if 'geojson' not in serializers.BUILTIN_SERIALIZERS: - serializers.BUILTIN_SERIALIZERS['geojson'] = "django.contrib.gis.serializers.geojson" + serializers.BUILTIN_SERIALIZERS.setdefault('geojson', "django.contrib.gis.serializers.geojson") diff --git a/django/contrib/gis/db/models/fields.py b/django/contrib/gis/db/models/fields.py --- a/django/contrib/gis/db/models/fields.py +++ b/django/contrib/gis/db/models/fields.py @@ -255,9 +255,9 @@ class GeometryField(BaseSpatialField): 'srid': self.srid, } defaults.update(kwargs) - if (self.dim > 2 and 'widget' not in kwargs and + if (self.dim > 2 and not getattr(defaults['form_class'].widget, 'supports_3d', False)): - defaults['widget'] = forms.Textarea + defaults.setdefault('widget', forms.Textarea) return super().formfield(**defaults) def select_format(self, compiler, sql, params): diff --git a/django/contrib/gis/gdal/raster/source.py b/django/contrib/gis/gdal/raster/source.py --- a/django/contrib/gis/gdal/raster/source.py +++ b/django/contrib/gis/gdal/raster/source.py @@ -381,27 +381,14 @@ class GDALRaster(GDALRasterBase): consult the GDAL_RESAMPLE_ALGORITHMS constant. """ # Get the parameters defining the geotransform, srid, and size of the raster - if 'width' not in ds_input: - ds_input['width'] = self.width - - if 'height' not in ds_input: - ds_input['height'] = self.height - - if 'srid' not in ds_input: - ds_input['srid'] = self.srs.srid - - if 'origin' not in ds_input: - ds_input['origin'] = self.origin - - if 'scale' not in ds_input: - ds_input['scale'] = self.scale - - if 'skew' not in ds_input: - ds_input['skew'] = self.skew - + ds_input.setdefault('width', self.width) + ds_input.setdefault('height', self.height) + ds_input.setdefault('srid', self.srs.srid) + ds_input.setdefault('origin', self.origin) + ds_input.setdefault('scale', self.scale) + ds_input.setdefault('skew', self.skew) # Get the driver, name, and datatype of the target raster - if 'driver' not in ds_input: - ds_input['driver'] = self.driver.name + ds_input.setdefault('driver', self.driver.name) if 'name' not in ds_input: ds_input['name'] = self.name + '_copy.' + self.driver.name diff --git a/django/db/migrations/operations/models.py b/django/db/migrations/operations/models.py --- a/django/db/migrations/operations/models.py +++ b/django/db/migrations/operations/models.py @@ -693,8 +693,8 @@ class AlterModelOptions(ModelOptionOperation): model_state.options = dict(model_state.options) model_state.options.update(self.options) for key in self.ALTER_OPTION_KEYS: - if key not in self.options and key in model_state.options: - del model_state.options[key] + if key not in self.options: + model_state.options.pop(key, False) state.reload_model(app_label, self.name_lower, delay=True) def database_forwards(self, app_label, schema_editor, from_state, to_state): diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py --- a/django/db/models/sql/query.py +++ b/django/db/models/sql/query.py @@ -692,8 +692,7 @@ class Query: # in the parent list. Again, it must be mentioned to ensure that # only "must include" fields are pulled in. for model in orig_opts.get_parent_list(): - if model not in seen: - seen[model] = set() + seen.setdefault(model, set()) for model, values in seen.items(): callback(target, model, values) diff --git a/django/forms/boundfield.py b/django/forms/boundfield.py --- a/django/forms/boundfield.py +++ b/django/forms/boundfield.py @@ -88,11 +88,11 @@ class BoundField: attrs = attrs or {} attrs = self.build_widget_attrs(attrs, widget) auto_id = self.auto_id - if auto_id and 'id' not in attrs and 'id' not in widget.attrs: + if auto_id and 'id' not in widget.attrs: if not only_initial: - attrs['id'] = auto_id + attrs.setdefault('id', auto_id) else: - attrs['id'] = self.html_initial_id + attrs.setdefault('id', self.html_initial_id) if not only_initial: name = self.html_name diff --git a/django/http/response.py b/django/http/response.py --- a/django/http/response.py +++ b/django/http/response.py @@ -136,10 +136,7 @@ class HttpResponseBase: self._headers[header.lower()] = (header, value) def __delitem__(self, header): - try: - del self._headers[header.lower()] - except KeyError: - pass + self._headers.pop(header.lower(), False) def __getitem__(self, header): return self._headers[header.lower()][1] diff --git a/django/middleware/locale.py b/django/middleware/locale.py --- a/django/middleware/locale.py +++ b/django/middleware/locale.py @@ -57,6 +57,5 @@ class LocaleMiddleware(MiddlewareMixin): if not (i18n_patterns_used and language_from_path): patch_vary_headers(response, ('Accept-Language',)) - if 'Content-Language' not in response: - response['Content-Language'] = language + response.setdefault('Content-Language', language) return response diff --git a/django/middleware/security.py b/django/middleware/security.py --- a/django/middleware/security.py +++ b/django/middleware/security.py @@ -37,10 +37,10 @@ class SecurityMiddleware(MiddlewareMixin): sts_header = sts_header + "; preload" response["strict-transport-security"] = sts_header - if self.content_type_nosniff and 'x-content-type-options' not in response: - response["x-content-type-options"] = "nosniff" + if self.content_type_nosniff: + response.setdefault("x-content-type-options", "nosniff") - if self.xss_filter and 'x-xss-protection' not in response: - response["x-xss-protection"] = "1; mode=block" + if self.xss_filter: + response.setdefault("x-xss-protection", "1; mode=block") return response diff --git a/django/template/defaulttags.py b/django/template/defaulttags.py --- a/django/template/defaulttags.py +++ b/django/template/defaulttags.py @@ -225,8 +225,7 @@ class IfChangedNode(Node): def render(self, context): # Init state storage state_frame = self._get_context_stack_frame(context) - if self not in state_frame: - state_frame[self] = None + state_frame.setdefault(self) nodelist_true_output = None if self._varlist: diff --git a/django/views/decorators/http.py b/django/views/decorators/http.py --- a/django/views/decorators/http.py +++ b/django/views/decorators/http.py @@ -103,8 +103,8 @@ def condition(etag_func=None, last_modified_func=None): if request.method in ('GET', 'HEAD'): if res_last_modified and not response.has_header('Last-Modified'): response['Last-Modified'] = http_date(res_last_modified) - if res_etag and not response.has_header('ETag'): - response['ETag'] = res_etag + if res_etag: + response.setdefault('ETag', res_etag) return response diff --git a/django/views/generic/base.py b/django/views/generic/base.py --- a/django/views/generic/base.py +++ b/django/views/generic/base.py @@ -21,8 +21,7 @@ class ContextMixin: extra_context = None def get_context_data(self, **kwargs): - if 'view' not in kwargs: - kwargs['view'] = self + kwargs.setdefault('view', self) if self.extra_context is not None: kwargs.update(self.extra_context) return kwargs diff --git a/tests/runtests.py b/tests/runtests.py --- a/tests/runtests.py +++ b/tests/runtests.py @@ -455,8 +455,7 @@ if __name__ == "__main__": if options.settings: os.environ['DJANGO_SETTINGS_MODULE'] = options.settings else: - if "DJANGO_SETTINGS_MODULE" not in os.environ: - os.environ['DJANGO_SETTINGS_MODULE'] = 'test_sqlite' + os.environ.setdefault("DJANGO_SETTINGS_MODULE", 'test_sqlite') options.settings = os.environ['DJANGO_SETTINGS_MODULE'] if options.selenium:
Change History (3)
comment:1 by , 7 years ago
Component: | Uncategorized → Core (Other) |
---|---|
Has patch: | set |
Triage Stage: | Unreviewed → Ready for checkin |
comment:2 by , 7 years ago
Summary: | Utilize dict.setdefault() → Remove 'not in' checks and use dict.setdefault() |
---|
Note:
See TracTickets
for help on using tickets.
PR from the patch.