monkeypatching

Overriding method python class of a pip module to update its behavior globally

Overriding method python class of a pip module to update its behavior globally Question: Using OOP I want to do something like from django.contrib import admin class NavigateFormAdmin(admin.ModelAdmin): def render_change_form(self, request, context, add=False, change=False, form_url=”, obj=None): context[‘next_record_id’] = custom_function_to_calculate(context[‘obj’].id) res = super().render_change_form(request, context, add, change, form_url) return res And expect that whenever render_change_form of admin.ModelAdmin …

Total answers: 1

Why use a superclass's __init__ to change it into a subclass?

Why use a superclass's __init__ to change it into a subclass? Question: I’m working on replicating the SHAP package algorithm – an explainability algorithm for machine learning. I’ve been reading through the author’s code, and I’ve come across a pattern I’ve never seen before. The author has created a superclass called Explainer, which is a …

Total answers: 1

Monkeypatching/mocking the HTTPX external requests

Monkeypatching/mocking the HTTPX external requests Question: I’m trying to monkeypatch the external request. Here is the code of a web endpoint: import httpx, json … @app.get(‘/test’) async def view_test(request): async with httpx.AsyncClient() as client: # sending external request api_response = await client.get( f’https://jsonplaceholder.typicode.com/todos/1′, timeout=10, ) resp = api_response.json() # modifying the result resp[‘foo’] = 0 …

Total answers: 1

pytest monkepatch, decorate stub instead of replace

pytest monkepatch, decorate stub instead of replace Question: So I have the following example code: import pytest class Helper: def function1(self): print("helper function called") class SUT: def real_function(self): helper = Helper() helper.function1() print("real function called") def test_Test(monkeypatch): test = SUT() def stub_function1(self): print("helper is stubbed") monkeypatch.setattr(Helper, "function1", stub_function1) test.real_function() and running pytest .test_scratch_1.py –capture=tee-sys prints …

Total answers: 2

How do I remove a handler from a loguru.logger when using pytest?

How do I remove a handler from a loguru.logger when using pytest? Question: I wrote a Thing class that does logging using loguru. At the bottom of the class file I add a handler to the logger. This is what thing.py looks like. from loguru import logger class Thing: def __init__(self): logger.info("Thing created") def __enter__(self): …

Total answers: 1

Python Test Monkeypatch with Arguments

Python Test Monkeypatch with Arguments Question: I try to create a mock test with monkeypatch. I have a typical service-repository class. repository_class.py find_by_id(id): con.select(….); service_class.py get_details(id): some pre-process… item = repository_class.find_by_id(id) post-process… return result then I try to create a mock test with mocking repository method under service: def test_bid_on_brand_keyword(monkeypatch): mock_data = "abc" monkeypatch.setattr(repository_class, ‘find_by_id’, …

Total answers: 2

how to monkey patch function_1 if function_1 is called by function_2

how to monkey patch function_1 if function_1 is called by function_2 Question: The code structure is basically like below. external_lib.py is an external library that I cannot modify, while my_module.py is the python file I own. I’ve written pseudo code in my_module.py for what I’m trying to accomplish. external_lib.py def function_1(arg1, arg2): # expensive calculations …

Total answers: 1

fastai.fastcore patch decorator vs simpe monkey-patching

fastai.fastcore patch decorator vs simpe monkey-patching Question: I’m trying to understand the value-added of using fastai‘s fastcore.basics.patch_to decorator. Here’s the fastcore way: from fastcore.basics import patch_to class _T3(int): pass @patch_to(_T3) def func1(self, a): return self + a And here’s the simple monkey-patching approach: class simple_T3(int): pass def func1(self, a): return self + a simple_T3.func1 = …

Total answers: 1

Changing monkeypatch setattr multiple times

Changing monkeypatch setattr multiple times Question: I am trying to test code that depends on a third party and would like to use monkeypatch to replicate what I expect a request will return. Here is a minimal example of the code that I have. import requests def get_urls(*urls): results = [] for url in urls: …

Total answers: 2