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

Mock spec not respected for attributes prefixed with assert #100739

Closed
cklein opened this issue Jan 4, 2023 · 1 comment
Closed

Mock spec not respected for attributes prefixed with assert #100739

cklein opened this issue Jan 4, 2023 · 1 comment
Labels
type-bug An unexpected behavior, bug, or error

Comments

@cklein
Copy link
Contributor

cklein commented Jan 4, 2023

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

@cklein cklein added the type-bug An unexpected behavior, bug, or error label Jan 4, 2023
cjw296 pushed a commit that referenced this issue Jan 4, 2023
Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Jan 4, 2023
…ythonGH-100740)

(cherry picked from commit 7f1eefc)

Co-authored-by: Christian Klein <167265+cklein@users.noreply.github.com>
Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Jan 4, 2023
…ythonGH-100740)

(cherry picked from commit 7f1eefc)

Co-authored-by: Christian Klein <167265+cklein@users.noreply.github.com>
Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
miss-islington added a commit that referenced this issue Jan 4, 2023
…0740)

(cherry picked from commit 7f1eefc)

Co-authored-by: Christian Klein <167265+cklein@users.noreply.github.com>
Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
miss-islington added a commit that referenced this issue Jan 4, 2023
…0740)

(cherry picked from commit 7f1eefc)

Co-authored-by: Christian Klein <167265+cklein@users.noreply.github.com>
Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
@AlexWaygood
Copy link
Member

Fixed, and backported to 3.11 and 3.10. Thanks @cklein!

carljm added a commit to carljm/cpython that referenced this issue Jan 5, 2023
* main:
  pythonGH-100288: Remove LOAD_ATTR_METHOD_WITH_DICT instruction. (pythonGH-100753)
  pythonGH-100766: Note that locale.LC_MESSAGES is not universal (pythonGH-100702)
  Drop myself from pathlib maintenance (python#100757)
  pythongh-100739: Respect mock spec when checking for unsafe prefixes (python#100740)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

2 participants