From b45a393b2f0c4ce0f17c3e58cf5d768bd653e155 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20G=C3=B3rny?= Date: Tue, 3 Jan 2023 06:49:32 +0100 Subject: [PATCH] Use tomllib/tomli for reading .toml configs 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 --- 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/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/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 copy 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 ._version import __version__ 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 @@ class TomlParser: 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): -- 2.39.0