Skip to content
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

unittest.mock.seal doesn't work as expected with AsyncMock #100287

Open
hauntsaninja opened this issue Dec 16, 2022 · 0 comments
Open

unittest.mock.seal doesn't work as expected with AsyncMock #100287

hauntsaninja opened this issue Dec 16, 2022 · 0 comments
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@hauntsaninja
Copy link
Contributor

hauntsaninja commented Dec 16, 2022

I noticed this while reviewing #100252 (comment)

I believe the following test case should pass, but it doesn't on main

import unittest
from unittest.mock import Mock, seal

class AsyncClass:
    async def async_method(self): pass
    def normal_method(self): pass

class Case(unittest.TestCase):
    def test_spec_normal_methods_on_class_with_mock_seal(self):
        mock = Mock(AsyncClass)
        seal(mock)
        # test passes, aka this raises AttributError
        with self.assertRaises(AttributeError):
            mock.normal_method
        # test fails, aka this does not raise AttributError
        with self.assertRaises(AttributeError):
            mock.async_method

unittest.main()

It's easy to fix, just need to move the clause that handles AsyncMock after the if self._mock_sealed: check.

cc @sobolevn who moved the if self._mock_sealed: check earlier in https://github.com/python/cpython/pull/28300/files , but not all the way

@hauntsaninja hauntsaninja added the type-bug An unexpected behavior, bug, or error label Dec 16, 2022
@sobolevn sobolevn added the stdlib Python modules in the Lib dir label Dec 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

2 participants