mirror of
https://github.com/python/cpython.git
synced 2025-11-01 14:11:41 +00:00
gh-125854: Improve error messages for invalid category in the warnings module (GH-137750)
Include the type name if the category is a type, but not a Warning subclass, instead of just 'type'.
This commit is contained in:
parent
2a6888ea14
commit
c47ffbf1a3
4 changed files with 31 additions and 38 deletions
|
|
@ -449,9 +449,12 @@ def warn(message, category=None, stacklevel=1, source=None,
|
|||
# Check category argument
|
||||
if category is None:
|
||||
category = UserWarning
|
||||
if not (isinstance(category, type) and issubclass(category, Warning)):
|
||||
raise TypeError("category must be a Warning subclass, "
|
||||
"not '{:s}'".format(type(category).__name__))
|
||||
elif not isinstance(category, type):
|
||||
raise TypeError(f"category must be a Warning subclass, not "
|
||||
f"'{type(category).__name__}'")
|
||||
elif not issubclass(category, Warning):
|
||||
raise TypeError(f"category must be a Warning subclass, not "
|
||||
f"class '{category.__name__}'")
|
||||
if not isinstance(skip_file_prefixes, tuple):
|
||||
# The C version demands a tuple for implementation performance.
|
||||
raise TypeError('skip_file_prefixes must be a tuple of strs.')
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue