Skip to content

Commit 94449d9

Browse files
committed
Compat cleanup in other modules
Various issues found by grepping for Python 2 comments and nocover pragmas.
1 parent 86bb906 commit 94449d9

35 files changed

+103
-277
lines changed

.flake8

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
[flake8]
22

33
exclude =
4-
hypothesis-python/src/hypothesis/vendor/*,
54
test_reflection.py,
65
test_imports.py,
76
test_lambda_formatting.py

hypothesis-python/.coveragerc

-1
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,3 @@ exclude_lines =
2222
__deepcopy__
2323
except ImportError:
2424
assert all\(.+\)
25-
if __reserved is not not_set:

hypothesis-python/src/hypothesis/_settings.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -627,7 +627,7 @@ def _validate_deadline(x):
627627
settings.lock_further_definitions()
628628

629629

630-
def note_deprecation(message, since):
630+
def note_deprecation(message, *, since):
631631
# type: (str, str) -> None
632632
if since != "RELEASEDAY":
633633
date = datetime.datetime.strptime(since, "%Y-%m-%d").date()

hypothesis-python/src/hypothesis/control.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ def target(observation, label=""):
168168
example shrinks right down to the threshold of failure (:issue:`2180`).
169169
"""
170170
check_type(float, observation, "observation")
171-
if math.isinf(observation) or math.isnan(observation):
171+
if not math.isfinite(observation):
172172
raise InvalidArgument("observation=%r must be a finite float." % observation)
173173
check_type(str, label, "label")
174174

hypothesis-python/src/hypothesis/core.py

-6
Original file line numberDiff line numberDiff line change
@@ -276,12 +276,6 @@ def wrapped_test(*arguments, **kwargs):
276276
"%s() got an unexpected keyword argument %r, from `%s=%r` in @given"
277277
% (name, arg, arg, given_kwargs[arg])
278278
)
279-
for a in original_argspec.args:
280-
if isinstance(a, list): # pragma: no cover
281-
return invalid(
282-
"Cannot decorate function %s() because it has destructuring arguments"
283-
% (name,)
284-
)
285279
if original_argspec.defaults or original_argspec.kwonlydefaults:
286280
return invalid("Cannot apply @given to a function with defaults.")
287281
missing = [repr(kw) for kw in original_argspec.kwonlyargs if kw not in given_kwargs]

hypothesis-python/src/hypothesis/database.py

+1-4
Original file line numberDiff line numberDiff line change
@@ -177,10 +177,7 @@ def save(self, key, value):
177177
mkdir_p(self._key_path(key))
178178
path = self._value_path(key, value)
179179
if not os.path.exists(path):
180-
suffix = binascii.hexlify(os.urandom(16))
181-
if not isinstance(suffix, str): # pragma: no branch
182-
# On Python 3, binascii.hexlify returns bytes
183-
suffix = suffix.decode("ascii")
180+
suffix = binascii.hexlify(os.urandom(16)).decode("ascii")
184181
tmpname = path + "." + suffix
185182
with open(tmpname, "wb") as o:
186183
o.write(value)

hypothesis-python/src/hypothesis/extra/numpy.py

+6-18
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
from hypothesis import assume
2626
from hypothesis.errors import InvalidArgument
2727
from hypothesis.internal.coverage import check_function
28-
from hypothesis.internal.reflection import proxies, reserved_means_kwonly_star
28+
from hypothesis.internal.reflection import proxies
2929
from hypothesis.internal.validation import check_type, check_valid_interval
3030
from hypothesis.strategies._internal import SearchStrategy
3131
from hypothesis.utils.conventions import UniqueIdentifier, not_set
@@ -1023,16 +1023,15 @@ def _hypothesis_parse_gufunc_signature(signature, all_checks=True):
10231023

10241024

10251025
@st.defines_strategy
1026-
@reserved_means_kwonly_star
10271026
def mutually_broadcastable_shapes(
1028-
__reserved=not_set, # type: Any
1027+
*,
10291028
num_shapes=not_set, # type: Union[UniqueIdentifier, int]
10301029
signature=not_set, # type: Union[UniqueIdentifier, str]
10311030
base_shape=(), # type: Shape
10321031
min_dims=0, # type: int
10331032
max_dims=None, # type: int
10341033
min_side=1, # type: int
1035-
max_side=None, # type: int
1034+
max_side=None # type: int
10361035
):
10371036
# type: (...) -> st.SearchStrategy[BroadcastableShapes]
10381037
"""Return a strategy for generating a specified number of shapes, N, that are
@@ -1108,9 +1107,6 @@ def mutually_broadcastable_shapes(
11081107
BroadcastableShapes(input_shapes=((3, 4, 2), (1, 2)), result_shape=(3, 4))
11091108
BroadcastableShapes(input_shapes=((4, 2), (1, 2, 3)), result_shape=(4, 3))
11101109
"""
1111-
if __reserved is not not_set:
1112-
raise InvalidArgument("Do not pass the __reserved argument.")
1113-
11141110
arg_msg = "Pass either the `num_shapes` or the `signature` argument, but not both."
11151111
if num_shapes is not not_set:
11161112
check_argument(signature is not_set, arg_msg)
@@ -1251,16 +1247,10 @@ def do_draw(self, data):
12511247

12521248

12531249
@st.defines_strategy
1254-
@reserved_means_kwonly_star
12551250
def basic_indices(
1256-
shape,
1257-
__reserved=not_set,
1258-
min_dims=0,
1259-
max_dims=None,
1260-
allow_newaxis=False,
1261-
allow_ellipsis=True,
1251+
shape, *, min_dims=0, max_dims=None, allow_newaxis=False, allow_ellipsis=True
12621252
):
1263-
# type: (Shape, Any, int, int, bool, bool) -> st.SearchStrategy[BasicIndex]
1253+
# type: (Shape, int, int, bool, bool) -> st.SearchStrategy[BasicIndex]
12641254
"""
12651255
The ``basic_indices`` strategy generates `basic indexes
12661256
<https://docs.scipy.org/doc/numpy/reference/arrays.indexing.html>`__ for
@@ -1288,10 +1278,8 @@ def basic_indices(
12881278
# Arguments to exclude scalars, zero-dim arrays, and dims of size zero were
12891279
# all considered and rejected. We want users to explicitly consider those
12901280
# cases if they're dealing in general indexers, and while it's fiddly we can
1291-
# back-compatibly add them later (hence using __reserved to sim kwonlyargs).
1281+
# back-compatibly add them later (hence using kwonlyargs).
12921282
check_type(tuple, shape, "shape")
1293-
if __reserved is not not_set:
1294-
raise InvalidArgument("Do not pass the __reserved argument.")
12951283
check_type(bool, allow_ellipsis, "allow_ellipsis")
12961284
check_type(bool, allow_newaxis, "allow_newaxis")
12971285
check_type(int, min_dims, "min_dims")

hypothesis-python/src/hypothesis/internal/cathetus.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#
1414
# END HEADER
1515

16-
from math import fabs, isinf, isnan, sqrt
16+
from math import fabs, inf, isinf, isnan, nan, sqrt
1717
from sys import float_info
1818

1919

@@ -35,21 +35,21 @@ def cathetus(h, a):
3535
Based on the C99 implementation https://github.com/jjgreen/cathetus
3636
"""
3737
if isnan(h):
38-
return float("nan")
38+
return nan
3939

4040
if isinf(h):
4141
if isinf(a):
42-
return float("nan")
42+
return nan
4343
else:
4444
# Deliberately includes the case when isnan(a), because the
4545
# C99 standard mandates that hypot(inf, nan) == inf
46-
return float("inf")
46+
return inf
4747

4848
h = fabs(h)
4949
a = fabs(a)
5050

5151
if h < a:
52-
return float("nan")
52+
return nan
5353

5454
# Thanks to floating-point precision issues when performing multiple
5555
# operations on extremely large or small values, we may rarely calculate

hypothesis-python/src/hypothesis/internal/conjecture/shrinking/floats.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
class Float(Shrinker):
2727
def setup(self):
28-
self.NAN = float("nan")
28+
self.NAN = math.nan
2929
self.debugging_enabled = True
3030

3131
def make_immutable(self, f):
@@ -46,11 +46,11 @@ def left_is_better(self, left, right):
4646
return lex1 < lex2
4747

4848
def short_circuit(self):
49-
for g in [sys.float_info.max, float("inf"), float("nan")]:
49+
for g in [sys.float_info.max, math.inf, math.nan]:
5050
self.consider(g)
5151

52-
# If we're stuck at a nasty float don't try to shrink it further. These
53-
if math.isinf(self.current) or math.isnan(self.current):
52+
# If we're stuck at a nasty float don't try to shrink it further.
53+
if not math.isfinite(self.current):
5454
return True
5555

5656
# If its too large to represent as an integer, bail out here. It's

hypothesis-python/src/hypothesis/internal/reflection.py

+3-56
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import re
2323
import types
2424
from functools import wraps
25+
from tokenize import detect_encoding
2526
from types import ModuleType
2627

2728
from hypothesis.internal.compat import (
@@ -32,11 +33,6 @@
3233
)
3334
from hypothesis.vendor.pretty import pretty
3435

35-
try:
36-
from tokenize import detect_encoding
37-
except ImportError: # pragma: no cover
38-
detect_encoding = None
39-
4036
if False:
4137
from typing import TypeVar # noqa
4238

@@ -266,17 +262,9 @@ def extract_lambda_source(f):
266262
"""
267263
argspec = inspect.getfullargspec(f)
268264
arg_strings = []
269-
# In Python 2 you can have destructuring arguments to functions. This
270-
# results in an argspec with non-string values. I'm not very interested in
271-
# handling these properly, but it's important to not crash on them.
272-
bad_lambda = False
273265
for a in argspec.args:
274-
if isinstance(a, (tuple, list)): # pragma: no cover
275-
arg_strings.append("(%s)" % (", ".join(a),))
276-
bad_lambda = True
277-
else:
278-
assert isinstance(a, str)
279-
arg_strings.append(a)
266+
assert isinstance(a, str)
267+
arg_strings.append(a)
280268
if argspec.varargs:
281269
arg_strings.append("*" + argspec.varargs)
282270
elif argspec.kwonlyargs:
@@ -292,8 +280,6 @@ def extract_lambda_source(f):
292280
if_confused = "lambda %s: <unknown>" % (", ".join(arg_strings),)
293281
else:
294282
if_confused = "lambda: <unknown>"
295-
if bad_lambda: # pragma: no cover
296-
return if_confused
297283
try:
298284
source = inspect.getsource(f)
299285
except OSError:
@@ -363,13 +349,6 @@ def extract_lambda_source(f):
363349
# an OSError. Or if `f` is a built-in function, in which case we get a
364350
# TypeError. In both cases, fall back to splitting the Unicode string.
365351
# It's not perfect, but it's the best we can do.
366-
#
367-
# Note 2: You can only detect the encoding with `tokenize.detect_encoding`
368-
# in Python 3.2 or later. But that's okay, because the only version that
369-
# affects for us is Python 2.7, and 2.7 doesn't support non-ASCII identifiers:
370-
# https://www.python.org/dev/peps/pep-3131/. In this case we'll get an
371-
# TypeError again because we set detect_encoding to None above.
372-
#
373352
try:
374353
with open(inspect.getsourcefile(f), "rb") as src_f:
375354
encoding, _ = detect_encoding(src_f.readline)
@@ -612,35 +591,3 @@ def accept(proxy):
612591
)
613592

614593
return accept
615-
616-
617-
def reserved_means_kwonly_star(func):
618-
# type: (C) -> C
619-
"""A decorator to implement Python-2-compatible kwonly args.
620-
621-
The following functions behave identically:
622-
def f(a, __reserved=not_set, b=None): ...
623-
def f(a, *, b=None): ...
624-
625-
Obviously this doesn't allow required kwonly args, but it's a nice way
626-
of defining forward-compatible APIs given our plans to turn all args
627-
with default values into kwonly args.
628-
"""
629-
signature = inspect.signature(func)
630-
seen = False
631-
parameters = []
632-
for param in signature.parameters.values():
633-
assert param.kind == inspect.Parameter.POSITIONAL_OR_KEYWORD
634-
if param.name == "__reserved":
635-
seen = True
636-
elif not seen:
637-
parameters.append(param)
638-
else:
639-
parameters.append(param.replace(kind=inspect.Parameter.KEYWORD_ONLY))
640-
assert seen, "function does not have `__reserved` argument"
641-
642-
func.__signature__ = signature.replace(parameters=parameters)
643-
newsig = define_function_signature(
644-
func.__name__, func.__doc__, inspect.getfullargspec(func)
645-
)
646-
return impersonate(func)(wraps(func)(newsig(func)))

hypothesis-python/src/hypothesis/strategies/_internal/core.py

+10-14
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@
5454
nicerepr,
5555
proxies,
5656
required_args,
57-
reserved_means_kwonly_star,
5857
)
5958
from hypothesis.internal.validation import (
6059
check_type,
@@ -430,7 +429,7 @@ def floats(
430429
Excluding either signed zero will also exclude the other.
431430
Attempting to exclude an endpoint which is None will raise an error;
432431
use ``allow_infinity=False`` to generate finite floats. You can however
433-
use e.g. ``min_value=float("-inf"), exclude_min=True`` to exclude only
432+
use e.g. ``min_value=-math.inf, exclude_min=True`` to exclude only
434433
one infinite endpoint.
435434
436435
Examples from this strategy have a complicated and hard to explain
@@ -481,9 +480,9 @@ def floats(
481480
"%d - use max_value=%r instead" % (max_arg, width, max_value)
482481
)
483482

484-
if exclude_min and (min_value is None or min_value == float("inf")):
483+
if exclude_min and (min_value is None or min_value == math.inf):
485484
raise InvalidArgument("Cannot exclude min_value=%r" % (min_value,))
486-
if exclude_max and (max_value is None or max_value == float("-inf")):
485+
if exclude_max and (max_value is None or max_value == -math.inf):
487486
raise InvalidArgument("Cannot exclude max_value=%r" % (max_value,))
488487

489488
if min_value is not None and (
@@ -505,9 +504,9 @@ def floats(
505504
max_value = next_down(max_value, width)
506505
assert max_value < max_arg # type: ignore
507506

508-
if min_value == float("-inf"):
507+
if min_value == -math.inf:
509508
min_value = None
510-
if max_value == float("inf"):
509+
if max_value == math.inf:
511510
max_value = None
512511

513512
bad_zero_bounds = (
@@ -538,9 +537,9 @@ def floats(
538537
"Cannot have allow_infinity=%r, with both min_value and "
539538
"max_value" % (allow_infinity)
540539
)
541-
elif min_value == float("inf"):
540+
elif min_value == math.inf:
542541
raise InvalidArgument("allow_infinity=False excludes min_value=inf")
543-
elif max_value == float("-inf"):
542+
elif max_value == -math.inf:
544543
raise InvalidArgument("allow_infinity=False excludes max_value=-inf")
545544

546545
unbounded_floats = FloatStrategy(
@@ -837,11 +836,10 @@ def iterables(
837836

838837

839838
@defines_strategy
840-
@reserved_means_kwonly_star
841839
def fixed_dictionaries(
842840
mapping, # type: Dict[T, SearchStrategy[Ex]]
843-
__reserved=not_set, # type: Any
844-
optional=None, # type: Dict[T, SearchStrategy[Ex]]
841+
*,
842+
optional=None # type: Dict[T, SearchStrategy[Ex]]
845843
):
846844
# type: (...) -> SearchStrategy[Dict[T, Ex]]
847845
"""Generates a dictionary of the same type as mapping with a fixed set of
@@ -858,8 +856,6 @@ def fixed_dictionaries(
858856
check_type(dict, mapping, "mapping")
859857
for k, v in mapping.items():
860858
check_strategy(v, "mapping[%r]" % (k,))
861-
if __reserved is not not_set:
862-
raise InvalidArgument("Do not pass __reserved; got %r" % (__reserved,))
863859
if optional is not None:
864860
check_type(dict, optional, "optional")
865861
for k, v in optional.items():
@@ -1915,7 +1911,7 @@ def complex_numbers(
19151911
check_valid_magnitude(min_magnitude, "min_magnitude")
19161912
check_valid_magnitude(max_magnitude, "max_magnitude")
19171913
check_valid_interval(min_magnitude, max_magnitude, "min_magnitude", "max_magnitude")
1918-
if max_magnitude == float("inf"):
1914+
if max_magnitude == math.inf:
19191915
max_magnitude = None
19201916
if min_magnitude == 0:
19211917
min_magnitude = None

hypothesis-python/src/hypothesis/strategies/_internal/numbers.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def do_draw(self, data):
7878
1.192092896e-07,
7979
2.2204460492503131e-016,
8080
]
81-
+ [float("inf"), float("nan")] * 5,
81+
+ [math.inf, math.nan] * 5,
8282
key=flt.float_to_lex,
8383
)
8484
NASTY_FLOATS = list(map(float, NASTY_FLOATS))

hypothesis-python/src/hypothesis/vendor/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# This file is part of Hypothesis, which may be found at
22
# https://github.com/HypothesisWorks/hypothesis/
33
#
4-
# Most of this work is copyright (C) 2013-2019 David R. MacIver
4+
# Most of this work is copyright (C) 2013-2020 David R. MacIver
55
# (david@drmaciver.com), but it contains contributions by others. See
66
# CONTRIBUTING.rst for a full list of people who may hold copyright, and
77
# consult the git log if you need to determine who owns an individual

0 commit comments

Comments
 (0)