Skip to content

Commit

Permalink
Fix hypothesis error in Python 3.10 for extremely small ("subnormal")…
Browse files Browse the repository at this point in the history
… histogram weights. Disable deprecations of deprecated Graph in its tests.
  • Loading branch information
ynikitenko committed Feb 18, 2022
1 parent 37b7e18 commit 9be50bd
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
8 changes: 6 additions & 2 deletions tests/structures/test_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
# from histogram_strategy import generate_increasing_list, generate_data_in_range


def test_graph_structure():
def test_graph():
xs = [0, 1]
ys = [2, 3]

Expand Down Expand Up @@ -84,7 +84,9 @@ def test_graph_structure():
assert gr4._points == [xs, [1, 1.5], [0.5, 1]]


def test_graph():
# Graph is deprecated, but we keep its tests at the moment.
@pytest.mark.filterwarnings('ignore::DeprecationWarning')
def test_Graph():
# sorts well
coords = range(0, 10)
values = map(math.sin, coords)
Expand Down Expand Up @@ -171,6 +173,7 @@ def test_graph():
assert gr1 == Graph()


@pytest.mark.filterwarnings('ignore::DeprecationWarning')
def test_graph_equal():
# test equality
gr1 = Graph([(0, 1)])
Expand All @@ -194,6 +197,7 @@ def test_graph_equal():
assert not gr2 == gr3


@pytest.mark.filterwarnings('ignore::DeprecationWarning')
def test_graph_to_csv():
# to_csv works
gr2 = Graph([(0, 1)])
Expand Down
17 changes: 14 additions & 3 deletions tests/structures/test_histogram.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
- scale (if filled with a non-negative weight) is always non-negative.
- scale of a histogram filled with *weight* is *weight* times scale.
- if all data fills into the histogram with bins size 1,
its scale is number of data points.
- if histogram bins are divided by N, its scale will be divided by N.
its scale is the number of data points.
- if histogram bins are divided by N, its scale is divided by N.
- Histogram and NumpyHistogram should be almost same (except for the upper edge).
"""
import math
import sys

import hypothesis
import pytest
Expand All @@ -30,7 +31,17 @@ def test_init_scale_zero(edges):
assert hist.scale() == 0


@given(edges=Edges1dStrategy, weight=s.floats(),
# we disable subnormals, because in Python 3.10 sometimes (!)
# the test failed with an error,
# where False = isclose((2.2216311086621543 * 5e-324), 1.5e-323)
# https://hypothesis.readthedocs.io/en/latest/data.html#hypothesis.strategies.floats
if sys.version_info.major > 2:
floats_ = s.floats(allow_subnormal=False)
else:
# in Python 2 allow_subnormal is not recognised
floats_ = s.floats()

@given(edges=Edges1dStrategy, weight=floats_,
refinement=s.integers(min_value=1, max_value=20),
data_samples=s.integers(min_value=1, max_value=50)
)
Expand Down

0 comments on commit 9be50bd

Please sign in to comment.