From bf5b75fdce5a173edfa3a28f02b2e7bbc7736d2a Mon Sep 17 00:00:00 2001 From: Damien Baty Date: Wed, 15 May 2024 17:13:34 +0200 Subject: [PATCH] req: Do not require `toml` with Python >= 3.11 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. --- docs/installation.rst | 15 +++++++-------- setup.cfg | 1 + src/check_oldies/compat.py | 17 +++++++++++++++++ src/check_oldies/configuration.py | 13 +++++-------- 4 files changed, 30 insertions(+), 16 deletions(-) diff --git a/docs/installation.rst b/docs/installation.rst index cbce49f..5087fa2 100644 --- a/docs/installation.rst +++ b/docs/installation.rst @@ -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 diff --git a/setup.cfg b/setup.cfg index b8d8e29..eb807c9 100644 --- a/setup.cfg +++ b/setup.cfg @@ -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] diff --git a/src/check_oldies/compat.py b/src/check_oldies/compat.py index fc5ecc1..dda0441 100644 --- a/src/check_oldies/compat.py +++ b/src/check_oldies/compat.py @@ -1,4 +1,5 @@ import enum +import pathlib __all__ = ["StrEnum"] @@ -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 diff --git a/src/check_oldies/configuration.py b/src/check_oldies/configuration.py index 088e662..02bfcbb 100644 --- a/src/check_oldies/configuration.py +++ b/src/check_oldies/configuration.py @@ -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" @@ -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]}")