GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Mock spec not respected for attributes prefixed with assert · Issue #100739 · python/cpython

Mock spec not respected for attributes prefixed with assert #100739

New issue
Copy link
New issue
Copy link
Closed
Closed
Mock spec not respected for attributes prefixed with assert#100739
Copy link
Labels
type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

Description

@cklein
cklein
opened on Jan 4, 2023
Issue body actions

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

  • gh-100739: Respect mock spec when checking for unsafe prefixes #100740
  • [3.11] gh-100739: Respect mock spec when checking for unsafe prefixes (GH-100740) #100760
  • [3.10] gh-100739: Respect mock spec when checking for unsafe prefixes (GH-100740) #100761

Activity

  1. cklein
    added
    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
    on Jan 4, 2023
  2. cjw296
    added a commit that references this issue on Jan 4, 2023

    gh-100739: Respect mock spec when checking for unsafe prefixes (#100740)

    7f1eefc
  3. miss-islington
    added 4 commits that reference this issue on Jan 4, 2023

    pythongh-100739: Respect mock spec when checking for unsafe prefixes (p…

    9cbfcc8

    pythongh-100739: Respect mock spec when checking for unsafe prefixes (p…

    f07d620

    gh-100739: Respect mock spec when checking for unsafe prefixes (GH-10…

    541e7b8

    gh-100739: Respect mock spec when checking for unsafe prefixes (GH-10…

    f49cc3c
  4. AlexWaygood commented on Jan 4, 2023

    @AlexWaygood
    AlexWaygood
    on Jan 4, 2023
    Member
    More actions

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

  5. AlexWaygood
    closed this as completedon Jan 4, 2023
  6. carljm
    added a commit that references this issue on Jan 5, 2023

    Merge branch 'main' into fieldrepr

    60aa562
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.