Skip to content

Commit a3638de

Browse files
JakobDevssbarnea
andauthored
Make toml support implicit (#93)
* Add support for tomllib * Make toml support implicit Co-authored-by: Sorin Sbarnea <[email protected]>
1 parent 6dcee38 commit a3638de

File tree

3 files changed

+17
-10
lines changed

3 files changed

+17
-10
lines changed

README.rst

+1-1
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
105+
* ``$CWD/pyproject.toml``
106106

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

setup.cfg

+1
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

+15-9
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,13 @@
3535
import os
3636
import sys
3737

38-
try:
39-
import tomli
4038

41-
HAVE_TOML = True
39+
try:
40+
# py3.11+
41+
from tomllib import load as toml_load # type: ignore
4242
except ImportError:
43-
HAVE_TOML = False
43+
# py3.10 or older
44+
from tomli import load as toml_load
4445

4546
from stevedore import extension
4647

@@ -51,9 +52,14 @@
5152

5253
FILE_PATTERNS = [".rst", ".txt"]
5354
MAX_LINE_LENGTH = 79
54-
CONFIG_FILENAMES = ["doc8.ini", ".config/doc8.ini", "tox.ini", "pep8.ini", "setup.cfg"]
55-
if HAVE_TOML:
56-
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+
]
5763

5864

5965
def split_set_type(text, delimiter=","):
@@ -135,7 +141,7 @@ def from_ini(fp):
135141

136142
def from_toml(fp):
137143
with open(fp, "rb") as f:
138-
parsed = tomli.load(f).get("tool", {}).get("doc8", {})
144+
parsed = toml_load(f).get("tool", {}).get("doc8", {})
139145

140146
cfg = {}
141147
for key, value in parsed.items():
@@ -157,7 +163,7 @@ def extract_config(args):
157163
continue
158164
if cfg_file.endswith((".ini", ".cfg")):
159165
cfg = from_ini(cfg_file)
160-
elif cfg_file.endswith(".toml") and HAVE_TOML:
166+
elif cfg_file.endswith(".toml"):
161167
cfg = from_toml(cfg_file)
162168
if cfg:
163169
break

0 commit comments

Comments
 (0)