Skip to content

Commit

Permalink
Type hint FakeAdapter and assertion function using a type protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
DevL committed Mar 1, 2024
1 parent 967eee7 commit d22ef3f
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/requtests/fake_adapter.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,24 @@
from itertools import cycle
from typing import Any, List, Union
from typing_extensions import Protocol
from requests.adapters import BaseAdapter
from requests.models import Response, PreparedRequest


class AssertionFunction(Protocol):
def __call__(self, prepared_request: PreparedRequest, **kwargs) -> Any:
"""
An assertion function is expected to raise an error if any of its assertions fail.
"""
pass


class FakeAdapter(BaseAdapter):
def __init__(self, *responses, assertions=None):
def __init__(
self,
*responses: Response,
assertions: Union[None, AssertionFunction, List[AssertionFunction]] = None
):
super().__init__()
self.closed = 0
self.responses = _to_generator(responses)
Expand Down

0 comments on commit d22ef3f

Please sign in to comment.