From 3bc3b876ce064feac58147d36c7ecc89db67fb1d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20G=C3=B3rny?= Date: Tue, 3 Jan 2023 10:08:48 +0100 Subject: [PATCH] Use tomllib/tomli for reading .toml configs (#608) 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 --- docs/release_notes.rst | 7 +++++++ poetry.lock | 16 ++++++++-------- pyproject.toml | 4 ++-- requirements/runtime.txt | 2 +- requirements/tests.txt | 1 - src/pydocstyle/config.py | 20 ++++++++++++-------- 6 files changed, 30 insertions(+), 20 deletions(-) diff --git a/docs/release_notes.rst b/docs/release_notes.rst index d7ca27f..852ce1d 100644 --- a/docs/release_notes.rst +++ b/docs/release_notes.rst @@ -5,6 +5,13 @@ Release Notes `Semantic Versioning `_ specification. +Current Development Version +--------------------------- + +Bug Fixes + +* Use tomllib/tomli to correctly read .toml files (#599, #600). + 6.2.0 - January 2nd, 2023 --------------------------- diff --git a/poetry.lock b/poetry.lock index 458a3a0..566a2ac 100644 --- a/poetry.lock +++ b/poetry.lock @@ -34,15 +34,15 @@ files = [ ] [[package]] -name = "toml" -version = "0.10.2" -description = "Python Library for Tom's Obvious, Minimal Language" +name = "tomli" +version = "1.2.3" +description = "A lil' TOML parser" category = "main" optional = true -python-versions = ">=2.6, !=3.0.*, !=3.1.*, !=3.2.*" +python-versions = ">=3.6" files = [ - {file = "toml-0.10.2-py2.py3-none-any.whl", hash = "sha256:806143ae5bfb6a3c6e736a764057db0e6a0e05e338b5630894a5f779cabb4f9b"}, - {file = "toml-0.10.2.tar.gz", hash = "sha256:b3bda1d108d5dd99f4a20d24d9c348e91c4db7ab1b749200bded2f839ccbe68f"}, + {file = "tomli-1.2.3-py3-none-any.whl", hash = "sha256:e3069e4be3ead9668e21cb9b074cd948f7b3113fd9c8bba083f48247aab8b11c"}, + {file = "tomli-1.2.3.tar.gz", hash = "sha256:05b6166bff487dc068d322585c7ea4ef78deed501cc124060e0f238e89a9231f"}, ] [[package]] @@ -74,9 +74,9 @@ docs = ["jaraco.packaging (>=8.2)", "rst.linker (>=1.9)", "sphinx"] testing = ["func-timeout", "jaraco.itertools", "pytest (>=4.6)", "pytest-black (>=0.3.7)", "pytest-checkdocs (>=2.4)", "pytest-cov", "pytest-enabler (>=1.0.1)", "pytest-flake8", "pytest-mypy"] [extras] -toml = ["toml"] +toml = ["tomli"] [metadata] lock-version = "2.0" python-versions = ">=3.6" -content-hash = "cd980bd00cd4a5ba9efb4e38007da4c71deab59aac093b9eb1042d2d43505dc6" +content-hash = "1abb1b7c1fa0c27846501ad1b5d7916eb5ec6e7961eab46ced6887d16428977a" diff --git a/pyproject.toml b/pyproject.toml index 607aa3f..84bfe0d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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" diff --git a/requirements/runtime.txt b/requirements/runtime.txt index ad37b51..8b00654 100644 --- a/requirements/runtime.txt +++ b/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" diff --git a/requirements/tests.txt b/requirements/tests.txt index ed12797..f236d68 100644 --- a/requirements/tests.txt +++ b/requirements/tests.txt @@ -2,5 +2,4 @@ pytest==6.2.5 mypy==0.930 black==22.3 isort==5.4.2 -types-toml types-setuptools diff --git a/src/pydocstyle/config.py b/src/pydocstyle/config.py index 4819cde..c05f7dc 100644 --- a/src/pydocstyle/config.py +++ b/src/pydocstyle/config.py @@ -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 @@ -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): @@ -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):