Automatically make and apply patches to Python dependencies
Monkey patching is great in some cases but can lead to a lot of overhead in others. For example, if the dependency source has
# dependency/source.py
def f():
# do lots of stuff
print(1)
# do lots more stuffand I need to change this to print(2) for my purposes, the only way to do this with monkey patching is often
def f_override():
# copied from dependecy/source.py
# copy do lots of stuff
print(2)
# copy do lots more stuff
# somehow replace f with f_override with dependency_sourceThis has a number of downsides
- Sometimes requires creativity, complexity, or additional copy/pasting to even use
f_overridein place offin the source - Requires maintaining this code and checking for changes to
do lots of stuffanddo lots more stuffany time thedependencypackage changes - It is hard to identify what are intentional changes you made, what is the copy/pasted source, and what are hacks to get the whole thing to work
Existing solutions include
but both have not beeen updated recently and using them to automate this whole process requires you to script around them a bunch.
pip install zz-patch-package
If you list the package somewhere like requirements.txt, ensure it is the last entry (which should happen automatically if the list is alphabetized) so that all other packages are installed first.
- Make changes directly in
dependecy/source.py python -m zz-patch-package dependency
Beyond installing zz-patch-package and making the patch, no other effort is normally required.
Patches generated by zz-patch-package are stored in patches_python/ and are automatically applied when zz-patch-package is installed.
If the version of the dependency doesn't match the version of the patch, zz-patch-package will attempt to update the patch and apply it.
If a patch fails at any point, zz-patch-package will print the errors for that patch and keep working.