monkeypatching

Bypassing a decorator for unit testing [duplicate]

女生的网名这么多〃 提交于 2021-02-20 03:51:51
问题 This question already has answers here : How to skip or ignore python decorators (3 answers) Closed 4 years ago . I have a decorator @auth which basically checks a database to ensure a user can access a given REST call. I would like to write some unit tests for these calls. My initial idea was to simply monkeypatch the decorator into a pass through that does nothing. (My initial idea failed, so I may just monkeypatch some function inside of @auth so that it always passes, but I'm still

Why doesn't this monkey patch work for python turtle?

穿精又带淫゛_ 提交于 2021-02-11 15:31:16
问题 So I'm trying to monkey patch the onkey function in turtle so it calls a function with the button hit instead of just calling it with no arguments. I'm using the string "tester" to see if it works, but it looks like the original functions never got changes. Can someone explain what I'm doing wrong? from threading import Thread from time import sleep from turtle import * def NEWonkeypress(self, fun, key=None): if fun is None: if key in self._keys: self._keys.remove(key) elif key is not None

Really weird… can't set attributes of built-in/extension type 'lxml.etree._Element'

匆匆过客 提交于 2021-02-10 14:18:40
问题 I've changed attributes for other classes before without issues. _Element is obviously not a built-in. from lxml.etree import _Element _Element.new_attr = 54 results in: TypeError: can't set attributes of built-in/extension type 'lxml.etree._Element' 回答1: _Element is implemented in Cython. As Steve Holden explains (my emphasis), The problem is that extension types' attributes are determined by the layout of the object's slots and forever fixed in the C code that implements them: the slots can

How to monkey patch a `__call__` method?

只愿长相守 提交于 2021-02-07 02:42:17
问题 I don't seem to be able to monkey patch a __call__ method of class instance (and yes, I want to patch just single instances, not all of them). The following code: class A(object): def test(self): return "TEST" def __call__(self): return "EXAMPLE" a = A() print("call method: {0}".format(a.__call__)) print("test method: {0}".format(a.test)) a.__call__ = lambda : "example" a.test = lambda : "test" print("call method: {0}".format(a.__call__)) print("test method: {0}".format(a.test)) print(a())

How does extending classes (Monkey Patching) work in Python?

让人想犯罪 __ 提交于 2021-02-06 15:26:57
问题 class Foo(object): pass foo = Foo() def bar(self): print 'bar' Foo.bar = bar foo.bar() #bar Coming from JavaScript, if a "class" prototype was augmented with a certain attribute. It is known that all instances of that "class" would have that attribute in its prototype chain, hence no modifications has to be done on any of its instances or "sub-classes". In that sense, how can a Class-based language like Python achieve Monkey patching? 回答1: The real question is, how can it not? In Python,

How does extending classes (Monkey Patching) work in Python?

喜夏-厌秋 提交于 2021-02-06 15:26:14
问题 class Foo(object): pass foo = Foo() def bar(self): print 'bar' Foo.bar = bar foo.bar() #bar Coming from JavaScript, if a "class" prototype was augmented with a certain attribute. It is known that all instances of that "class" would have that attribute in its prototype chain, hence no modifications has to be done on any of its instances or "sub-classes". In that sense, how can a Class-based language like Python achieve Monkey patching? 回答1: The real question is, how can it not? In Python,

How does extending classes (Monkey Patching) work in Python?

夙愿已清 提交于 2021-02-06 15:25:36
问题 class Foo(object): pass foo = Foo() def bar(self): print 'bar' Foo.bar = bar foo.bar() #bar Coming from JavaScript, if a "class" prototype was augmented with a certain attribute. It is known that all instances of that "class" would have that attribute in its prototype chain, hence no modifications has to be done on any of its instances or "sub-classes". In that sense, how can a Class-based language like Python achieve Monkey patching? 回答1: The real question is, how can it not? In Python,

How to remove a library with monkeypatch or mock in pytest?

ぃ、小莉子 提交于 2020-12-15 06:38:22
问题 If my library has a contrib extra that has dependencies in it (say requests ) that I want users to have to install to have access to a CLI API, but I install the contrib extra during my tests in CI how do I use pytest's MonkeyPatch to remove the dependencies during tests to ensure my detection is correct? For example, if the contrib extra will additionally install requests and so I want users to have to do $ python -m pip install mylib[contrib] to then be able to at the command line have a

How to remove a library with monkeypatch or mock in pytest?

久未见 提交于 2020-12-15 06:37:51
问题 If my library has a contrib extra that has dependencies in it (say requests ) that I want users to have to install to have access to a CLI API, but I install the contrib extra during my tests in CI how do I use pytest's MonkeyPatch to remove the dependencies during tests to ensure my detection is correct? For example, if the contrib extra will additionally install requests and so I want users to have to do $ python -m pip install mylib[contrib] to then be able to at the command line have a