Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Lib/test/test_warnings/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,13 @@ def test_filterwarnings_duplicate_filters(self):
"the beginning of list"
)

def test_simplefilter_invalid_category(self):
with original_warnings.catch_warnings(record=True, module=self.module):
invalid_category = 'spam'
with self.assertRaises(AssertionError) as cm:
self.module.simplefilter("once", invalid_category)
self.assertEqual(str(cm.exception), "category must be a class")

def test_simplefilter_duplicate_filters(self):
with original_warnings.catch_warnings(module=self.module):
self.module.resetwarnings()
Expand Down
1 change: 1 addition & 0 deletions Lib/warnings.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ def simplefilter(action, category=Warning, lineno=0, append=False):
"once"), "invalid action: %r" % (action,)
assert isinstance(lineno, int) and lineno >= 0, \
"lineno must be an int >= 0"
assert isinstance(category, type), "category must be a class"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer the implementation from the patch by @berkerpeksag:

 if not (isinstance(category, type) and issubclass(category, Warning)):
        raise TypeError('category must be a Warning subclass, '
                        'not {!r}'.format(category))

You can emulate something close.

You can emulate some parts of the test from the same patch:

+    def test_simplefilter_invalid_category(self):
+        class MyWarningClass(Warning):
+            pass
+
+        class NonWarningSubclass:
+            pass
+
+        msg_regex = 'category must be a Warning subclass, not (.*)'
+
+        with self.assertRaisesRegex(TypeError, msg_regex):
+            self.module.simplefilter('always', '')
+
+        with self.assertRaisesRegex(TypeError, msg_regex):
+            self.module.simplefilter('always', NonWarningSubclass)
+
+        with self.assertRaisesRegex(TypeError, msg_regex):
+            self.module.simplefilter('always', MyWarningClass())

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the suggestion. I'll take a look at this.

I also have to get some context on the failing tests. Not sure why they are failing.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Taking a look at this now. I might update this PR soon.

_add_filter(action, None, category, None, lineno, append=append)

def _add_filter(*item, append):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add category type check to warnings.simplefilter.