Skip to content

Commit

Permalink
req: Do not require toml with Python >= 3.11
Browse files Browse the repository at this point in the history
Python 3.11 ships with the `tomllib` package in the standard library.
The optional dependency on `toml` is thus not necessary. Docs were
updated to simplify the installation, supposing that most users have
Python 3.11 or a later version.
  • Loading branch information
dbaty committed May 15, 2024
1 parent b6b7926 commit bf5b75f
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 16 deletions.
15 changes: 7 additions & 8 deletions docs/installation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,21 @@ Install with ``pip``, preferably in a `virtual environment`_:
$ python3 -m venv /path/to/your/virtualenv
$ source /path/to/your/virtualenv/bin/activate
$ pip install "check-oldies[toml]"
$ pip install "check-oldies"
.. note::

If you do not need to read TOML configuration files, you may omit the
optional dependency and install like this:
If you want to read TOML configuration files **and if you have
Python 3.10 or earlier**, you must install an optional dependency,
like this:

.. code-block:: bash
$ pip install check-oldies
$ pip install "check-oldies[toml]"
Configuration files are not required, but some options are only
available through configuration files. If you are not sure, do not
use the latter command. Instead, use ``pip install check-oldies[toml]``
as indicated above.
available through them. If you are not sure, use the latter
command.


.. _virtual environment: https://packaging.python.org/tutorials/installing-packages/#creating-virtual-environments
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ console-scripts =
forget-me-not = check_oldies.forget_me_not:main

[options.extras_require]
# This is only needed for Python < 3.11.
toml = toml

[options.packages.find]
Expand Down
17 changes: 17 additions & 0 deletions src/check_oldies/compat.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import enum
import pathlib


__all__ = ["StrEnum"]
Expand Down Expand Up @@ -36,3 +37,19 @@ def __new__(cls, *values):
def _generate_next_value_(name, start, count, last_values):
return name.lower()
# fmt: on


try:
import tomllib

def load_toml(path: pathlib.Path) -> dict:
with open(path, "rb") as fp:
return tomllib.load(fp)
except ImportError: # Python < 3.11
try:
import toml

def load_toml(path: pathlib.Path) -> dict:
return toml.load(path)
except ImportError:
load_toml = None
13 changes: 5 additions & 8 deletions src/check_oldies/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@
import subprocess
import sys


try:
import toml
except ImportError: # pragma: no cover
toml = None
from . import compat


PYPROJECT_FILENAME = "pyproject.toml"
Expand Down Expand Up @@ -34,13 +30,14 @@ def replace_dashes(options):


def read_from_configuration_file(path, tool_name):
if not toml: # pragma: no cover
if not compat.load_toml: # pragma: no cover
sys.exit(
'You must install with `pip install "check-oldies[toml]" '
"to read from TOML configuration files."
"to read TOML configuration files."
)

try:
conf = toml.load(path)
conf = compat.load_toml(path)
except Exception as exc: # pylint: disable=broad-except # pragma: no cover
sys.exit(f"Error reading {path}: {exc.args[-1]}")

Expand Down

0 comments on commit bf5b75f

Please sign in to comment.