Re: [pytest-dev] monkeypatch.setattr versus replace
On Fri, Sep 13, 2013 at 13:12 +0200, Anthon van der Neut wrote:
On 2013-09-13 12:33, holger krekel wrote:
Hi all,
this was discussed before on IRC, but here is an update and a question. currently pytest-trunk implements a convenient new monkeypatch.replace method:
# example usage monkeypatch.replace("os.path.abspath", lambda x: "/abc")
This effectively performs:
import os.path monkeypatch.setattr(os.path, "abspath", lambda x: "/abc")
However, i just wanted to use it and couldn't remember the "replace" name and don't find it very telling anymore. I often do something like this::
monkeypatch.setattr("os.path.abspath", lambda x: "/abc")
As long as the old 3 parameter setattr is not going to be away, that seems fine. Depending on the length of the string I would probably still use the 3 parameter version to get the <TAB> completion on the first argument. Unfortunately just writing
monkeypatch.setattr(os.path.abspath, lambda x: "/abc")
It's actually possible to make this work in many common cases, but is bordering on the magic side. holger
[resending to list - apologies] On 13 September 2013 13:32, holger krekel <holger@merlinux.eu> wrote:
monkeypatch.setattr(os.path.abspath, lambda x: "/abc")
It's actually possible to make this work in many common cases, but is bordering on the magic side.
This would actually be my favourite and is what I usually type when I'm not thinking. However there is nothing wrong with the two-argument version where the first is a string either, so I'm +1 on that. Accepting this form is then a separate issue to consider. Regards, Floris
participants (2)
-
Floris Bruynooghe -
holger krekel