From 1a2fa1a52b7b497a0688cc52706a149527e78a05 Mon Sep 17 00:00:00 2001 From: JakobDev Date: Tue, 26 Jul 2022 11:36:27 +0200 Subject: [PATCH] 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():