-
Notifications
You must be signed in to change notification settings - Fork 90
Simplify deprecation machinery: don't cache previous messages, and use warnings rather than logging. #220
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Simplify deprecation machinery: don't cache previous messages, and use warnings rather than logging. #220
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
a2f1cdc
Simplify deprecation machinery: don't cache previous messages, and us…
mdickinson 6d0619e
Add an assertIssuesDeprecationWarning method to UnittestTools, and te…
mdickinson bc1e740
Comment and style fixes; fix copyright header in deprecated.py.
mdickinson c41c807
Rename: assertIssuesDeprecationWarning -> assertDeprecated.
mdickinson f2d79be
Test that 'assertDeprecated' fails when appropriate.
mdickinson 1213bb6
Add reference to related Python issue.
mdickinson f772eb5
Add tests for 'deprecated' decorator with functions and methods.
mdickinson 32c9c71
Flake8, style.
mdickinson 26188f9
Import unittest from unittest_tools so that we have the full range of…
mdickinson 5100da9
Add CHANGES.txt entry.
mdickinson File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| #------------------------------------------------------------------------------ | ||
| # Copyright (c) 2005-2014, Enthought, Inc. | ||
| # All rights reserved. | ||
| # | ||
| # This software is provided without warranty under the terms of the BSD | ||
| # license included in /LICENSE.txt and may be redistributed only | ||
| # under the conditions described in the aforementioned license. The license | ||
| # is also available online at http://www.enthought.com/licenses/BSD.txt | ||
| # Thanks for using Enthought open source! | ||
| #------------------------------------------------------------------------------ | ||
|
|
||
| from traits.testing.api import UnittestTools | ||
| from traits.testing.unittest_tools import unittest | ||
| from traits.util.api import deprecated | ||
|
|
||
|
|
||
| @deprecated("Addition is deprecated; use subtraction instead.") | ||
| def my_deprecated_addition(x, y): | ||
| return x + y | ||
|
|
||
|
|
||
| @deprecated("Broken code. Use something else.") | ||
| def my_bad_function(): | ||
| 1 / 0 | ||
|
|
||
|
|
||
| class ClassWithDeprecatedBits(object): | ||
| @deprecated('bits are deprecated; use bytes') | ||
| def bits(self): | ||
| return 42 | ||
|
|
||
| @deprecated('bytes are deprecated too. Use base 10.') | ||
| def bytes(self, required_arg, *args, **kwargs): | ||
| return required_arg, args, kwargs | ||
|
|
||
|
|
||
| class TestDeprecated(unittest.TestCase, UnittestTools): | ||
| def test_deprecated_function(self): | ||
| with self.assertDeprecated(): | ||
| result = my_deprecated_addition(42, 1729) | ||
| self.assertEqual(result, 1771) | ||
|
|
||
| def test_deprecated_exception_raising_function(self): | ||
| with self.assertRaises(ZeroDivisionError): | ||
| with self.assertDeprecated(): | ||
| my_bad_function() | ||
|
|
||
| def test_deprecated_method(self): | ||
| obj = ClassWithDeprecatedBits() | ||
| with self.assertDeprecated(): | ||
| result = obj.bits() | ||
| self.assertEqual(result, 42) | ||
|
|
||
| def test_deprecated_method_with_fancy_signature(self): | ||
| obj = ClassWithDeprecatedBits() | ||
| with self.assertDeprecated(): | ||
| result = obj.bytes(3, 27, 65, name='Boris', age=-3.2) | ||
| self.assertEqual( | ||
| result, (3, (27, 65), {'name': 'Boris', 'age': -3.2})) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are we breaking compatibility with Python >= 3.4 by using this hack?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No: it should be entirely harmless on Python >= 3.4: it clears out the
__warningregistry__dict, but on Python 3.4 the__warningregistry__entries are effectively invalidated (but not removed) every time the warnings filterlist is touched [1], so all we're doing is removing entries from the registry that wouldn't have been used anyway.The tests pass on Python 3.4, and we have Travis CI running on 3.4 too, so we should be pretty safe.
[1] There's a clever counter mechanism for this; see the Python issue for the details.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could add a
sys.versioncheck here, and not execute the hack for Python >= 3.4.Looking closely, it's not quite true to say that it's unnecessary on Python >= 3.4: it's unnecessary on Python >= 3.4.2 (released in October). It's still necessary on 3.4.0 or 3.4.1, so it would be better not to execute the hack for Python >= 3.5. And Python 3.5 doesn't even exist yet.
All in all, I think I'd rather just leave this as it is. :-)