Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make toml support implicit #93

Merged
merged 2 commits into from Jul 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.rst
Expand Up @@ -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``

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

Expand Down
1 change: 1 addition & 0 deletions setup.cfg
Expand Up @@ -63,6 +63,7 @@ install_requires =
docutils
restructuredtext-lint>=0.7
stevedore
tomli; python_version < '3.11'
Pygments

[options.entry_points]
Expand Down
24 changes: 15 additions & 9 deletions src/doc8/main.py
Expand Up @@ -35,12 +35,13 @@
import os
import sys

try:
import tomli

HAVE_TOML = True
try:
# py3.11+
from tomllib import load as toml_load # type: ignore
except ImportError:
HAVE_TOML = False
# py3.10 or older
from tomli import load as toml_load

from stevedore import extension

Expand All @@ -51,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=","):
Expand Down Expand Up @@ -135,7 +141,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_load(f).get("tool", {}).get("doc8", {})

cfg = {}
for key, value in parsed.items():
Expand All @@ -157,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
Expand Down