使用 .__wrapped__ 测试未修饰的函数会给出错误“需要 1 个以上的位置参数”
我在论坛上寻找过这个,但我可以找到任何精确处理 functool 包裹的东西...
我有一个带有函数 copyfile 和装饰器文件处理的类,定义为:
class Sync():
...
def fileprocessing(func):
"decorator for copyfile methods"
@functools.wraps(func)
def wrapped_f(*args):
...#some code invoking copyfile
return wrapped_f
@fileprocessing
def copyfile(self,src, dst, file):
"try copying with shutil file in src folder to dst folder, otherwise with python"
try:
shutil.copy2(f'{src}/{file}',f'{dst}/{file}', follow_symlinks=False)
except Exception as err:
print(err)
self.pythoncopyfile(f'{src}/{file}',f'{dst}/{file}')
我正在尝试使用 pytest 测试这个函数,它在以下情况下工作正常它是装饰的。 但是,我想测试未修饰的函数。
我放入 test_file.py :
def test_undecorated_copyfile():
sync=Sync()
for file in range(3):
sync.copyfile.__wrapped__('source_folder', 'dest_folder', f'{file}.txt')
当我运行 pytest 时,它会抛出“TypeError: copyfile() Missing 1 requiredpositional argument: 'file'”
所以我想这与如何处理复制文件参数中的“self”,但我不知道从哪里开始理解 .__wrapped__ 做错了什么
我试图在论坛上查看,但我得到的只是如何取消装饰一个函数(用 ._包裹_ ),在正常情况下如何处理 self。
我不知道如何处理这个错误以及使用哪些对象或方法来调查
I have looked for this on the forum but i could find anything precisely dealing with functool wrapped...
I have a class with a function copyfile and a decorator fileprocessing defined as :
class Sync():
...
def fileprocessing(func):
"decorator for copyfile methods"
@functools.wraps(func)
def wrapped_f(*args):
...#some code invoking copyfile
return wrapped_f
@fileprocessing
def copyfile(self,src, dst, file):
"try copying with shutil file in src folder to dst folder, otherwise with python"
try:
shutil.copy2(f'{src}/{file}',f'{dst}/{file}', follow_symlinks=False)
except Exception as err:
print(err)
self.pythoncopyfile(f'{src}/{file}',f'{dst}/{file}')
I am trying to test this function with pytest and it works fine when it is decorated.
However, i want to test the undecorated function.
I put in my test_file.py :
def test_undecorated_copyfile():
sync=Sync()
for file in range(3):
sync.copyfile.__wrapped__('source_folder', 'dest_folder', f'{file}.txt')
And when i run pytest, it throws me "TypeError: copyfile() missing 1 required positional argument: 'file' "
So i guess this has something to do with how to handle "self" in the copyfile arguments, but i dont know where to begin with to understand what .__wrapped__ is doing wrong
I tried to look on the forum but all i get is how to undecorate a function (with ._wrapped_ ), how to deal with self in normal situations.
I dont know how to deal with this error and which objects or methods to use to investigate
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你是完全正确的。
.__wrapped__
属性在这里是一个(未绑定)函数(不是方法),因此需要显式提供一个“self”参数,例如:
You're exactly right.
The
.__wrapped__
attribute is an (unbound) function here (not a method)and therefore needs a "self" argument supplied explicitly, e.g.: