From 3914fb93c991e98adce7a5e788d5ab17006f303e Mon Sep 17 00:00:00 2001 From: Andreas Schimmelschulze Date: Tue, 30 Apr 2024 14:08:02 +0200 Subject: [PATCH] refactor: add type hints for request matcher for better usability --- http_request_recorder/http_request_recorder.py | 7 ++++--- setup.py | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/http_request_recorder/http_request_recorder.py b/http_request_recorder/http_request_recorder.py index 6f7fd25..1976542 100644 --- a/http_request_recorder/http_request_recorder.py +++ b/http_request_recorder/http_request_recorder.py @@ -5,6 +5,7 @@ from itertools import tee from logging import getLogger from typing import Iterable, Union +from collections.abc import Callable from aiohttp import web from aiohttp.web_request import BaseRequest @@ -38,7 +39,7 @@ def __init__(self, response): self.was_triggered = Event() self.response = response - def __init__(self, matcher, responses: ResponsesType, name: str, timeout: int): + def __init__(self, matcher: Callable[[RecordedRequest], bool], responses: ResponsesType, name: str, timeout: int): self.name: str = name self._timeout: int = timeout @@ -55,7 +56,7 @@ def __init__(self, matcher, responses: ResponsesType, name: str, timeout: int): self._recorded = [] self._next_for_response, self._next_to_return = tee(self.responses) - self._matcher = matcher + self._matcher: Callable[[RecordedRequest], bool] = matcher def __repr__(self): return f"<{self.__class__.__name__} '{self.name}'>" @@ -156,7 +157,7 @@ async def handle_request(self, request: BaseRequest): return web.Response(status=200, body=response) - def expect(self, matcher, responses: ResponsesType = "", name: str = None, timeout: int = 3) -> ExpectedInteraction: + def expect(self, matcher: Callable[[RecordedRequest], bool], responses: ResponsesType = "", name: str = None, timeout: int = 3) -> ExpectedInteraction: expectation = ExpectedInteraction(matcher, responses, name, timeout) self._expectations.append(expectation) return expectation diff --git a/setup.py b/setup.py index ea7cd63..eb983ae 100644 --- a/setup.py +++ b/setup.py @@ -11,7 +11,7 @@ setup( name='http_request_recorder', - version='0.2.1', + version='0.2.2', description='A package to record an respond to http requests, primarily for use in black box testing.', long_description=readme, author='',