Live data from Hacker News

Show HN: Modshim – A new alternative to monkey-patching in Python

github.com

11–20 of 32 posts

Re: Show HN: Modshim – A new alternative to monkey-patching in Python

#12
Of these:

> * Fix bugs in third-party libraries without forking

> * Modify the behavior of existing functions

> * Add new features or options to existing classes

> * Test alternative implementations in an isolated way

only the last sounds close to something you might actually want to do, and then only as a throwaway thing

If you want to change a library, fork it. If you want to change the behavior of existing functions, don't or at least fork first. If you want to add new features to a class, write a new class, or again, at least fork first

Re: Show HN: Modshim – A new alternative to monkey-patching in Python

#13
For context: one of the several projects I'm working on right now is an automated extraction system for literate-code-style documentation in python. This isn't the place nor time to talk about the why of it (especially compared to other existing similar solutions). The important thing is the how: it uses a temporary import hook to stub out all module imports, allowing the docs generator to process each module independently at runtime, track imports between them, etc. At the end of the process, it also cleans itself up nicely.

Point being, it's a lot of really complicated fiddling with the python import system. And a lesson I have learned is that messing around with import internals in python is extremely tricky to get right. Furthermore, trying to coordinate correctly between modules that do and don't get modified my the hook is very finicky. Not to mention that supply side attacks on the import system itself could be a terrifying attack vector that would be absurdly difficult to detect.

All this to say, I'm not a big fan of monkeypatching, but I know exactly how it behaves, its edge cases, and what to expect if I do it. It is, after all, pretty standard practice to patch things during python unit tests. And even with all its warts, I would prefer patching to import fiddling any day of the week and twice on Sunday.

Feedback for the author: you need to explain the "why" of your project more thoroughly. I'm sure you had a good reason to strike out in this direction, and maybe this is a super elegant solution. But you've failed to explain to me under what circumstances I might also encounter the same problems with patching that you've encountered, in order to explain to me why the risk of an import hook is justified.

Re: Show HN: Modshim – A new alternative to monkey-patching in Python

#14
post #13

For context: one of the several projects I'm working on right now is an automated extraction system for literate-code-style documentation in python. This isn't the place nor time to talk about the why of it (especially compared to other existing similar solutions). The important thing is the how: it uses a temporary import hook to stub out all module imports, allowing the docs generator to process each module indepen…

Sounds super interesting. Is it ready to demo?

Re: Show HN: Modshim – A new alternative to monkey-patching in Python

#15
post #13

For context: one of the several projects I'm working on right now is an automated extraction system for literate-code-style documentation in python. This isn't the place nor time to talk about the why of it (especially compared to other existing similar solutions). The important thing is the how: it uses a temporary import hook to stub out all module imports, allowing the docs generator to process each module indepen…

Sounds super interesting. Is it ready to demo?

No, but I'll definitely post it to HN when it is!

Re: Show HN: Modshim – A new alternative to monkey-patching in Python

#16
post #13

For context: one of the several projects I'm working on right now is an automated extraction system for literate-code-style documentation in python. This isn't the place nor time to talk about the why of it (especially compared to other existing similar solutions). The important thing is the how: it uses a temporary import hook to stub out all module imports, allowing the docs generator to process each module indepen…

I didn't really get why I'd want to actually use it (vs. just a cool demo) either, until:

> means if you want to make changes to a third-party package, you don't have to take on the maintenance burden of forking, you can package and distribute just your changes.

That's a big win. I've seen and done my share of `# this file from github.com/blah with minor change X to L123` etc.

Re: Show HN: Modshim – A new alternative to monkey-patching in Python

#17
post #16
post #13

For context: one of the several projects I'm working on right now is an automated extraction system for literate-code-style documentation in python. This isn't the place nor time to talk about the why of it (especially compared to other existing similar solutions). The important thing is the how: it uses a temporary import hook to stub out all module imports, allowing the docs generator to process each module indepen…

I didn't really get why I'd want to actually use it (vs. just a cool demo) either, until: > means if you want to make changes to a third-party package, you don't have to take on the maintenance burden of forking, you can package and distribute just your changes. That's a big win. I've seen and done my share of `# this file from github.com/blah with minor change X to L123` etc.

If the goal is to actually package and distribute the changes via import hook, that makes the supply chain attack question particularly relevant. And it still doesn't explain why you couldn't just package and distribute the monkeypatch itself, instead of creating a whole new import ecosystem surrounding hooks.

I've done my fair share of that too, but I'm still not seeing the benefit vs patching.

Re: Show HN: Modshim – A new alternative to monkey-patching in Python

#19
This is interesting, and I'll try to remember to give this a go next time I'm tempted to patch something from the standard library, but...

The README mentions 3 scenarios that this might be preferred over, but not the fourth which I regularly do: Create my own functions/classes that are composed from the unchanged modules. E.g. a request_with_retries function which adds retry logic to requests without the need to monkey patch. I regularly use decorators as well to add things like retries.

For more complex scenarios Modshim might win out, as mentioned in the understated section of the README "Benefits of this Approach":

> Internal Reference Rewriting: This example demonstrates modshim's most powerful feature. By replacing requests.sessions.Session, we automatically upgraded top-level functions like requests.get() because their internal references to Session are redirected to our new class.

> Preservation of the Original Module: The original requests package is not altered. Code in other parts of an application that imports requests directly will continue to use the original Session object without any retry logic, preventing unintended side-effects.

What I think this means is Modshim lets you really get in to the guts of a module (monkey-patch style, giving you god-like powers), while limiting the damage.

Re: Show HN: Modshim – A new alternative to monkey-patching in Python

#20
post #15

Earlier quoted context omitted.

Sounds super interesting. Is it ready to demo?

No, but I'll definitely post it to HN when it is!

Please do - I’m very interested in ways to keep code and documentation tightly in sync.
Post reply on HN