/zz-patch-package

Automatically make and apply patches to Python dependencies

MIT LicenseMIT

zz-patch-package

Automatically make and apply patches to Python dependencies

Problem

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 stuff

and 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_source

This has a number of downsides

  1. Sometimes requires creativity, complexity, or additional copy/pasting to even use f_override in place of f in the source
  2. Requires maintaining this code and checking for changes to do lots of stuff and do lots more stuff any time the dependency package changes
  3. 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.

Solution

Installation

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.

Making a patch

  1. Make changes directly in dependecy/source.py
  2. python -m zz-patch-package dependency

Other details

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.