Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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"
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
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
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"
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
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"
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"
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"
def patch_QuerySet():
patchy.patch(
QuerySet.annotate,
"""\
@@ -17,7 +17,7 @@