From dd6cfc2b28b4b91ea283190d17c6d6769ce5cbb3 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. --- responses/__init__.py | 6 ++++-- responses/_recorder.py | 8 ++++---- responses/tests/test_recorder.py | 10 ++++++---- setup.py | 4 ++-- 4 files changed, 16 insertions(+), 12 deletions(-) diff --git a/responses/__init__.py b/responses/__init__.py index 2a2c083b..97719021 100644 --- a/responses/__init__.py +++ b/responses/__init__.py @@ -1,6 +1,7 @@ import inspect import json as json_module import logging +import sys from collections import namedtuple from functools import partialmethod from functools import wraps @@ -25,7 +26,8 @@ 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 +772,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..0f9b764b 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,7 @@ from responses.registries import OrderedRegistry -def _dump(registered: "List[BaseResponse]", destination: "io.IOBase") -> None: +def _dump(registered: "List[BaseResponse]", destination: "BinaryIO") -> None: data: Dict[str, Any] = {"responses": []} for rsp in registered: try: @@ -71,7 +71,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..db588850 100644 --- a/responses/tests/test_recorder.py +++ b/responses/tests/test_recorder.py @@ -1,7 +1,9 @@ +import sys from pathlib import Path import requests -import toml +import tomli_w +import tomli as _toml import responses from responses import _recorder @@ -95,7 +97,7 @@ def run(): run() with open(self.out_file) as file: - data = toml.load(file) + data = _toml.load(file) assert data == get_data(httpserver.host, httpserver.port) @@ -109,8 +111,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'", ]