#28769: Utilize 'x or y' in place of 'x if x else y'
-------------------------------------+-------------------------------------
     Reporter:  Дилян Палаузов       |                    Owner:  nobody
         Type:                       |                   Status:  new
  Cleanup/optimization               |
    Component:  Core (Other)         |                  Version:  1.11
     Severity:  Normal               |               Resolution:
     Keywords:                       |             Triage Stage:
                                     |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------

Comment (by Дилян Палаузов):

 This is already inconsistent in the code:
 {{{
 django/db/migrations/operations/models.py:48:        self.options =
 options or {}
 django/db/migrations/operations/models.py:49:        self.bases = bases or
 (models.Model,)
 django/db/migrations/operations/special.py:17:
 self.database_operations = database_operations or []
 django/db/migrations/operations/special.py:18:
 self.state_operations = state_operations or []
 django/db/migrations/operations/special.py:75:
 self.state_operations = state_operations or []
 django/db/migrations/operations/special.py:76:        self.hints = hints
 or {}
 django/db/migrations/operations/special.py:153:        self.hints = hints
 or {}
 django/db/migrations/state.py:88:        self.models = models or {}
 django/db/migrations/state.py:90:        self.real_apps = real_apps or []
 django/db/migrations/state.py:363:        self.options = options or {}
 django/db/migrations/state.py:365:        self.bases = bases or
 (models.Model, )
 django/db/migrations/state.py:366:        self.managers = managers or []
 }}}

 Adding ternary expression to avoid using incorrectly or/and is fine, but
 it was not added to start using "x if x else y" instead of "x or y".

 There are more places which can be shortened this way:
 {{{
 diff --git a/django/contrib/sessions/backends/file.py
 b/django/contrib/sessions/backends/file.py
 --- a/django/contrib/sessions/backends/file.py
 +++ b/django/contrib/sessions/backends/file.py
 @@ -73,10 +73,7 @@ class SessionStore(SessionBase):
          """
          Return the expiry time of the file storing the session's content.
          """
 -        expiry = session_data.get('_session_expiry')
 -        if not expiry:
 -            expiry = self._last_modification() +
 datetime.timedelta(seconds=settings.SESSION_COOKIE_AGE)
 -        return expiry
 +        return session_data.get('_session_expiry') or
 self._last_modification() +
 datetime.timedelta(seconds=settings.SESSION_COOKIE_AGE)

      def load(self):
          session_data = {}
 diff --git a/django/core/handlers/wsgi.py b/django/core/handlers/wsgi.py
 --- a/django/core/handlers/wsgi.py
 +++ b/django/core/handlers/wsgi.py
 @@ -66,13 +66,11 @@ class LimitedStream:
  class WSGIRequest(HttpRequest):
      def __init__(self, environ):
          script_name = get_script_name(environ)
 -        path_info = get_path_info(environ)
 -        if not path_info:
 +        path_info = get_path_info(environ) or '/'
              # Sometimes PATH_INFO exists, but is empty (e.g. accessing
              # the SCRIPT_NAME URL without a trailing slash). We really
 need to
              # operate as if they'd requested '/'. Not amazingly nice to
 force
              # the path like this, but should be harmless.
 -            path_info = '/'
          self.environ = environ
          self.path_info = path_info
          # be careful to only replace the first slash in the path because
 of
 diff --git a/django/core/management/commands/makemessages.py
 b/django/core/management/commands/makemessages.py
 --- a/django/core/management/commands/makemessages.py
 +++ b/django/core/management/commands/makemessages.py
 @@ -501,9 +501,7 @@ class Command(BaseCommand):
                              locale_dir = path
                              break
                      if not locale_dir:
 -                        locale_dir = self.default_locale_path
 -                    if not locale_dir:
 -                        locale_dir = NO_LOCALE_DIR
 +                        locale_dir = self.default_locale_path or
 NO_LOCALE_DIR
 all_files.append(self.translatable_file_class(dirpath, filename,
 locale_dir))
          return sorted(all_files)

 diff --git a/django/db/backends/sqlite3/creation.py
 b/django/db/backends/sqlite3/creation.py
 --- a/django/db/backends/sqlite3/creation.py
 +++ b/django/db/backends/sqlite3/creation.py
 @@ -12,9 +12,7 @@ class DatabaseCreation(BaseDatabaseCreation):
          return database_name == ':memory:' or 'mode=memory' in
 database_name

      def _get_test_db_name(self):
 -        test_database_name =
 self.connection.settings_dict['TEST']['NAME']
 -        if not test_database_name:
 -            test_database_name = ':memory:'
 +        test_database_name =
 self.connection.settings_dict['TEST']['NAME'] or ':memory:'
          if test_database_name == ':memory:':
              return 'file:memorydb_%s?mode=memory&cache=shared' %
 self.connection.alias
          return test_database_name
 diff --git a/django/utils/dateparse.py b/django/utils/dateparse.py
 --- a/django/utils/dateparse.py
 +++ b/django/utils/dateparse.py
 @@ -131,9 +131,7 @@ def parse_duration(value):
      Also supports ISO 8601 representation and PostgreSQL's day-time
 interval
      format.
      """
 -    match = standard_duration_re.match(value)
 -    if not match:
 -        match = iso8601_duration_re.match(value) or
 postgres_interval_re.match(value)
 +    match = standard_duration_re.match(value) or
 iso8601_duration_re.match(value) or postgres_interval_re.match(value)
      if match:
          kw = match.groupdict()
          days = datetime.timedelta(float(kw.pop('days', 0) or 0))
 }}}

-- 
Ticket URL: <https://code.djangoproject.com/ticket/28769#comment:2>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To post to this group, send email to django-updates@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/072.c29bb0d585c72b9c5a903e4834deb038%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to