Skip to content

C Task objects cannot be mocked with multiple inhertance #111542

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
kristjanvalur opened this issue Oct 31, 2023 · 7 comments
Closed

C Task objects cannot be mocked with multiple inhertance #111542

kristjanvalur opened this issue Oct 31, 2023 · 7 comments
Labels
extension-modules C modules in the Modules dir topic-asyncio type-bug An unexpected behavior, bug, or error

Comments

@kristjanvalur
Copy link
Contributor

kristjanvalur commented Oct 31, 2023

Bug report

Bug description:

When inheriting from asyncio.Task, which by default is a C implemented object, and also from
a python class, mocking fails on the resulting object. Running the following code will result in an error, on python 3.11 and all other versions I've tested:

from asyncio import Task
from asyncio.tasks import _PyTask as PyTask
from unittest import mock

class Other:
    pass
class MyTask(Task, Other):
    def foo(self):
        pass

class MyPyTask(PyTask, Other):
    def foo(self):
        pass

async def func():
    pass

# this will fail with a TypeError
with mock.patch.object(MyPyTask(func()), "foo"):
    pass  # ok
with mock.patch.object(MyTask(func()), "foo"):
    pass  # this fails with TypeError

This results in:

    with mock.patch.object(MyTask(func()), "foo"):
  File "C:\Program Files\Python311\Lib\unittest\mock.py", line 1443, in __enter__
    original, local = self.get_original()
                      ^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\Python311\Lib\unittest\mock.py", line 1406, in get_original
    original = target.__dict__[name]
               ^^^^^^^^^^^^^^^
TypeError: this __dict__ descriptor does not support 'MyTask' objects

CPython versions tested on:

3.10, 3.11

Operating systems tested on:

Windows

@kristjanvalur kristjanvalur added the type-bug An unexpected behavior, bug, or error label Oct 31, 2023
@AlexWaygood AlexWaygood added topic-asyncio extension-modules C modules in the Modules dir labels Oct 31, 2023
@github-project-automation github-project-automation bot moved this to Todo in asyncio Oct 31, 2023
@sobolevn
Copy link
Member

sobolevn commented Nov 1, 2023

This seems to be solved on main:

» ./python.exe --version
Python 3.13.0a1+
                                                                                           
(.venv) ~/Desktop/cpython2  main ✗                                                        
» ./python.exe ex.py    
/Users/sobolev/Desktop/cpython2/ex.py:19: DeprecationWarning: There is no current event loop
  with mock.patch.object(MyPyTask(func()), "foo"):
                                          

@kristjanvalur
Copy link
Contributor Author

kristjanvalur commented Nov 1, 2023

Indeed. Using binary search I have found the commit:
4cfc1b8, from 29th of november 2022.
GH-91375: Port _asyncio static types to heap types and module state (#99122)

The issue is a different one, but it has fixed this issue as a side effect of re-structuring the types in the module.
Did this make it into 3.12?

@erlend-aasland
Copy link
Contributor

Did this make it into 3.12?

Unfortunately, we cannot backport these types of changes :(

ISTM, there is nothing to do for 3.12 and older, so I suggest closing this issue. cc. @kumaraditya303

@erlend-aasland erlend-aasland added the pending The issue will be closed if no feedback is provided label Nov 1, 2023
@kumaraditya303
Copy link
Contributor

Yeah nothing can be done for older versions. If you really want to patch it then avoid using C implementation by

sys.modules['_asyncio'] = None

@erlend-aasland erlend-aasland closed this as not planned Won't fix, can't repro, duplicate, stale Nov 1, 2023
@github-project-automation github-project-automation bot moved this from Todo to Done in asyncio Nov 1, 2023
@erlend-aasland erlend-aasland removed the pending The issue will be closed if no feedback is provided label Nov 1, 2023
@kristjanvalur
Copy link
Contributor Author

kristjanvalur commented Nov 1, 2023

Did this make it into 3.12?

Unfortunately, we cannot backport these types of changes :(

What I meant was that since the commit was almost a year ago, did it make it into the 3.12 branch before it got branched off master? I haven't been a regular core contributor for almost 10 years now and don't follow the development roadmap too closely.

However, I do take it from your reply that it didn't.

Cheers, and thanks for looking into this with me.

@sobolevn
Copy link
Member

sobolevn commented Nov 1, 2023

I think that it made into 3.12, but not into 3.11

@terryjreedy
Copy link
Member

@kristjanvalur Main and 3.12.0 were separated in May. With our current yearly schedule, this is predictable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
extension-modules C modules in the Modules dir topic-asyncio type-bug An unexpected behavior, bug, or error
Projects
Status: Done
Development

No branches or pull requests

6 participants