Skip to main content

The magic_patch is a simple library that provides the ability to patch an object in different scopes at once. Now you can patch objects easily like magic.

Project description

The magic_patch is a simple library that provides the ability to patch an object in different scopes at once. Now you can patch objects easily like magic.

  • Example 1 * A project contains a few modules that imports datetime library
# module 1
from datetime import datetime

...
# module 2
from datetime import datetime

...

Using magic_patch you don't need to patch each module manually

# test
from unittest.mock import Mock
from magic_patch import magic_patch

from datetime import datetime  # import target object
from ... import start_module  # necessary to run imports 

datetime_mock = Mock()

with magic_patch(start_module, datetime, datetime_mock):
    # all done! - all modules patched!!!
    ...
  • Example 2 * A project contains a few modules that import the datetime class from datatime library There are different names for the target object. The magic_patch provides the ability to examine all available variables in each module
# module 1
from datetime import datetime

...
# module 2
from datetime import datetime as new_name_datetime

...

Just set up the option search_by_all_names=True and magic_patch finds your target with other names

# test
from unittest.mock import Mock
from magic_patch import magic_patch

from datetime import datetime  # import target object
from ... import start_module  # necessary to run imports 

datetime_mock = Mock()
with magic_patch(start_module, datetime, datetime_mock, search_by_all_names=True):
    # all done! - all modules patched!!!
    ...

Happy magic patching!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

magic_patch-0.0.2.tar.gz (3.2 kB view hashes)

Uploaded Source

Built Distribution

magic_patch-0.0.2-py3-none-any.whl (3.3 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page