Skip to content

Commit 86bb906

Browse files
committed
Shrink compat.py
1 parent 60fe37d commit 86bb906

File tree

101 files changed

+509
-1344
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

101 files changed

+509
-1344
lines changed

.flake8

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

33
exclude =
4-
compat.py,
54
hypothesis-python/src/hypothesis/vendor/*,
65
test_reflection.py,
76
test_imports.py,
@@ -15,7 +14,3 @@ ignore =
1514
B008,B011,
1615
# flake8-bandit security warnings we disagree with or don't mind
1716
S101,S102,S105,S110,S307,S311,S404,S6
18-
19-
# Use flake8-alfred to forbid builtins that require compatibility wrappers.
20-
warn-symbols=
21-
bytes=Instead of bytes(), use hbytes() or binary_type

hypothesis-python/.coveragerc

-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,5 @@ exclude_lines =
2121
__copy__
2222
__deepcopy__
2323
except ImportError:
24-
if PY2:
2524
assert all\(.+\)
2625
if __reserved is not not_set:

hypothesis-python/docs/examples.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ So, convinced that our implementation is broken, we write a better one:
164164
.. code:: python
165165
166166
def sort_nodes(xs):
167-
for i in hrange(1, len(xs)):
167+
for i in range(1, len(xs)):
168168
j = i - 1
169169
while j >= 0:
170170
if xs[j].sorts_before(xs[j + 1]):

hypothesis-python/src/hypothesis/_settings.py

+8-11
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
InvalidArgument,
3434
InvalidState,
3535
)
36-
from hypothesis.internal.compat import integer_types, quiet_raise, string_types
3736
from hypothesis.internal.reflection import get_pretty_function_description
3837
from hypothesis.internal.validation import check_type, try_convert
3938
from hypothesis.utils.conventions import not_set
@@ -308,14 +307,14 @@ def register_profile(name, parent=None, **kwargs):
308307
keyword arguments for each setting that will be set differently to
309308
parent (or settings.default, if parent is None).
310309
"""
311-
check_type(string_types, name, "name")
310+
check_type(str, name, "name")
312311
settings._profiles[name] = settings(parent=parent, **kwargs)
313312

314313
@staticmethod
315314
def get_profile(name):
316315
# type: (str) -> settings
317316
"""Return the profile with the given name."""
318-
check_type(string_types, name, "name")
317+
check_type(str, name, "name")
319318
try:
320319
return settings._profiles[name]
321320
except KeyError:
@@ -330,7 +329,7 @@ def load_profile(name):
330329
Any setting not defined in the profile will be the library
331330
defined default for that setting.
332331
"""
333-
check_type(string_types, name, "name")
332+
check_type(str, name, "name")
334333
settings._current_profile = name
335334
settings._assign_default_internal(settings.get_profile(name))
336335

@@ -578,18 +577,16 @@ def _validate_deadline(x):
578577
"number of milliseconds, or None to disable the per-test-case deadline."
579578
% (x, type(x).__name__)
580579
)
581-
if isinstance(x, integer_types + (float,)):
580+
if isinstance(x, (int, float)):
582581
if isinstance(x, bool):
583582
raise invalid_deadline_error
584583
try:
585584
x = duration(milliseconds=x)
586585
except OverflowError:
587-
quiet_raise(
588-
InvalidArgument(
589-
"deadline=%r is invalid, because it is too large to represent "
590-
"as a timedelta. Use deadline=None to disable deadlines." % (x,)
591-
)
592-
)
586+
raise InvalidArgument(
587+
"deadline=%r is invalid, because it is too large to represent "
588+
"as a timedelta. Use deadline=None to disable deadlines." % (x,)
589+
) from None
593590
if isinstance(x, datetime.timedelta):
594591
if x <= datetime.timedelta(0):
595592
raise InvalidArgument(

hypothesis-python/src/hypothesis/control.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
from hypothesis import Verbosity, settings
2020
from hypothesis.errors import CleanupFailed, InvalidArgument, UnsatisfiedAssumption
21-
from hypothesis.internal.compat import string_types
2221
from hypothesis.internal.conjecture.data import ConjectureData
2322
from hypothesis.internal.validation import check_type
2423
from hypothesis.reporting import report, verbose_report
@@ -171,7 +170,7 @@ def target(observation, label=""):
171170
check_type(float, observation, "observation")
172171
if math.isinf(observation) or math.isnan(observation):
173172
raise InvalidArgument("observation=%r must be a finite float." % observation)
174-
check_type(string_types, label, "label")
173+
check_type(str, label, "label")
175174

176175
context = _current_build_context.value
177176
if context is None:

hypothesis-python/src/hypothesis/core.py

+8-25
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
import traceback
2525
import warnings
2626
import zlib
27+
from inspect import getfullargspec
28+
from io import StringIO
2729
from random import Random
2830
from unittest import TestCase
2931

@@ -54,13 +56,9 @@
5456
)
5557
from hypothesis.executors import new_style_executor
5658
from hypothesis.internal.compat import (
57-
PY2,
5859
bad_django_TestCase,
5960
benchmark_time,
60-
binary_type,
6161
get_type_hints,
62-
getfullargspec,
63-
hbytes,
6462
int_from_bytes,
6563
qualname,
6664
)
@@ -95,7 +93,7 @@
9593
SearchStrategy,
9694
)
9795
from hypothesis.utils.conventions import infer
98-
from hypothesis.vendor.pretty import CUnicodeIO, RepresentationPrinter
96+
from hypothesis.vendor.pretty import RepresentationPrinter
9997
from hypothesis.version import __version__
10098

10199
if False:
@@ -182,9 +180,7 @@ def accept(test):
182180

183181

184182
def encode_failure(buffer):
185-
# This needs to be a real bytes() instance, so we use binary_type()
186-
# instead of hbytes() here.
187-
buffer = binary_type(buffer)
183+
buffer = bytes(buffer)
188184
compressed = zlib.compress(buffer)
189185
if len(compressed) < len(buffer):
190186
buffer = b"\1" + compressed
@@ -308,10 +304,7 @@ class ArtificialDataForExample(ConjectureData):
308304
def __init__(self, kwargs):
309305
self.__draws = 0
310306
self.__kwargs = kwargs
311-
312-
super().__init__(
313-
max_length=0, prefix=hbytes(), random=None,
314-
)
307+
super().__init__(max_length=0, prefix=b"", random=None)
315308

316309
def draw_bits(self, n):
317310
raise NotImplementedError() # pragma: no cover
@@ -562,7 +555,7 @@ def run(data):
562555
text_repr[0] = arg_string(test, args, kwargs)
563556

564557
if print_example or current_verbosity() >= Verbosity.verbose:
565-
output = CUnicodeIO()
558+
output = StringIO()
566559

567560
printer = RepresentationPrinter(output)
568561
if print_example:
@@ -1088,18 +1081,8 @@ def wrapped_test(*arguments, **kwargs):
10881081
# full of Hypothesis internals they don't care about.
10891082
# We have to do this inline, to avoid adding another
10901083
# internal stack frame just when we've removed the rest.
1091-
if PY2:
1092-
# Python 2 doesn't have Exception.with_traceback(...);
1093-
# instead it has a three-argument form of the `raise`
1094-
# statement. Unfortunately this is a SyntaxError on
1095-
# Python 3, and before Python 2.7.9 it was *also* a
1096-
# SyntaxError to use it in a nested function so we
1097-
# can't `exec` or `eval` our way out (BPO-21591).
1098-
# So unless we break some versions of Python 2, none
1099-
# of them get traceback elision.
1100-
raise
1101-
# On Python 3, we swap out the real traceback for our
1102-
# trimmed version. Using a variable ensures that the line
1084+
#
1085+
# Using a variable for our trimmed error ensures that the line
11031086
# which will actually appear in tracebacks is as clear as
11041087
# possible - "raise the_error_hypothesis_found".
11051088
the_error_hypothesis_found = e.with_traceback(

hypothesis-python/src/hypothesis/database.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
from hypothesis.configuration import mkdir_p, storage_directory
2222
from hypothesis.errors import HypothesisException, HypothesisWarning
23-
from hypothesis.internal.compat import hbytes
2423
from hypothesis.utils.conventions import not_set
2524

2625

@@ -124,10 +123,10 @@ def fetch(self, key):
124123
yield from self.data.get(key, ())
125124

126125
def save(self, key, value):
127-
self.data.setdefault(key, set()).add(hbytes(value))
126+
self.data.setdefault(key, set()).add(bytes(value))
128127

129128
def delete(self, key, value):
130-
self.data.get(key, set()).discard(hbytes(value))
129+
self.data.get(key, set()).discard(bytes(value))
131130

132131
def close(self):
133132
pass
@@ -167,7 +166,7 @@ def fetch(self, key):
167166
for path in os.listdir(kp):
168167
try:
169168
with open(os.path.join(kp, path), "rb") as i:
170-
yield hbytes(i.read())
169+
yield i.read()
171170
except OSError:
172171
pass
173172

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,14 @@
3434
Lark, unless someone volunteers to either fund or do the maintainence.
3535
"""
3636

37+
from inspect import getfullargspec
3738

3839
import attr
3940
import lark
4041
from lark.grammar import NonTerminal, Terminal
4142

4243
import hypothesis.strategies._internal.core as st
4344
from hypothesis.errors import InvalidArgument
44-
from hypothesis.internal.compat import getfullargspec, string_types
4545
from hypothesis.internal.conjecture.utils import calc_label_from_name
4646
from hypothesis.internal.validation import check_type
4747
from hypothesis.strategies._internal import SearchStrategy
@@ -188,7 +188,7 @@ def calc_has_reusable_values(self, recur):
188188

189189
def check_explicit(name):
190190
def inner(value):
191-
check_type(string_types, value, "value drawn from " + name)
191+
check_type(str, value, "value drawn from " + name)
192192
return value
193193

194194
return inner

0 commit comments

Comments
 (0)