diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index f7e1f831..899de43a 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -25,7 +25,12 @@ repos: hooks: - id: flake8 args: [ '--max-line-length', '100', '--max-doc-length', '120' ] - - repo: https://github.com/asottile/blacken-docs + - repo: https://github.com/asottile/pyupgrade + rev: v3.10.1 + hooks: + - id: pyupgrade + args: [--py37-plus] + - repo: https://github.com/adamchainz/blacken-docs rev: 1.14.0 hooks: - id: blacken-docs diff --git a/responses/__init__.py b/responses/__init__.py index 2666b7c8..a89e2d7e 100644 --- a/responses/__init__.py +++ b/responses/__init__.py @@ -111,8 +111,6 @@ class FalseBool: def __bool__(self) -> bool: return False - __nonzero__ = __bool__ - def urlencoded_params_matcher(params: Optional[Dict[str, str]]) -> Callable[..., Any]: warn( @@ -369,7 +367,7 @@ def is_closed() -> bool: return data -class BaseResponse(object): +class BaseResponse: passthrough: bool = False content_type: Optional[str] = None headers: Optional[Mapping[str, str]] = None @@ -662,7 +660,7 @@ def __init__(self, *args: Any, **kwargs: Any): super().__init__(*args, passthrough=True, **kwargs) -class RequestsMock(object): +class RequestsMock: DELETE: Literal["DELETE"] = "DELETE" GET: Literal["GET"] = "GET" HEAD: Literal["HEAD"] = "HEAD" @@ -792,7 +790,7 @@ def add( def _parse_response_file( self, file_path: "Union[str, bytes, os.PathLike[Any]]" ) -> "Dict[str, Any]": - with open(file_path, "r") as file: + with open(file_path) as file: data = yaml.safe_load(file) return data @@ -1047,7 +1045,7 @@ def _on_request( if self.passthru_prefixes: error_msg += "Passthru prefixes:\n" for p in self.passthru_prefixes: - error_msg += "- {}\n".format(p) + error_msg += f"- {p}\n" response = ConnectionError(error_msg) response.request = request @@ -1156,7 +1154,7 @@ def stop(self, allow_assert: bool = True) -> None: not_called = [m for m in self.registered() if m.call_count == 0] if not_called: raise AssertionError( - "Not all requests have been executed {0!r}".format( + "Not all requests have been executed {!r}".format( [(match.method, match.url) for match in not_called] ) ) diff --git a/responses/matchers.py b/responses/matchers.py index e9fb3726..066f5234 100644 --- a/responses/matchers.py +++ b/responses/matchers.py @@ -48,7 +48,7 @@ def list_to_str(input_list: List[str]) -> str: elif isinstance(val, list): val = list_to_str(input_list=val) - items_list.append("{}: {}".format(key, val)) + items_list.append(f"{key}: {val}") key_val_str = "{{{}}}".format(", ".join(items_list)) return key_val_str diff --git a/responses/registries.py b/responses/registries.py index 4b7e2337..fcf481a8 100644 --- a/responses/registries.py +++ b/responses/registries.py @@ -11,7 +11,7 @@ from responses import BaseResponse -class FirstMatchRegistry(object): +class FirstMatchRegistry: def __init__(self) -> None: self._responses: List["BaseResponse"] = [] @@ -67,9 +67,7 @@ def replace(self, response: "BaseResponse") -> "BaseResponse": try: index = self.registered.index(response) except ValueError: - raise ValueError( - "Response is not registered for URL {}".format(response.url) - ) + raise ValueError(f"Response is not registered for URL {response.url}") self.registered[index] = response return response diff --git a/responses/tests/test_recorder.py b/responses/tests/test_recorder.py index bcd05cef..d892845c 100644 --- a/responses/tests/test_recorder.py +++ b/responses/tests/test_recorder.py @@ -87,7 +87,7 @@ def run(): run() - with open(self.out_file, "r") as file: + with open(self.out_file) as file: data = yaml.safe_load(file) assert data == get_data(httpserver.host, httpserver.port) diff --git a/responses/tests/test_responses.py b/responses/tests/test_responses.py index cbcee548..2ae3d655 100644 --- a/responses/tests/test_responses.py +++ b/responses/tests/test_responses.py @@ -1,5 +1,3 @@ -# coding: utf-8 - import inspect import os import re @@ -912,7 +910,7 @@ def fruit_basket(my_fruit): @pytest.mark.usefixtures("my_fruit", "fruit_basket") -class TestFixtures(object): +class TestFixtures: """ Test that pytest fixtures work well with 'activate' decorator """ @@ -960,7 +958,7 @@ def test_function(a, b=None): def test_activate_doesnt_change_signature_for_method(): - class TestCase(object): + class TestCase: def test_function(self, a, b=None): return self, a, b @@ -1100,10 +1098,10 @@ def test_response_filebody(): def run(): current_file = os.path.abspath(__file__) with responses.RequestsMock() as m: - with open(current_file, "r", encoding="utf-8") as out: + with open(current_file, encoding="utf-8") as out: m.add(responses.GET, "http://example.com", body=out.read(), stream=True) resp = requests.get("http://example.com", stream=True) - with open(current_file, "r", encoding="utf-8") as out: + with open(current_file, encoding="utf-8") as out: assert resp.text == out.read() run() @@ -1195,7 +1193,7 @@ def test_some_second_function(): def test_allow_redirects_samehost(): redirecting_url = "http://example.com" final_url_path = "/1" - final_url = "{0}{1}".format(redirecting_url, final_url_path) + final_url = f"{redirecting_url}{final_url_path}" url_re = re.compile(r"^http://example.com(/)?(\d+)?$") def request_callback(request): @@ -1209,7 +1207,7 @@ def request_callback(request): n = 1 else: n = 0 - redirect_headers = {"location": "/{0!s}".format(n)} + redirect_headers = {"location": f"/{n!s}"} return 301, redirect_headers, None def run(): @@ -1696,12 +1694,12 @@ def test_passthrough_response(self, httpserver): @responses.activate def run(): responses.add(PassthroughResponse(responses.GET, url)) - responses.add(responses.GET, "{}/one".format(url), body="one") + responses.add(responses.GET, f"{url}/one", body="one") responses.add(responses.GET, "http://example.com/two", body="two") resp = requests.get("http://example.com/two") assert_response(resp, "two") - resp = requests.get("{}/one".format(url)) + resp = requests.get(f"{url}/one") assert_response(resp, "one") resp = requests.get(url) assert_response(resp, "OK") @@ -1762,12 +1760,12 @@ def test_passthru(self, httpserver): @responses.activate def run(): responses.add_passthru(url) - responses.add(responses.GET, "{}/one".format(url), body="one") + responses.add(responses.GET, f"{url}/one", body="one") responses.add(responses.GET, "http://example.com/two", body="two") resp = requests.get("http://example.com/two") assert_response(resp, "two") - resp = requests.get("{}/one".format(url)) + resp = requests.get(f"{url}/one") assert_response(resp, "one") resp = requests.get(url) assert_response(resp, "OK") @@ -1784,7 +1782,7 @@ def test_passthru_regex(self, httpserver): @responses.activate def run(): responses.add_passthru(re.compile(f"{url}/\\w+")) - responses.add(responses.GET, "{}/one".format(url), body="one") + responses.add(responses.GET, f"{url}/one", body="one") responses.add(responses.GET, "http://example.com/two", body="two") resp = requests.get("http://example.com/two") @@ -1876,7 +1874,7 @@ def run(): params = {"hello": "world", "example": "params"} responses.add( method=responses.GET, - url="{0}?hello=world".format(url), + url=f"{url}?hello=world", body="test", match_querystring=False, ) @@ -1923,7 +1921,7 @@ def run(): with pytest.raises(AssertionError) as excinfo: responses.assert_call_count(url, 2) - assert "Expected URL '{0}' to be called 2 times. Called 0 times.".format( + assert "Expected URL '{}' to be called 2 times. Called 0 times.".format( url ) in str(excinfo.value) @@ -1936,7 +1934,7 @@ def run(): requests.get(url) with pytest.raises(AssertionError) as excinfo: responses.assert_call_count(url, 3) - assert "Expected URL '{0}' to be called 3 times. Called 2 times.".format( + assert "Expected URL '{}' to be called 3 times. Called 2 times.".format( url ) in str(excinfo.value)