Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mfonism/e12890d34dedc3e4de30efe2d7795184 to your computer and use it in GitHub Desktop.
Save mfonism/e12890d34dedc3e4de30efe2d7795184 to your computer and use it in GitHub Desktop.
This gist documents the steps that were taken to drop Python 2 support in Django User Accounts

Dropping Python 2 Support in Django User Accounts

This gist documents the steps that were taken to drop Python 2 support in Django User Accounts, an app in the Pinax project.

It is for most of the part similar to this other gist, which documents the steps that were taken to drop support in Pinax Ratings, another app in the Pinax project.


  • Remove Python 2 from test matrix

  • Remove redundant future statements/features

    print_functon and unicode_literals in this case, as they are enabled in Python 3 by default.

  • Drop compatibility handling in account.compat

    • The django.core.urlresolvers module was moved to django.urls in version 1.10, and deprecated in version 2.0

      The lowest version of Django in this app is 1.11, so we can safely use functions from the latter module.

    • Also, as of Django 1.10, user.is_authenticated is a property, and not a method.

      We can therefore safely drop that part of the compatibility handling, too.

  • Drop account.compat

    • account.compat.NoReverseMatch ==> django.urls.NoReverseMatch
    • account.compat.resolve ==> django.urls.resolve
    • account.compat.reverse ==> django.urls.reverse
    • account.compat.is_authenticated(user) ==> user.is_authenticated
  • Drop unicode compatibility handlers

  • Drop django.utils.six

    • django.utils.six.moves.reduce ==> functools.reduce
    • django.utils.six.StringIO ==> io.StringIO
    • django.utils.six.moves.urllib.parse.urlparse ==> urllib.parse.urlparse
  • Drop compatibility handlers for stdlib modules

    • collections.OrderedDict exists across all supported versions of Python
    • urlparse.urlparse ==> urllib.parse.urlparse
    • urlparse.urlunparse ==> urllib.parse.urlunparse
  • Prefer settings.MIDDLEWARE to settings.MIDDLEWARE_CLASSES

    • The MIDDLEWARE_CLASSES setting was deprecated in Django 1.10 and the MIDDLEWARE setting took precedence.
    • Support for the old-style MIDDLEWARE_CLASSES setting was dropped in Django 2.0
  • Update setup and docs


  • Fix issues identified by deepsource

    • PERFORMAMCE issue in account/models.py

      1 occurence of inheriting from object

    • ANTI-PATTERN in account/templatetags/account_tags.py

      1 occurrence of using len without comparison to determine if a sequence is empty

      if len(bits):
          ...

      Was changed to:

      if len(bits) > 0:
          ...
    • BUG RISK in account/auth_backends.py

      Match parameters in overriding methods with those in the respective overriden methods

      def authenticate(self, *args, **credentials):
          ...

      Was changed to

      def authenticate(self, request, username=None, password=None, **kwargs):
          ...

      Also, the body of the function was updated accordingly.

    • ANTI-PATTERN in account/models.py

      Drop occurrences of re-defining variables from outer scope.

      def now(self):
          now = datetime.datetime.utcnow().replace(tzinfo=pytz.timezone("UTC"))
          timezone = settings.TIME_ZONE if not self.timezone else self.timezone
          return now.astimezone(pytz.timezone(timezone))

      Was updated to

      def now(self):
          now = datetime.datetime.utcnow().replace(tzinfo=pytz.timezone("UTC"))
          tz = settings.TIME_ZONE if not self.timezone else self.timezone
          return now.astimezone(pytz.timezone(tz))

      In order to avoid shadowing the Python timezone library which was imported at the top-level


ALERT!: Deepsource also identified four style issues which I couldn't wrap my head around 😓.

@mfonism
Copy link
Author

mfonism commented Jan 22, 2020

TO-DO: Update Circle CI config file

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment