Skip to content

Commit

Permalink
Use canonical module names
Browse files Browse the repository at this point in the history
  • Loading branch information
Zac-HD committed Feb 18, 2024
1 parent 4c18799 commit 4acbb91
Show file tree
Hide file tree
Showing 10 changed files with 35 additions and 34 deletions.
7 changes: 4 additions & 3 deletions hypothesis-python/src/hypothesis/extra/ghostwriter.py
Original file line number Diff line number Diff line change
Expand Up @@ -732,9 +732,10 @@ def _get_module_helper(obj):

dots = [i for i, c in enumerate(module_name) if c == "."] + [None]
for idx in dots:
if getattr(sys.modules.get(module_name[:idx]), obj.__name__, None) is obj:
KNOWN_FUNCTION_LOCATIONS[obj] = module_name[:idx]
return module_name[:idx]
for candidate in (module_name[:idx].lstrip("_"), module_name[:idx]):
if getattr(sys.modules.get(candidate), obj.__name__, None) is obj:
KNOWN_FUNCTION_LOCATIONS[obj] = candidate
return candidate
return module_name


Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
# This test code was written by the `hypothesis.extra.ghostwriter` module
# and is provided under the Creative Commons Zero public domain dedication.

import _operator
import numpy
import operator
import test_expected_output
from hypothesis import given, strategies as st


@given(a=st.floats(), b=st.floats())
def test_equivalent_add_add_add(a: float, b: float) -> None:
result_0_add = _operator.add(a, b)
result_1_add = numpy.add(a, b)
result_0_add = numpy.add(a, b)
result_1_add = operator.add(a, b)
result_2_add = test_expected_output.add(a=a, b=b)
assert result_0_add == result_1_add, (result_0_add, result_1_add)
assert result_0_add == result_2_add, (result_0_add, result_2_add)
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# This test code was written by the `hypothesis.extra.ghostwriter` module
# and is provided under the Creative Commons Zero public domain dedication.

import _operator
import operator
from hypothesis import given, strategies as st

# TODO: replace st.nothing() with an appropriate strategy
Expand All @@ -11,4 +11,4 @@ truediv_operands = st.nothing()

@given(a=truediv_operands)
def test_identity_binary_operation_truediv(a):
assert a == _operator.truediv(a, "identity element here")
assert a == operator.truediv(a, "identity element here")
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# This test code was written by the `hypothesis.extra.ghostwriter` module
# and is provided under the Creative Commons Zero public domain dedication.

import _operator
import operator
from hypothesis import given, strategies as st

# TODO: replace st.nothing() with an appropriate strategy
Expand All @@ -11,4 +11,4 @@ truediv_operands = st.nothing()

@given(a=truediv_operands)
def test_identity_binary_operation_truediv(a) -> None:
assert a == _operator.truediv(a, "identity element here")
assert a == operator.truediv(a, "identity element here")
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# This test code was written by the `hypothesis.extra.ghostwriter` module
# and is provided under the Creative Commons Zero public domain dedication.

import _operator
import operator
import test_expected_output
from hypothesis import given, reject, strategies as st

Expand All @@ -10,7 +10,7 @@ from hypothesis import given, reject, strategies as st
def test_roundtrip_divide_mul(a: int, b: int) -> None:
try:
value0 = test_expected_output.divide(a=a, b=b)
value1 = _operator.mul(value0, b)
value1 = operator.mul(value0, b)
except ArithmeticError:
reject()
assert a == value1, (a, value1)
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# This test code was written by the `hypothesis.extra.ghostwriter` module
# and is provided under the Creative Commons Zero public domain dedication.

import _operator
import operator
import test_expected_output
from hypothesis import given, reject, strategies as st

Expand All @@ -12,5 +12,5 @@ def test_roundtrip_divide_mul(a: int, b: int) -> None:
value0 = test_expected_output.divide(a=a, b=b)
except ZeroDivisionError:
reject()
value1 = _operator.mul(value0, b)
value1 = operator.mul(value0, b)
assert a == value1, (a, value1)
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# This test code was written by the `hypothesis.extra.ghostwriter` module
# and is provided under the Creative Commons Zero public domain dedication.

import _operator
import operator
import test_expected_output
from hypothesis import given, reject, strategies as st

Expand All @@ -12,5 +12,5 @@ def test_roundtrip_divide_mul(a, b):
value0 = test_expected_output.divide(a=a, b=b)
except ZeroDivisionError:
reject()
value1 = _operator.mul(value0, b)
value1 = operator.mul(value0, b)
assert a == value1, (a, value1)
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# This test code was written by the `hypothesis.extra.ghostwriter` module
# and is provided under the Creative Commons Zero public domain dedication.

import _operator
import operator
import test_expected_output
from hypothesis import given, reject, strategies as st

Expand All @@ -13,7 +13,7 @@ def test_roundtrip_divide_mul(a: int, b: int) -> None:
value0 = test_expected_output.divide(a=a, b=b)
except ZeroDivisionError:
reject()
value1 = _operator.mul(value0, b)
value1 = operator.mul(value0, b)
except TypeError:
reject()
assert a == value1, (a, value1)
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# This test code was written by the `hypothesis.extra.ghostwriter` module
# and is provided under the Creative Commons Zero public domain dedication.

import _operator
import operator
from hypothesis import given, strategies as st

# TODO: replace st.nothing() with an appropriate strategy
Expand All @@ -11,25 +11,25 @@ mul_operands = st.nothing()

@given(a=mul_operands, b=mul_operands, c=mul_operands)
def test_associative_binary_operation_mul(a, b, c):
left = _operator.mul(a, _operator.mul(b, c))
right = _operator.mul(_operator.mul(a, b), c)
left = operator.mul(a, operator.mul(b, c))
right = operator.mul(operator.mul(a, b), c)
assert left == right, (left, right)


@given(a=mul_operands, b=mul_operands)
def test_commutative_binary_operation_mul(a, b):
left = _operator.mul(a, b)
right = _operator.mul(b, a)
left = operator.mul(a, b)
right = operator.mul(b, a)
assert left == right, (left, right)


@given(a=mul_operands)
def test_identity_binary_operation_mul(a):
assert a == _operator.mul(a, 1)
assert a == operator.mul(a, 1)


@given(a=mul_operands, b=mul_operands, c=mul_operands)
def test_add_distributes_over_binary_operation_mul(a, b, c):
left = _operator.add(_operator.mul(a, b), _operator.mul(a, c))
right = _operator.mul(a, _operator.add(b, c))
left = operator.add(operator.mul(a, b), operator.mul(a, c))
right = operator.mul(a, operator.add(b, c))
assert left == right, (left, right)
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# This test code was written by the `hypothesis.extra.ghostwriter` module
# and is provided under the Creative Commons Zero public domain dedication.

import _operator
import operator
import unittest
from hypothesis import given, strategies as st

Expand All @@ -13,22 +13,22 @@ class TestBinaryOperationmul(unittest.TestCase):

@given(a=mul_operands, b=mul_operands, c=mul_operands)
def test_associative_binary_operation_mul(self, a, b, c):
left = _operator.mul(a, _operator.mul(b, c))
right = _operator.mul(_operator.mul(a, b), c)
left = operator.mul(a, operator.mul(b, c))
right = operator.mul(operator.mul(a, b), c)
self.assertEqual(left, right)

@given(a=mul_operands, b=mul_operands)
def test_commutative_binary_operation_mul(self, a, b):
left = _operator.mul(a, b)
right = _operator.mul(b, a)
left = operator.mul(a, b)
right = operator.mul(b, a)
self.assertEqual(left, right)

@given(a=mul_operands)
def test_identity_binary_operation_mul(self, a):
self.assertEqual(a, _operator.mul(a, 1))
self.assertEqual(a, operator.mul(a, 1))

@given(a=mul_operands, b=mul_operands, c=mul_operands)
def test_add_distributes_over_binary_operation_mul(self, a, b, c):
left = _operator.add(_operator.mul(a, b), _operator.mul(a, c))
right = _operator.mul(a, _operator.add(b, c))
left = operator.add(operator.mul(a, b), operator.mul(a, c))
right = operator.mul(a, operator.add(b, c))
self.assertEqual(left, right)

0 comments on commit 4acbb91

Please sign in to comment.