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:
Serhiy Storchaka 2025-08-14 14:59:04 +03:00 committed by GitHub
parent 2a6888ea14
commit c47ffbf1a3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 31 additions and 38 deletions

View file

@ -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.')