Hasty Briefsbeta

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

6 days ago
  • #Python
  • #enhancement
  • #library
  • Modshim is a Python library for enhancing existing modules without modifying their source code.
  • It provides a clean alternative to forking, vendoring, and monkey-patching.
  • Key features include fixing bugs in third-party libraries, modifying behavior of existing functions, and adding new features.
  • Modshim works by creating a new 'shimmed' module that combines original code with enhancements.
  • Example use case: adding a prefix argument to TextWrapper in the textwrap module.
  • Modshim preserves the original module, ensuring no unintended side-effects.
  • It supports complex scenarios like adding retry mechanisms to the requests library.
  • Modshim uses AST rewriting to redirect internal references, maintaining consistency.
  • Advantages over monkey-patching include no global pollution and better maintainability.
  • Compared to forking, modshim reduces maintenance overhead and dependency complexity.