Skip to content

Commit 19311ab

Browse files
committed
Make toml support implicit
1 parent fb12b0a commit 19311ab

File tree

3 files changed

+18
-15
lines changed

3 files changed

+18
-15
lines changed

README.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ instead.
102102
* ``$CWD/tox.ini``
103103
* ``$CWD/pep8.ini``
104104
* ``$CWD/setup.cfg``
105-
* ``$CWD/pyproject.toml`` in section ``[tool.doc8]`` if ``tomli`` is installed or when using a Python version greater or equal than 3.11
105+
* ``$CWD/pyproject.toml``
106106

107107
An example section that can be placed into one of these files::
108108

setup.cfg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ install_requires =
6363
docutils
6464
restructuredtext-lint>=0.7
6565
stevedore
66+
tomli; python_version < '3.11'
6667
Pygments
6768

6869
[options.entry_points]

src/doc8/main.py

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,17 @@
3131
import argparse
3232
import collections
3333
import configparser
34-
import importlib
3534
import logging
3635
import os
3736
import sys
3837

3938

40-
HAVE_TOML = False
41-
for module_name in ("tomllib", "tomli"):
42-
try:
43-
toml_module = importlib.import_module(module_name)
44-
HAVE_TOML = True
45-
break
46-
except ModuleNotFoundError:
47-
pass
39+
try:
40+
# py3.11+
41+
from tomllib import load as toml_load # type: ignore
42+
except ImportError:
43+
# py3.10 or older
44+
from tomli import load as toml_load
4845

4946
from stevedore import extension
5047

@@ -55,9 +52,14 @@
5552

5653
FILE_PATTERNS = [".rst", ".txt"]
5754
MAX_LINE_LENGTH = 79
58-
CONFIG_FILENAMES = ["doc8.ini", ".config/doc8.ini", "tox.ini", "pep8.ini", "setup.cfg"]
59-
if HAVE_TOML:
60-
CONFIG_FILENAMES.extend(["pyproject.toml"])
55+
CONFIG_FILENAMES = [
56+
"doc8.ini",
57+
".config/doc8.ini",
58+
"tox.ini",
59+
"pep8.ini",
60+
"setup.cfg",
61+
"pyproject.toml",
62+
]
6163

6264

6365
def split_set_type(text, delimiter=","):
@@ -139,7 +141,7 @@ def from_ini(fp):
139141

140142
def from_toml(fp):
141143
with open(fp, "rb") as f:
142-
parsed = toml_module.load(f).get("tool", {}).get("doc8", {})
144+
parsed = toml_load(f).get("tool", {}).get("doc8", {})
143145

144146
cfg = {}
145147
for key, value in parsed.items():
@@ -161,7 +163,7 @@ def extract_config(args):
161163
continue
162164
if cfg_file.endswith((".ini", ".cfg")):
163165
cfg = from_ini(cfg_file)
164-
elif cfg_file.endswith(".toml") and HAVE_TOML:
166+
elif cfg_file.endswith(".toml"):
165167
cfg = from_toml(cfg_file)
166168
if cfg:
167169
break

0 commit comments

Comments
 (0)