Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add type checking #277

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
Open

Add type checking #277

wants to merge 18 commits into from

Conversation

JonathonReinhart
Copy link
Owner

No description provided.

@JonathonReinhart JonathonReinhart force-pushed the add-type-checking branch 5 times, most recently from fdbd2d8 to 3a4679d Compare January 14, 2024 18:33
This adds an initial [tool.mypy] section to pyproject.toml which checks
modules which have no typing errors.

We set python_version = "3.7" to ensure we are always checking against
the syntax and stdlib of our lowest supported Python version.

This also adds mypy to requirements.txt for development.
This also updates the single() implementation, replacing the use of
_SENTINEL as a nothing-to-return marker with a separate bool variable.

Note that we need to use both collections.abc.Iterable (for isinstance()) and
typing.Iterable (for type annotation) until Python 3.9 (#265). If we
attempt to use the former for type annotation in Python < 3.9, we get
the following error:

    TypeError: 'ABCMeta' object is not subscriptable

This also simplifies coerce_sequence() to always return a list, removing
the return-type argument.
This updates git_describe() to return Optional[GitDescribe] to simplify
typing.

The whole file looks much closer now to this (and in some ways better):
https://github.com/JonathonReinhart/scuba/blob/18135bb19a/scuba/version.py

This also adds importlib-metadata to requirements.txt for development.
This is because even though we're running with Python 3.11 on Debian 12,
we specify python_version = "3.8" in pyproject.toml, which means the
backport is needed for type checking.
For some reason, running mypy v1.8.0 --python-version=3.8 in a
Python 3.11 venv doesn't pick up the importlib_resources types,
even when the (runtime-unnecessary) importlib-resources package is
installed, so we have to add a cast.
Also take the opportunity to clean up some wildcard imports.
One slightly annoying issue is that I like to set attributes to None in
close() after they've been closed. But this requires marking their types
as Optional, which requires checking that they are not None where they
are used. A well-polished API would check and raise a "closed"
exception. For now, we just assert.

This also fixes #278.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant