dmayo3 / mocksafe Goto Github PK
View Code? Open in Web Editor NEWType-safe mocking for Python.
Home Page: https://pypi.org/project/mocksafe
License: MIT License
Type-safe mocking for Python.
Home Page: https://pypi.org/project/mocksafe
License: MIT License
Currently there's first class support for mocking classes, but mocking modules in a type-safe way is cumbersome and requires defining a class that mirrors the module structure.
For example, here is a subset of the requests
library being defined as a class type. It would never used in production, only to facilitate type-safe mocking. It calls through to the original functions to validate the call signatures line up, but this code is never exercised.
It can then be mocked using mock(RequestsInterface)
.
class RequestsInterface:
def get(self, url: str, params: dict | None=None, **kwargs) -> Response:
return requests.get(url, params, **kwargs)
def post(self, url: str, data=None, json=None, **kwargs) -> Response:
return requests.post(url, data, json, **kwargs)
def delete(self, url: str, **kwargs) -> Response:
return requests.delete(url, **kwargs)
Support 3.9 if not earlier.
This means dropping some of the more recent improvements such as the new union type syntax foo | bar
.
Property stubbing leaks across multiple mock instances.
Currently there's only basic support for getting properties.
Add better support for attributes that are not methods or properties.
Use custom exceptions, enhance the error messages, see if we can filter the trace back or something. Document expected error cases.
See if we can make it more concise or more robust.
Make it easier to migrate from unittest.mock
to this library by shimming its API.
Add optional support for mocks that are themselves callable with __call__()
As a basic starting point.
Sometimes it's useful to be able to monkeypatch
attributes on a module to swap a real implementation for a mocked version.
It would be useful to provide a fixture that can be easily used to patch and mock an attribute at the same time.
I attempted it, but I couldn't quite figure out how to make it work.
Here is the fixture I came up with while experimenting, which would then need to be exposed by mocksafe
as a pytest plugin.
from mocksafe import mock
from typing import TypeVar, Callable
T = TypeVar('T')
class _Patcher:
def __init__(self, monkeypatch):
self._monkeypatch = monkeypatch
def patch(self, obj, attr: str, class_type: type[T]) -> T:
mock_obj: T = mock(class_type)
self._monkeypatch.setattr(obj, attr, mock_obj)
return mock_obj
Patcher = Callable[[object, str, type[T]], T]
@pytest.fixture
def patch(monkeypatch) -> Patcher:
return _Patcher(monkeypatch).patch
The Patcher
is a type that can be imported and used on the patch
fixture, something like so:
@pytest.fixture
def mock_foo(patch: Patcher) -> ClassToMock:
return patch(real_object, "attribute_to_mock", ClassToMock)
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.