Skip to content

bpo-16845: add category type check to warnings.simplefilter#26696

Closed
Bonifacio2 wants to merge 4 commits into
python:mainfrom
Bonifacio2:simplefilter-validation
Closed

bpo-16845: add category type check to warnings.simplefilter#26696
Bonifacio2 wants to merge 4 commits into
python:mainfrom
Bonifacio2:simplefilter-validation

Conversation

@Bonifacio2
Copy link
Copy Markdown
Contributor

@Bonifacio2 Bonifacio2 commented Jun 12, 2021

Co-authored-by: Vajrasky Kok

https://bugs.python.org/issue16845

Co-authored-by: Vajrasky Kok
Comment thread Lib/warnings.py
"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.

@github-actions
Copy link
Copy Markdown

This PR is stale because it has been open for 30 days with no activity.

@github-actions github-actions Bot added the stale Stale PR or inactive for long period of time. label Jul 31, 2021
@github-actions github-actions Bot removed the stale Stale PR or inactive for long period of time. label Aug 9, 2022
@Bonifacio2
Copy link
Copy Markdown
Contributor Author

This PR turned obsolete given the following PR: #61049

@Bonifacio2 Bonifacio2 closed this Mar 24, 2023
@Bonifacio2 Bonifacio2 deleted the simplefilter-validation branch March 24, 2023 13:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants