[pytest-dev] monkeypatch.setattr versus replace
Anthon van der Neut
anthon at mnt.org
Fri Sep 13 13:13:07 CEST 2013
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")
in Python to get the same effect is out of the question AFAIK. Maybe I
should try and make an/my editor smart enough to handle strings that are
actually object-attribute-hierarchy references and do TAB completion on
those.
> which currently ends up as an error. Would it be too awkward to
> just make the latter work and forget about introducing a new "replace"
> method name? IMO it's rather intuitive to just make setattr accept
> the new signature, although the docstring will need a bit of explaining.
>
> cheers,
> holger
>
> _______________________________________________
> Pytest-dev mailing list
> Pytest-dev at python.org
> https://mail.python.org/mailman/listinfo/pytest-dev
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/pytest-dev/attachments/20130913/07955ef7/attachment.html>
More information about the Pytest-dev
mailing list