Skip to content

Mock spec not respected for attributes prefixed with assert #100739

@cklein

Description

@cklein

Bug report

Example:

from unittest.mock import Mock
class Foo:
    def assert_something(self):
         pass

m = Mock(spec=Foo)
m.assert_something()

An exception is raised:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/cklein/github/cklein/cpython/Lib/unittest/mock.py", 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.

Python 3.9 and lower:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/unittest/mock.py", line 635, in __getattr__
    raise AttributeError("Attributes cannot start with 'assert' "
AttributeError: Attributes cannot start with 'assert' or 'assret'

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

See cklein@735ffc4 for a possible fix

Your environment

  • CPython versions tested on:
  • 3.12.0a3+
  • 3.11.1
  • 3.10.9
  • 3.9.16
  • 3.8.16
  • 3.7.16
  • Operating system and architecture:
    • Darwin 21.6.0 on arm64
    • Linux 5.4.0-1088-aws on x86_64 (only Python 3.7.5 and 3.8.0)

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy