Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use tomllib/tomli/tomli-w instead of unmaintained toml [WIP] #596

Merged
merged 1 commit into from
Oct 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions responses/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"]:
Expand Down
18 changes: 13 additions & 5 deletions responses/_recorder.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -18,15 +18,23 @@
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
from responses import _real_send
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:
Expand All @@ -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):
Expand All @@ -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
Expand Down
11 changes: 6 additions & 5 deletions responses/tests/test_recorder.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)

Expand All @@ -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():
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'",
]

Expand Down