使用 .__wrapped__ 测试未修饰的函数会给出错误“需要 1 个以上的位置参数”

发布于 2025-01-15 20:23:04 字数 1268 浏览 3 评论 0原文

我在论坛上寻找过这个,但我可以找到任何精确处理 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

送舟行 2025-01-22 20:23:04

所以我想这与如何处理复制文件参数中的“self”有关,但我不知道从哪里开始理解 .__wrapped__ 做错了什么

你是完全正确的。

.__wrapped__ 属性在这里是一个(未绑定)函数(不是方法)

print(sync.copyfile.__wrapped__)
#  prints <function Sync.copyfile ...>, 
#  not    <bound method Sync.copyfile of <__main__.Sync object ...>

,因此需要显式提供一个“self”参数,例如:

# Change
sync.copyfile.__wrapped__('source_folder', 'dest_folder', f'{file}.txt')

# to                      ↓↓↓↓
sync.copyfile.__wrapped__(sync, 'source_folder', 'dest_folder', f'{file}.txt')

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

You're exactly right.

The .__wrapped__ attribute is an (unbound) function here (not a method)

print(sync.copyfile.__wrapped__)
#  prints <function Sync.copyfile ...>, 
#  not    <bound method Sync.copyfile of <__main__.Sync object ...>

and therefore needs a "self" argument supplied explicitly, e.g.:

# Change
sync.copyfile.__wrapped__('source_folder', 'dest_folder', f'{file}.txt')

# to                      ↓↓↓↓
sync.copyfile.__wrapped__(sync, 'source_folder', 'dest_folder', f'{file}.txt')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文