Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a65a3d4

Browse files
authored
bpo-39912: Raise appropriate exceptions in filterwarnings() and simplefilter() (pythonGH-18878)
1 parent 847e4fe commit a65a3d4

File tree

3 files changed

+43
-12
lines changed

3 files changed

+43
-12
lines changed

Lib/test/test_warnings/__init__.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,28 @@ def test_append_duplicate(self):
375375
"appended duplicate changed order of filters"
376376
)
377377

378+
def test_argument_validation(self):
379+
with self.assertRaises(ValueError):
380+
self.module.filterwarnings(action='foo')
381+
with self.assertRaises(TypeError):
382+
self.module.filterwarnings('ignore', message=0)
383+
with self.assertRaises(TypeError):
384+
self.module.filterwarnings('ignore', category=0)
385+
with self.assertRaises(TypeError):
386+
self.module.filterwarnings('ignore', category=int)
387+
with self.assertRaises(TypeError):
388+
self.module.filterwarnings('ignore', module=0)
389+
with self.assertRaises(TypeError):
390+
self.module.filterwarnings('ignore', lineno=int)
391+
with self.assertRaises(ValueError):
392+
self.module.filterwarnings('ignore', lineno=-1)
393+
with self.assertRaises(ValueError):
394+
self.module.simplefilter(action='foo')
395+
with self.assertRaises(TypeError):
396+
self.module.simplefilter('ignore', lineno=int)
397+
with self.assertRaises(ValueError):
398+
self.module.simplefilter('ignore', lineno=-1)
399+
378400
def test_catchwarnings_with_simplefilter_ignore(self):
379401
with original_warnings.catch_warnings(module=self.module):
380402
self.module.resetwarnings()

Lib/warnings.py

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -139,14 +139,18 @@ def filterwarnings(action, message="", category=Warning, module="", lineno=0,
139139
'lineno' -- an integer line number, 0 matches all warnings
140140
'append' -- if true, append to the list of filters
141141
"""
142-
assert action in ("error", "ignore", "always", "default", "module",
143-
"once"), "invalid action: %r" % (action,)
144-
assert isinstance(message, str), "message must be a string"
145-
assert isinstance(category, type), "category must be a class"
146-
assert issubclass(category, Warning), "category must be a Warning subclass"
147-
assert isinstance(module, str), "module must be a string"
148-
assert isinstance(lineno, int) and lineno >= 0, \
149-
"lineno must be an int >= 0"
142+
if action not in {"error", "ignore", "always", "default", "module", "once"}:
143+
raise ValueError(f"invalid action: {action!r}")
144+
if not isinstance(message, str):
145+
raise TypeError("message must be a string")
146+
if not isinstance(category, type) or not issubclass(category, Warning):
147+
raise TypeError("category must be a Warning subclass")
148+
if not isinstance(module, str):
149+
raise TypeError("module must be a string")
150+
if not isinstance(lineno, int):
151+
raise TypeError("lineno must be an int")
152+
if lineno < 0:
153+
raise ValueError("lineno must be an int >= 0")
150154

151155
if message or module:
152156
import re
@@ -172,10 +176,12 @@ def simplefilter(action, category=Warning, lineno=0, append=False):
172176
'lineno' -- an integer line number, 0 matches all warnings
173177
'append' -- if true, append to the list of filters
174178
"""
175-
assert action in ("error", "ignore", "always", "default", "module",
176-
"once"), "invalid action: %r" % (action,)
177-
assert isinstance(lineno, int) and lineno >= 0, \
178-
"lineno must be an int >= 0"
179+
if action not in {"error", "ignore", "always", "default", "module", "once"}:
180+
raise ValueError(f"invalid action: {action!r}")
181+
if not isinstance(lineno, int):
182+
raise TypeError("lineno must be an int")
183+
if lineno < 0:
184+
raise ValueError("lineno must be an int >= 0")
179185
_add_filter(action, None, category, None, lineno, append=append)
180186

181187
def _add_filter(*item, append):
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
:func:`warnings.filterwarnings()` and :func:`warnings.simplefilter()` now raise
2+
appropriate exceptions instead of ``AssertionError``. Patch contributed by
3+
Rémi Lapeyre.

0 commit comments

Comments
 (0)