Check for assert prefixes doesn't respect spec


Python 3.12.0a3+ (heads/disallow_mock_method_prefix:7bc8c46056, Jan  2 2023, 17:24:32) [Clang 14.0.0 (clang-1400.0.29.202)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from unittest.mock import Mock
>>> class Foo:
...     def assert_something(self):
...         pass
>>> m1 = Mock(spec=Foo)
>>> m1.assert_something()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/cklein/github/cklein/cpython/Lib/unittest/", line 657, in __getattr__
    raise AttributeError(
AttributeError: 'assert_something' is not a valid assertion. Use a spec for the mock if 'assert_something' is meant to be an attribute.

The error message suggests that accessing attributes with prefix “assert_” should work when using a spec.

See Respect mock spec when checking for unsafe prefixes · cklein/cpython@735ffc4 · GitHub for a possible fix

Unless someone says otherwise in a day, I think you should file a bug report.

Thanks Terry, will do!

Ticket: Mock spec not respected for attributes prefixed with assert · Issue #100739 · python/cpython · GitHub
PR: gh-100739: Respect mock spec when checking for unsafe prefixes by cklein · Pull Request #100740 · python/cpython · GitHub