Skip to content

Add py.typed and annotate the leaf modules #275

Description

@jaysonsantos

Part of the repository modernization effort.

Background

No module in bmemcached/ has type annotations. All type information lives in Sphinx docstring fields, for example bmemcached/protocol.py:239,288,363,409,452. A type checker cannot read these. Some are already stale.

bmemcached/client/mixin.py:153 holds one lone comment-style hint:

# type: (six.string_types, int) -> bool

No other line in that file matches it.

Two type-checker comments exist. Both mark Python 2 code:

  • bmemcached/protocol.py:7,9 — # type: ignore[import-not-found] on the urlparse fallback.
  • bmemcached/compat.py:5 — import pickle as pickle # type: ignore.

The Python 2 cleanup issues delete both.

bmemcached/py.typed does not exist. Downstream mypy and pyright users cannot read this package's types. See PEP 561.

No mypy config exists. mypy.ini is absent. pyproject.toml holds no [tool.mypy] table.

Scope

Annotate the leaf modules only. Do not annotate bmemcached/protocol.py in this issue.

bmemcached/protocol.py is the core module. Its COMMANDS dict-of-dicts and its tuple response handling need a TypedDict or NamedTuple refactor before they type cleanly. That is separate work.

Plan

  1. Add an empty bmemcached/py.typed file.
  2. Include py.typed in the built package. Add [tool.setuptools.package-data] to pyproject.toml, or add the path to MANIFEST.in.
  3. Annotate bmemcached/exceptions.py. It holds three classes.
  4. Annotate bmemcached/utils.py. It holds one function.
  5. Annotate bmemcached/client/constants.py. It holds constants only.
  6. Add mypy to the test dependency group.
  7. Add a [tool.mypy] table. Run --strict on those three files only.
  8. Wire the mypy run into tox.ini.

Follow-up order for later issues

Annotate in this order after this issue lands:

  1. bmemcached/client/mixin.py. It defines the shared NotImplementedError stubs that the other two clients override.
  2. bmemcached/client/distributed.py and bmemcached/client/replicating.py.
  3. bmemcached/protocol.py, with the TypedDict refactor.

Acceptance criteria

  • bmemcached/py.typed exists and is empty.
  • The built wheel holds bmemcached/py.typed. Check with python -m build and an unzip of the wheel.
  • bmemcached/exceptions.py, bmemcached/utils.py, and bmemcached/client/constants.py hold full annotations.
  • pyproject.toml holds a [tool.mypy] table.
  • mypy --strict passes on those three files.
  • tox.ini runs the mypy command.
  • mypy is in the test dependency group.

Files to change

bmemcached/py.typed (new), pyproject.toml, MANIFEST.in, bmemcached/exceptions.py, bmemcached/utils.py, bmemcached/client/constants.py, tox.ini.

Order

Do the six removal issue first. Do not annotate code that a later issue deletes.

Do the defect-fix issue first. That issue adds MemcachedException.__str__.

Do the PEP 621 issue first. This issue changes pyproject.toml.

Activity

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

    code-qualityCode quality, typing and defectsmodernizationRepo modernization effort

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions