Skip to content
This repository has been archived by the owner on Nov 3, 2023. It is now read-only.

Commit

Permalink
Use tomllib/tomli for reading .toml configs
Browse files Browse the repository at this point in the history
Use the built-in `tomllib` module in Python 3.11 and the modern `tomli`
package in older Python versions to read .toml configs instead of
the unmaintained and broken `toml` package.

Fixes #599
Fixes #600
  • Loading branch information
mgorny committed Oct 12, 2022
1 parent a6fe422 commit 4fb964b
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 11 deletions.
2 changes: 1 addition & 1 deletion requirements/runtime.txt
@@ -1,2 +1,2 @@
snowballstemmer==1.2.1
toml==0.10.2
tomli==2.0.1; python_version < "3.11"
1 change: 0 additions & 1 deletion requirements/tests.txt
Expand Up @@ -2,5 +2,4 @@ pytest==6.2.5
mypy==0.930
black==22.3
isort==5.4.2
types-toml
types-setuptools
2 changes: 1 addition & 1 deletion setup.py
Expand Up @@ -8,7 +8,7 @@
'snowballstemmer',
]
extra_requirements = {
'toml': ['toml'],
'toml': ['tomli; python_version < "3.11"'],
}


Expand Down
20 changes: 12 additions & 8 deletions src/pydocstyle/config.py
Expand Up @@ -4,6 +4,7 @@
import itertools
import operator
import os
import sys
from collections import namedtuple
from collections.abc import Set
from configparser import NoOptionError, NoSectionError, RawConfigParser
Expand All @@ -13,10 +14,13 @@
from .utils import __version__, log
from .violations import ErrorRegistry, conventions

try:
import toml
except ImportError: # pragma: no cover
toml = None # type: ignore
if sys.version_info >= (3, 11):
import tomllib
else:
try:
import tomli as tomllib
except ImportError: # pragma: no cover
tomllib = None # type: ignore


def check_initialized(method):
Expand Down Expand Up @@ -59,15 +63,15 @@ def read(self, filenames, encoding=None):
read_ok = []
for filename in filenames:
try:
with open(filename, encoding=encoding) as fp:
if not toml:
with open(filename, "rb") as fp:
if not tomllib:
log.warning(
"The %s configuration file was ignored, "
"because the `toml` package is not installed.",
"because the `tomli` package is not installed.",
filename,
)
continue
self._config.update(toml.load(fp))
self._config.update(tomllib.load(fp))
except OSError:
continue
if isinstance(filename, os.PathLike):
Expand Down

0 comments on commit 4fb964b

Please sign in to comment.