How to use patchy - 10 common examples

To help you get started, we’ve selected a few patchy examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github adamchainz / patchy / tests / test_patch.py View on Github external
def test_patch_instancemethod_twice():
    class Artist(object):
        def method(self):
            return "Chalk"

    patchy.patch(
        Artist.method,
        """\
        @@ -1,2 +1,2 @@
         def method(self):
        -    return "Chalk"
        +    return "Cheese"
        """,
    )

    patchy.patch(
        Artist.method,
        """\
        @@ -1,2 +1,2 @@
         def method(self):
        -    return "Cheese"
        +    return "Crackers"
        """,
    )

    assert Artist().method() == "Crackers"
github adamchainz / patchy / tests / test_patch.py View on Github external
def test_patch_mutable_default_arg():
    def foo(append=None, mutable=[]):  # noqa: B006
        if append is not None:
            mutable.append(append)
        return len(mutable)

    assert foo() == 0
    assert foo("v1") == 1
    assert foo("v2") == 2
    assert foo(mutable=[]) == 0

    patchy.patch(
        foo,
        """\
        @@ -1,2 +1,3 @@
         def foo(append=None, mutable=[]):
        +    len(mutable)
             if append is not None:
        """,
    )

    assert foo() == 2
    assert foo("v3") == 3
    assert foo(mutable=[]) == 0
github adamchainz / patchy / tests / test_patch.py View on Github external
package.join("__init__.py").ensure(file=True)
    package.join("mod.py").write(
        dedent(
            """\
        class Foo(object):
            def sample(self):
                return 1
        """
        )
    )
    sys.path.insert(0, six.text_type(tmpdir))
    try:
        from patch_by_path_pkg2.mod import Foo

        assert Foo().sample() == 1
        patchy.patch(
            "patch_by_path_pkg2.mod.Foo.sample",
            """\
            @@ -2,2 +2,2 @@
            -    return 1
            +    return 2
            """,
        )
    finally:
        sys.path.pop(0)

    assert Foo().sample() == 2
github adamchainz / patchy / tests / test_patch.py View on Github external
def test_patch_instancemethod():
    class Artist(object):
        def method(self):
            return "Chalk"

    patchy.patch(
        Artist.method,
        """\
        @@ -1,2 +1,2 @@
         def method(self):
        -    return "Chalk"
        +    return "Cheese"
        """,
    )

    assert Artist().method() == "Cheese"
github adamchainz / patchy / tests / test_patch.py View on Github external
def sample():
            pass
    """
        )
    )
    sys.path.insert(0, six.text_type(tmpdir))

    try:
        from future_user import sample
    finally:
        sys.path.pop(0)

    assert sample.__code__.co_flags & __future__.annotations.compiler_flag

    patchy.patch(
        sample,
        """\
        @@ -1,2 +1,3 @@
         def sample():
        +    pass
             pass
        """,
    )

    assert sample.__code__.co_flags & __future__.annotations.compiler_flag
github adamchainz / patchy / tests / test_patch.py View on Github external
def test_patch_instancemethod_mangled():
    class Artist(object):
        def __mangled_name(self, v):
            return v + " on toast"

        def method(self):
            filling = "Chalk"
            return self.__mangled_name(filling)

    patchy.patch(
        Artist.method,
        """\
        @@ -1,2 +1,2 @@
         def method(self):
        -    filling = "Chalk"
        +    filling = "Cheese"
             return self.__mangled_name(filling)
        """,
    )

    assert Artist().method() == "Cheese on toast"
github adamchainz / patchy / tests / test_patch.py View on Github external
def test_patch_staticmethod_twice():
    class Doge(object):
        @staticmethod
        def bark():
            return "Woof"

    patchy.patch(
        Doge.bark,
        """\
        @@ -1,3 +1,3 @@
         @staticmethod
         def bark():
        -    return "Woof"
        +    return "Wow\"""",
    )

    patchy.patch(
        Doge.bark,
        """\
        @@ -1,3 +1,3 @@
         @staticmethod
         def bark():
        -    return "Wow"
github adamchainz / patchy / tests / test_patch.py View on Github external
def test_patch_classmethod():
    class Emotion(object):
        def __init__(self, name):
            self.name = name

        @classmethod
        def create(cls, name):
            return cls(name)

    patchy.patch(
        Emotion.create,
        """\
        @@ -1,2 +1,3 @@
         @classmethod
         def create(cls, name):
        +    name = name.title()
             return cls(name)""",
    )

    assert Emotion.create("Happy").name == "Happy"
    assert Emotion.create("happy").name == "Happy"
github adamchainz / patchy / tests / test_replace.py View on Github external
def test_replace():
    def sample():
        return 1

    patchy.replace(
        sample,
        """\
        def sample():
            return 1
        """,
        """\
        def sample():
            return 42
        """,
    )

    assert sample() == 42
github adamchainz / patchy / tests / test_replace.py View on Github external
def test_replace_instancemethod():
    class Artist(object):
        def method(self):
            return "Chalk"

    patchy.replace(
        Artist.method,
        """\
        def method(self):
            return 'Chalk'
        """,
        """\
        def method(self):
            return 'Cheese'
        """,
    )

    assert Artist().method() == "Cheese"