From dd205bb529a0cfc08bf0ec1e533554843cd92db7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20G=C3=B3rny?= Date: Wed, 12 Oct 2022 13:03:26 +0200 Subject: [PATCH] Use tomli/tomli-w instead of unmaintained toml Replace the `toml` dependency that is unmaintained (last release in 2020) and does not implement TOML 1.0 with more modern `tomli` and `tomli-w` packages. Co-authored-by: Maksim Beliaev --- responses/__init__.py | 4 ++-- responses/_recorder.py | 18 +++++++++++++----- responses/tests/test_recorder.py | 11 ++++++----- setup.py | 4 ++-- 4 files changed, 23 insertions(+), 14 deletions(-) diff --git a/responses/__init__.py b/responses/__init__.py index 2a2c083b..094c0e98 100644 --- a/responses/__init__.py +++ b/responses/__init__.py @@ -25,7 +25,7 @@ from typing import overload from warnings import warn -import toml as _toml +import tomli as _toml from requests.adapters import HTTPAdapter from requests.adapters import MaxRetryError from requests.exceptions import ConnectionError @@ -770,7 +770,7 @@ def add( put = partialmethod(add, PUT) def _add_from_file(self, file_path: "Union[str, bytes, os.PathLike[Any]]") -> None: - with open(file_path) as file: + with open(file_path, "rb") as file: data = _toml.load(file) for rsp in data["responses"]: diff --git a/responses/_recorder.py b/responses/_recorder.py index 21f8317d..b871f038 100644 --- a/responses/_recorder.py +++ b/responses/_recorder.py @@ -2,10 +2,10 @@ from typing import TYPE_CHECKING if TYPE_CHECKING: # pragma: no cover - import io import os from typing import Any + from typing import BinaryIO from typing import Callable from typing import Dict from typing import List @@ -18,7 +18,7 @@ from responses import _F from responses import BaseResponse -import toml as _toml +import tomli_w as _toml from responses import RequestsMock from responses import Response @@ -26,7 +26,15 @@ from responses.registries import OrderedRegistry -def _dump(registered: "List[BaseResponse]", destination: "io.IOBase") -> None: +def _remove_nones(d: "Any") -> "Any": + if isinstance(d, dict): + return {k: _remove_nones(v) for k, v in d.items() if v is not None} + if isinstance(d, list): + return [_remove_nones(i) for i in d] + return d + + +def _dump(registered: "List[BaseResponse]", destination: "BinaryIO") -> None: data: Dict[str, Any] = {"responses": []} for rsp in registered: try: @@ -49,7 +57,7 @@ def _dump(registered: "List[BaseResponse]", destination: "io.IOBase") -> None: "Cannot dump response object." "Probably you use custom Response object that is missing required attributes" ) from exc - _toml.dump(data, destination) + _toml.dump(_remove_nones(data), destination) class Recorder(RequestsMock): @@ -71,7 +79,7 @@ def deco_record(function: "_F") -> "Callable[..., Any]": def wrapper(*args: "Any", **kwargs: "Any") -> "Any": # type: ignore[misc] with self: ret = function(*args, **kwargs) - with open(file_path, "w") as file: + with open(file_path, "wb") as file: _dump(self.get_registry().registered, file) return ret diff --git a/responses/tests/test_recorder.py b/responses/tests/test_recorder.py index d70d60f6..d7c40087 100644 --- a/responses/tests/test_recorder.py +++ b/responses/tests/test_recorder.py @@ -1,7 +1,8 @@ from pathlib import Path import requests -import toml +import tomli as _toml +import tomli_w import responses from responses import _recorder @@ -94,8 +95,8 @@ def run(): run() - with open(self.out_file) as file: - data = toml.load(file) + with open(self.out_file, "rb") as file: + data = _toml.load(file) assert data == get_data(httpserver.host, httpserver.port) @@ -109,8 +110,8 @@ def teardown(self): assert not out_file.exists() def test_add_from_file(self): - with open("out.toml", "w") as file: - toml.dump(get_data("example.com", "8080"), file) + with open("out.toml", "wb") as file: + tomli_w.dump(get_data("example.com", "8080"), file) @responses.activate def run(): diff --git a/setup.py b/setup.py index 450b65af..f4447b50 100644 --- a/setup.py +++ b/setup.py @@ -19,8 +19,8 @@ install_requires = [ "requests>=2.22.0,<3.0", "urllib3>=1.25.10", - "toml", - "types-toml", + "tomli", + "tomli-w", "typing_extensions; python_version < '3.8'", ]