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 (#608)
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

Co-authored-by: Sambhav Kothari <sambhavs.email@gmail.com>
  • Loading branch information
mgorny and samj1912 committed Jan 3, 2023
1 parent 5c55802 commit 3bc3b87
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 20 deletions.
7 changes: 7 additions & 0 deletions docs/release_notes.rst
Expand Up @@ -5,6 +5,13 @@ Release Notes
`Semantic Versioning <http://semver.org/>`_ specification.


Current Development Version
---------------------------

Bug Fixes

* Use tomllib/tomli to correctly read .toml files (#599, #600).

6.2.0 - January 2nd, 2023
---------------------------

Expand Down
16 changes: 8 additions & 8 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions pyproject.toml
Expand Up @@ -21,11 +21,11 @@ classifiers = [
[tool.poetry.dependencies]
python = ">=3.6"
snowballstemmer = ">=2.2.0"
toml = {version = ">=0.10.2", optional = true}
tomli = {version = ">=1.2.3", optional = true, python = "<3.11"}
importlib-metadata = {version = ">=2.0.0,<5.0.0", python = "<3.8"}

[tool.poetry.extras]
toml = ["toml"]
toml = ["tomli"]

[tool.poetry.scripts]
pydocstyle = "pydocstyle.cli:main"
Expand Down
2 changes: 1 addition & 1 deletion requirements/runtime.txt
@@ -1,3 +1,3 @@
snowballstemmer>=1.2.1
toml>=0.10.2
tomli>=1.2.3; python_version < "3.11"
importlib-metadata<5.0.0,>=2.0.0; python_version < "3.8"
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
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 @@ -14,10 +15,13 @@
from .utils import 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 @@ -60,15 +64,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 3bc3b87

Please sign in to comment.