Skip to content

Commit

Permalink
Merge pull request #2131 from HypothesisWorks/DRMacIver/simpler-mock-…
Browse files Browse the repository at this point in the history
…checks

Simplify checking for mock objects
  • Loading branch information
DRMacIver committed Oct 14, 2019
2 parents 48576da + 27d5b05 commit 0c3bba6
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
5 changes: 5 additions & 0 deletions hypothesis-python/RELEASE.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
RELEASE_TYPE: patch

This release changes how Hypothesis checks if a parameter to a test function is a mock object.
It is unlikely to have any noticeable effect, but may result in a small performance improvement,
especially for test functions where a mock object is being passed as the first argument.
21 changes: 9 additions & 12 deletions hypothesis-python/src/hypothesis/internal/reflection.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import re
import tokenize
import types
import uuid
from functools import wraps
from types import ModuleType

Expand Down Expand Up @@ -55,17 +54,15 @@ def fully_qualified_name(f):


def is_mock(obj):
"""Determine if the given argument is a mock type.
We want to be able to detect these when dealing with various test
args. As they are sneaky and can look like almost anything else,
we'll check this by looking for random attributes. This is more
robust than looking for types.
"""
for _ in range(10):
if not hasattr(obj, str(uuid.uuid4())):
return False
return True
"""Determine if the given argument is a mock type."""

# We want to be able to detect these when dealing with various test
# args. As they are sneaky and can look like almost anything else,
# we'll check this by looking for an attribute with a name that it's really
# unlikely to implement accidentally, and that anyone who implements it
# deliberately should know what they're doing. This is more robust than
# looking for types.
return hasattr(obj, "hypothesis_internal_is_this_a_mock_check")


def function_digest(function):
Expand Down

0 comments on commit 0c3bba6

Please sign in to comment.