Skip to content

Remove the six dependency and delete bmemcached/compat.py #266

Description

@jaysonsantos

Part of the repository modernization effort.

Background

The package depends on six and on bmemcached/compat.py. Both exist only to support Python 2. The project targets Python 3 only. See the minimum-version issue.

bmemcached/compat.py gives three names: pickle, long, and unicode. On Python 3, long is int and unicode is str.

Inventory

six in runtime code

Location Current code Change to
setup.py:35 "six" in install_requires Remove the entry
bmemcached/compat.py:1,9 import six, if six.PY3: Delete the file
bmemcached/utils.py:1,16 six.binary_type, six.string_types bytes, str
bmemcached/protocol.py:14-15 import six, from six import binary_type, text_type bytes, str. Delete the imports
bmemcached/protocol.py:428 if six.PY3: branch and the Python 2 branch after it Keep return value.decode('utf8'). Delete the Python 2 branch
bmemcached/client/mixin.py:1,68 import six, isinstance(servers, six.string_types) isinstance(servers, str)

six in docstrings only

These sites hold no runtime code. Reword six.string_types to str and six.binary_type to bytes.

  • bmemcached/protocol.py:283,285,357,405,409,450,512,514,577,625,651,687,713,889,923,941,958,1051
  • bmemcached/client/mixin.py:14,16,104,153
  • bmemcached/client/replicating.py:71,110,210,319,357,416,438
  • bmemcached/client/distributed.py:128,152,176,232,258,277

six in tests

Five files hold this shim:

if six.PY3:
    from unittest import mock
else:
    import mock

Sites: test/test_auth.py:4,9-12, test/test_server_parsing.py:4,8-11, test/test_errors.py:4,9-12, test/test_compression.py:4,9-12, test/test_simple_functions.py:5,12-15.

Replace each shim with from unittest import mock.

six.assertCountEqual(self, ...) appears at test/test_simple_functions.py:37,46,117,127,137,250,565,637,647,657,694 and test/test_error_handling.py:151,157,214. Replace each call with self.assertCountEqual(...). unittest.TestCase holds this method since Python 3.2.

Remove import six from test/test_error_handling.py:4.

bmemcached/compat.py consumers

  • bmemcached/protocol.py:17 — from bmemcached.compat import long, pickle
  • bmemcached/client/mixin.py:4 — from bmemcached.compat import pickle
  • bmemcached/client/distributed.py:6 — from bmemcached.compat import pickle
  • test/test_simple_functions.py:10 — from bmemcached.compat import long, unicode

Replace each pickle import with import pickle. The cPickle fallback has no purpose on Python 3.

In test/test_simple_functions.py, replace long at lines 48, 55, 260, 262, 263, 710, 712, 713 with int. Replace unicode at line 43 with str.

Warning: keep wire compatibility for the long flag

bmemcached/protocol.py:95 declares 'long': 1 << 2 in FLAGS. Two branches use it.

The write branch at bmemcached/protocol.py:373-375 is already dead:

elif isinstance(value, long) and isinstance(value, bool) is False:
    flags |= self.FLAGS['long']

compat.long is int on Python 3. The isinstance(value, int) check at lines 371-372 always matches first. Delete lines 373-375.

The read branch at bmemcached/protocol.py:421-422 must stay:

elif flags & FLAGS['long']:
    return long(value)

An old client version can have written a value with this flag. Keep the branch. Change long(value) to int(value). Keep the FLAGS['long'] constant.

Acceptance criteria

  • No file in bmemcached/ or test/ imports six.
  • setup.py drops six from install_requires.
  • bmemcached/compat.py is deleted.
  • The write branch at bmemcached/protocol.py:373-375 is deleted.
  • The read branch at bmemcached/protocol.py:421-422 stays and calls int(value).
  • FLAGS['long'] stays in bmemcached/protocol.py:95.
  • mock is removed from requirements_test.txt.
  • python -m compileall -q bmemcached test passes.
  • The full test suite passes. It needs a local memcached binary on PATH.

Files to change

bmemcached/compat.py (delete), bmemcached/utils.py, bmemcached/protocol.py, bmemcached/client/mixin.py, bmemcached/client/replicating.py, bmemcached/client/distributed.py, setup.py, requirements_test.txt, test/test_auth.py, test/test_server_parsing.py, test/test_errors.py, test/test_compression.py, test/test_simple_functions.py, test/test_error_handling.py.

Order

Do the minimum-version issue first.

Activity

  1. jaysonsantos commented on Sep 8, 2026

    @jaysonsantos
    OwnerAuthor

    Tracked in #277.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    modernizationRepo modernization effortpython-versionSupported Python version changes

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions