-
-
Notifications
You must be signed in to change notification settings - Fork 33.2k
gh-125893: Add type check for category argument in warnings.simplefilter and warnings.filterwarning #136305
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
base: main
Are you sure you want to change the base?
gh-125893: Add type check for category argument in warnings.simplefilter and warnings.filterwarning #136305
Changes from 4 commits
4edb4c7
022e4b3
de55c4f
e51c00e
e8e6b31
656ec39
00eb17c
2a28591
13c37e3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -250,6 +250,19 @@ def _formatwarnmsg(msg): | |
| msg.filename, msg.lineno, msg.line) | ||
| return _wm._formatwarnmsg_impl(msg) | ||
|
|
||
| def _valid_warning_category(category): | ||
| """ | ||
| Return True if category is a Warning subclass or tuple of such. | ||
| Always perform class checks; only perform tuple iteration in debug mode. | ||
| """ | ||
| if isinstance(category, type) and issubclass(category, Warning): | ||
| return True | ||
| if isinstance(category, tuple): | ||
| if __debug__: | ||
|
||
| return all(isinstance(c, type) and issubclass(c, Warning) | ||
| for c in category) | ||
| return True | ||
| return False | ||
|
|
||
| def filterwarnings(action, message="", category=Warning, module="", lineno=0, | ||
| append=False): | ||
|
|
@@ -267,8 +280,9 @@ def filterwarnings(action, message="", category=Warning, module="", lineno=0, | |
| raise ValueError(f"invalid action: {action!r}") | ||
| if not isinstance(message, str): | ||
| raise TypeError("message must be a string") | ||
| if not isinstance(category, type) or not issubclass(category, Warning): | ||
| raise TypeError("category must be a Warning subclass") | ||
| if not _valid_warning_category(category): | ||
| raise TypeError("category must be a Warning subclass, " | ||
| "not '{:s}'".format(type(category).__name__)) | ||
| if not isinstance(module, str): | ||
| raise TypeError("module must be a string") | ||
| if not isinstance(lineno, int): | ||
|
|
@@ -303,6 +317,9 @@ def simplefilter(action, category=Warning, lineno=0, append=False): | |
| """ | ||
| if action not in {"error", "ignore", "always", "all", "default", "module", "once"}: | ||
| raise ValueError(f"invalid action: {action!r}") | ||
| if not _valid_warning_category(category): | ||
| raise TypeError("category must be a Warning subclass, " | ||
| "not '{:s}'".format(type(category).__name__)) | ||
| if not isinstance(lineno, int): | ||
| raise TypeError("lineno must be an int") | ||
| if lineno < 0: | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -398,6 +398,33 @@ def test_argument_validation(self): | |
| with self.assertRaises(ValueError): | ||
| self.module.simplefilter('ignore', lineno=-1) | ||
|
|
||
| def test_invalid_category_types(self): | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We probably need to skip this if we're not in debug mode, so add something like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated accordingly |
||
| with self.assertRaises(TypeError): | ||
| self.module.filterwarnings("ignore", category="notawarning") | ||
| with self.assertRaises(TypeError): | ||
| self.module.filterwarnings("ignore", category=123) | ||
| with self.assertRaises(TypeError): | ||
| self.module.filterwarnings("ignore", category=17.02) | ||
| with self.assertRaises(TypeError): | ||
| self.module.filterwarnings("ignore", category=True) | ||
| with self.assertRaises(TypeError): | ||
| self.module.filterwarnings( | ||
| "ignore", category=(UserWarning, 17) | ||
| ) | ||
|
|
||
| with self.assertRaises(TypeError): | ||
| self.module.simplefilter("ignore", category="notawarning") | ||
| with self.assertRaises(TypeError): | ||
| self.module.simplefilter("ignore", category=123) | ||
| with self.assertRaises(TypeError): | ||
| self.module.filterwarnings("ignore", category=17.02) | ||
| with self.assertRaises(TypeError): | ||
| self.module.filterwarnings("ignore", category=True) | ||
| with self.assertRaises(TypeError): | ||
| self.module.simplefilter( | ||
| "ignore", category=(UserWarning, 'abc') | ||
| ) | ||
|
|
||
| def test_catchwarnings_with_simplefilter_ignore(self): | ||
| with self.module.catch_warnings(module=self.module): | ||
| self.module.resetwarnings() | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| Added validation for the ``category`` argument in | ||
| ``warnings.filterwarnings()`` and ``warnings.simplefilter()`` | ||
hasrat17 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
Uh oh!
There was an error while loading. Please reload this page.