Skip to content

Commit fc30885

Browse files
authored
Merge pull request #3104 from HypothesisWorks/create-pull-request/patch
2 parents 1910770 + c908cc2 commit fc30885

27 files changed

+75
-69
lines changed

.flake8

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,6 @@ ignore =
1515
B008,B011,
1616
# flake8-bandit security warnings we disagree with or don't mind
1717
S101,S102,S105,S110,S307,S311,S404,S6
18+
select =
19+
# enable checks for self-or-cls param name, use of raise-from
20+
B902,B904

hypothesis-python/RELEASE.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
RELEASE_TYPE: patch
2+
3+
This patch fixes some new linter warnings such as :pypi:`flake8-bugbear`'s
4+
``B904`` for explicit exception chaining, so tracebacks might be a bit nicer.

hypothesis-python/src/hypothesis/_settings.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,11 @@ def __doc__(self):
9292

9393

9494
class settingsMeta(type):
95-
def __init__(self, *args, **kwargs):
95+
def __init__(cls, *args, **kwargs):
9696
super().__init__(*args, **kwargs)
9797

9898
@property
99-
def default(self):
99+
def default(cls):
100100
v = default_variable.value
101101
if v is not None:
102102
return v
@@ -105,10 +105,10 @@ def default(self):
105105
assert default_variable.value is not None
106106
return default_variable.value
107107

108-
def _assign_default_internal(self, value):
108+
def _assign_default_internal(cls, value):
109109
default_variable.value = value
110110

111-
def __setattr__(self, name, value):
111+
def __setattr__(cls, name, value):
112112
if name == "default":
113113
raise AttributeError(
114114
"Cannot assign to the property settings.default - "
@@ -121,7 +121,7 @@ def __setattr__(self, name, value):
121121
"settings with settings.load_profile, or use @settings(...) "
122122
"to decorate your test instead."
123123
)
124-
return type.__setattr__(self, name, value)
124+
return type.__setattr__(cls, name, value)
125125

126126

127127
class settings(metaclass=settingsMeta):

hypothesis-python/src/hypothesis/core.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -206,15 +206,17 @@ def decode_failure(blob):
206206
try:
207207
buffer = base64.b64decode(blob)
208208
except Exception:
209-
raise InvalidArgument(f"Invalid base64 encoded string: {blob!r}")
209+
raise InvalidArgument(f"Invalid base64 encoded string: {blob!r}") from None
210210
prefix = buffer[:1]
211211
if prefix == b"\0":
212212
return buffer[1:]
213213
elif prefix == b"\1":
214214
try:
215215
return zlib.decompress(buffer[1:])
216-
except zlib.error:
217-
raise InvalidArgument(f"Invalid zlib compression for blob {blob!r}")
216+
except zlib.error as err:
217+
raise InvalidArgument(
218+
f"Invalid zlib compression for blob {blob!r}"
219+
) from err
218220
else:
219221
raise InvalidArgument(
220222
f"Could not decode blob {blob!r}: Invalid start byte {prefix!r}"
@@ -724,7 +726,7 @@ def _execute_once_for_engine(self, data):
724726
# This can happen if an error occurred in a finally
725727
# block somewhere, suppressing our original StopTest.
726728
# We raise a new one here to resume normal operation.
727-
raise StopTest(data.testcounter)
729+
raise StopTest(data.testcounter) from e
728730
else:
729731
# The test failed by raising an exception, so we inform the
730732
# engine that this test run was interesting. This is the normal
@@ -1107,13 +1109,13 @@ def wrapped_test(*arguments, **kwargs):
11071109
"The shape of the test data has changed in some way "
11081110
"from where this blob was defined. Are you sure "
11091111
"you're running the same test?"
1110-
)
1112+
) from None
11111113
except UnsatisfiedAssumption:
11121114
raise DidNotReproduce(
11131115
"The test data failed to satisfy an assumption in the "
11141116
"test. Have you added it since this blob was "
11151117
"generated?"
1116-
)
1118+
) from None
11171119

11181120
# There was no @reproduce_failure, so start by running any explicit
11191121
# examples from @example decorators.

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,21 +96,21 @@ def obj_name(s: str) -> object:
9696
modulename, funcname = s.rsplit(".", 1)
9797
try:
9898
module = importlib.import_module(modulename)
99-
except ImportError:
99+
except ImportError as err:
100100
raise click.UsageError(
101101
f"Failed to import the {modulename} module for introspection. "
102102
"Check spelling and your Python import path, or use the Python API?"
103-
)
103+
) from err
104104
try:
105105
return getattr(module, funcname)
106-
except AttributeError:
106+
except AttributeError as err:
107107
public_names = [name for name in vars(module) if not name.startswith("_")]
108108
matches = get_close_matches(funcname, public_names)
109109
raise click.UsageError(
110110
f"Found the {modulename!r} module, but it doesn't have a "
111111
f"{funcname!r} attribute."
112112
+ (f" Closest matches: {matches!r}" if matches else "")
113-
)
113+
) from err
114114

115115
def _refactor(func, fname):
116116
try:

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ def draw_symbol(self, data, symbol, draw_state):
171171
"use of %%declare unless you pass `explicit`, a dict of "
172172
'names-to-strategies, such as `{%r: st.just("")}`'
173173
% (symbol.name, symbol.name)
174-
)
174+
) from None
175175
draw_state.result.append(data.draw(strategy))
176176
else:
177177
assert isinstance(symbol, NonTerminal)

hypothesis-python/src/hypothesis/extra/pandas/impl.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,11 @@ def convert_element(value):
9696
raise InvalidArgument(
9797
"Cannot convert %s=%r of type %s to dtype %s"
9898
% (name, value, type(value).__name__, dtype.str)
99-
)
99+
) from None
100100
except ValueError:
101101
raise InvalidArgument(
102102
f"Cannot convert {name}={value!r} to type {dtype.str}"
103-
)
103+
) from None
104104

105105
elements = elements.map(convert_element)
106106
assert elements is not None
@@ -518,7 +518,7 @@ def row():
518518
"Column names must be hashable, but columns[%d].name was "
519519
"%r of type %s, which cannot be hashed."
520520
% (i, c.name, type(c.name).__name__)
521-
)
521+
) from None
522522

523523
if c.name in column_names:
524524
raise InvalidArgument(f"duplicate definition of column name {c.name!r}")

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ def __setitem__(self, key, value):
9494
if self.max_size == self.__pinned_entry_count:
9595
raise ValueError(
9696
"Cannot increase size of cache where all keys have been pinned."
97-
)
97+
) from None
9898
entry = Entry(key, value, self.new_entry(key, value))
9999
if len(self.data) >= self.max_size:
100100
evicted = self.data[0]

hypothesis-python/src/hypothesis/internal/conjecture/data.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -744,10 +744,8 @@ def as_result(self):
744744

745745
class ConjectureData:
746746
@classmethod
747-
def for_buffer(self, buffer, observer=None):
748-
return ConjectureData(
749-
prefix=buffer, max_length=len(buffer), random=None, observer=observer
750-
)
747+
def for_buffer(cls, buffer, observer=None):
748+
return cls(len(buffer), buffer, random=None, observer=observer)
751749

752750
def __init__(self, max_length, prefix, random, observer=None):
753751
if observer is None:

hypothesis-python/src/hypothesis/internal/conjecture/datatree.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -298,8 +298,8 @@ def simulate_test_function(self, data):
298298
v = data.draw_bits(node.transition.bit_length)
299299
try:
300300
node = node.transition.children[v]
301-
except KeyError:
302-
raise PreviouslyUnseenBehaviour()
301+
except KeyError as err:
302+
raise PreviouslyUnseenBehaviour() from err
303303
else:
304304
assert isinstance(node.transition, Killed)
305305
data.observer.kill_branch()

0 commit comments

Comments
 (0)