15 comments

[ 0.21 ms ] story [ 47.7 ms ] thread
Man, having this as a context manager (maybe with the help of contextlib) would be great, because context managers are great.
That's the idea. Everything to make testing easier! :)
What's the use case?
Probably for the same reason why reopening Ruby classes is used so much. "hello, world".capitalize looks nicer than capitalize("hello, world"), and it's more convenient for method chaining.
If you are doing this for testing, it's already a feature of pytest: http://pytest.org/latest/monkeypatch.html
I might be wrong, but I don't think that it's actually possible to mock built-in objects (written in C, not in python) using pytest.

A very quick test would be trying to do something like this:

>>> monkeypatch(str, "blah", "bleh") >>> assert str.blah == "bleh"

I tried it here with no success though.

What are the license implications of using GPLv3 code as part of a test suite?
I don't think that there's any implications for that, but I'll probably relicense this library as MIT or at least LGPL pretty soon.

I just used my default licence template, which defaults to GPL.

Wow! Great news. It would be nice have this project under MIT license.
While it is a cool project, I hope I never have to work on a project that use this. I hate when people overwrite built-ins, but that might just be me.
Look good! I would like to see more examples of how to use it in tests tho!