From fb12b0a0b20d34e4375be2714b24c257a8c6374e Mon Sep 17 00:00:00 2001 From: JakobDev Date: Tue, 26 Jul 2022 11:36:27 +0200 Subject: [PATCH 1/2] Add support for tomllib --- README.rst | 2 +- src/doc8/main.py | 16 ++++++++++------ 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/README.rst b/README.rst index 5d6992d..cc32a8f 100644 --- a/README.rst +++ b/README.rst @@ -102,7 +102,7 @@ instead. * ``$CWD/tox.ini`` * ``$CWD/pep8.ini`` * ``$CWD/setup.cfg`` -* ``$CWD/pyproject.toml`` in section ``[tool.doc8]`` if ``tomli`` is installed +* ``$CWD/pyproject.toml`` in section ``[tool.doc8]`` if ``tomli`` is installed or when using a Python version greater or equal than 3.11 An example section that can be placed into one of these files:: diff --git a/src/doc8/main.py b/src/doc8/main.py index b355362..9285b7b 100644 --- a/src/doc8/main.py +++ b/src/doc8/main.py @@ -31,16 +31,20 @@ import argparse import collections import configparser +import importlib import logging import os import sys -try: - import tomli - HAVE_TOML = True -except ImportError: - HAVE_TOML = False +HAVE_TOML = False +for module_name in ("tomllib", "tomli"): + try: + toml_module = importlib.import_module(module_name) + HAVE_TOML = True + break + except ModuleNotFoundError: + pass from stevedore import extension @@ -135,7 +139,7 @@ def from_ini(fp): def from_toml(fp): with open(fp, "rb") as f: - parsed = tomli.load(f).get("tool", {}).get("doc8", {}) + parsed = toml_module.load(f).get("tool", {}).get("doc8", {}) cfg = {} for key, value in parsed.items(): From 19311ab23c100d34aad80596d31e4832c50876b3 Mon Sep 17 00:00:00 2001 From: Sorin Sbarnea Date: Wed, 27 Jul 2022 13:11:37 +0100 Subject: [PATCH 2/2] Make toml support implicit --- README.rst | 2 +- setup.cfg | 1 + src/doc8/main.py | 30 ++++++++++++++++-------------- 3 files changed, 18 insertions(+), 15 deletions(-) diff --git a/README.rst b/README.rst index cc32a8f..71628eb 100644 --- a/README.rst +++ b/README.rst @@ -102,7 +102,7 @@ instead. * ``$CWD/tox.ini`` * ``$CWD/pep8.ini`` * ``$CWD/setup.cfg`` -* ``$CWD/pyproject.toml`` in section ``[tool.doc8]`` if ``tomli`` is installed or when using a Python version greater or equal than 3.11 +* ``$CWD/pyproject.toml`` An example section that can be placed into one of these files:: diff --git a/setup.cfg b/setup.cfg index baf0db5..d8b680a 100644 --- a/setup.cfg +++ b/setup.cfg @@ -63,6 +63,7 @@ install_requires = docutils restructuredtext-lint>=0.7 stevedore + tomli; python_version < '3.11' Pygments [options.entry_points] diff --git a/src/doc8/main.py b/src/doc8/main.py index 9285b7b..7aa80d6 100644 --- a/src/doc8/main.py +++ b/src/doc8/main.py @@ -31,20 +31,17 @@ import argparse import collections import configparser -import importlib import logging import os import sys -HAVE_TOML = False -for module_name in ("tomllib", "tomli"): - try: - toml_module = importlib.import_module(module_name) - HAVE_TOML = True - break - except ModuleNotFoundError: - pass +try: + # py3.11+ + from tomllib import load as toml_load # type: ignore +except ImportError: + # py3.10 or older + from tomli import load as toml_load from stevedore import extension @@ -55,9 +52,14 @@ FILE_PATTERNS = [".rst", ".txt"] MAX_LINE_LENGTH = 79 -CONFIG_FILENAMES = ["doc8.ini", ".config/doc8.ini", "tox.ini", "pep8.ini", "setup.cfg"] -if HAVE_TOML: - CONFIG_FILENAMES.extend(["pyproject.toml"]) +CONFIG_FILENAMES = [ + "doc8.ini", + ".config/doc8.ini", + "tox.ini", + "pep8.ini", + "setup.cfg", + "pyproject.toml", +] def split_set_type(text, delimiter=","): @@ -139,7 +141,7 @@ def from_ini(fp): def from_toml(fp): with open(fp, "rb") as f: - parsed = toml_module.load(f).get("tool", {}).get("doc8", {}) + parsed = toml_load(f).get("tool", {}).get("doc8", {}) cfg = {} for key, value in parsed.items(): @@ -161,7 +163,7 @@ def extract_config(args): continue if cfg_file.endswith((".ini", ".cfg")): cfg = from_ini(cfg_file) - elif cfg_file.endswith(".toml") and HAVE_TOML: + elif cfg_file.endswith(".toml"): cfg = from_toml(cfg_file) if cfg: break