Visitar URL original
bpo-16845: add category type check to warnings.simplefilter by Bonifacio2 · Pull Request #26696 · python/cpython · GitHub
The Wayback Machine - https://web.archive.org/web/20251017021351/https://github.com/python/cpython/pull/26696
Skip to content

Conversation

Bonifacio2
Copy link
Contributor

@Bonifacio2 Bonifacio2 commented Jun 12, 2021 •

Co-authored-by: Vajrasky Kok

https://bugs.python.org/issue16845

Co-authored-by: Vajrasky Kok
"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
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
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
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

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
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