Skip to content

Commit

Permalink
Merge pull request #1610 from Zac-HD/warning-stacklevel
Browse files Browse the repository at this point in the history
Fix deprecation warning stacklevel
  • Loading branch information
Zac-HD authored Sep 30, 2018
2 parents e8a4565 + 8343c64 commit 174f595
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
4 changes: 4 additions & 0 deletions hypothesis-python/RELEASE.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
RELEASE_TYPE: patch

This patch ensures that Hypothesis deprecation warnings display the code
that emitted them when you're not running in ``-Werror`` mode (:issue:`652`).
2 changes: 1 addition & 1 deletion hypothesis-python/src/hypothesis/_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -845,7 +845,7 @@ def note_deprecation(message, s=None):
verbosity = s.verbosity
warning = HypothesisDeprecationWarning(message)
if verbosity > Verbosity.quiet:
warnings.warn(warning, stacklevel=3)
warnings.warn(warning, stacklevel=2)


settings.register_profile('default', settings())
Expand Down
44 changes: 44 additions & 0 deletions hypothesis-python/tests/nocover/test_regressions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# coding=utf-8
#
# This file is part of Hypothesis, which may be found at
# https://github.com/HypothesisWorks/hypothesis-python
#
# Most of this work is copyright (C) 2013-2018 David R. MacIver
# ([email protected]), but it contains contributions by others. See
# CONTRIBUTING.rst for a full list of people who may hold copyright, and
# consult the git log if you need to determine who owns an individual
# contribution.
#
# This Source Code Form is subject to the terms of the Mozilla Public License,
# v. 2.0. If a copy of the MPL was not distributed with this file, You can
# obtain one at http://mozilla.org/MPL/2.0/.
#
# END HEADER

from __future__ import division, print_function, absolute_import

import warnings

from hypothesis.errors import HypothesisDeprecationWarning
from hypothesis._settings import note_deprecation
from hypothesis.strategies import integers, composite


def test_note_deprecation_blames_right_code_issue_652():
msg = 'this is an arbitrary deprecation warning message'

@composite
def deprecated_strategy(draw):
draw(integers())
note_deprecation(msg)

with warnings.catch_warnings(record=True) as log:
warnings.simplefilter('always')
deprecated_strategy().example()

assert len(log) == 1
record, = log
# We got the warning we expected, from the right file
assert isinstance(record.message, HypothesisDeprecationWarning)
assert record.message.args == (msg,)
assert record.filename == __file__

0 comments on commit 174f595

Please sign in to comment.