Hacker News new | ask | show | jobs
by o11c 242 days ago
The import limitation seems to make this not useful for me. Usually when I am monkeypatching, it's because some code I do not control has a (possibly dynamic) import of the "buggy" module under another name, so I need to make my changes visible under the original name.

If I control all the imports I can usually subclass things myself just fine.

1 comments

> Because our enhanced Session class now enables retries by default, we don't even need to instantiate it directly. modshim's AST rewriting ensures that internal references within the requests module are updated. This means convenience functions like requests.get() will automatically use our enhanced Session class

This seems to explicitly handle the case you are interested in - automatically updating library-internal references to the lower to instead use the upper?

That's talking about internal imports (and static - as much as python supports - ones at that), not external ones.

If A is my application, B is buggy, and C is some other library, consider:

  # A.py
  monkeypatch_B()
  import C

  # C.py
  B = __import__('B')

  # B.py
  bugs()
It should in theory be possible to mount the new virtual package over the lower module - but I don't think works currently (I'll have to test this). Doing this would make modifications available globally like you describe.