Open hyperkai opened 3 days ago
Actually, the error message seems to stem from the re module, not the warnings module. So we need, at some point, to normalize the inputs, or just sanitize the message before it is being given (namely, use some isinstance(message, str)
).
EDIT: Actually, after some investigation, it appears that I cannot reproduce the issue again (neither on 3.12.6 nor on main):
>>> import warnings
>>> warnings.warn(5)
<python-input-1>:1: UserWarning: 5
warnings.warn(5)
@hyperkai Can you confirm the exact version of Python? as well as whether you have filters enabled? (namely, the exact state of the warnings module)
Related: https://github.com/python/cpython/issues/47889 which added the following test:
def test_warn_nonstandard_types(self):
# warn() should handle non-standard types without issue.
for ob in (Warning, None, 42):
with original_warnings.catch_warnings(record=True,
module=self.module) as w:
self.module.simplefilter("once")
self.module.warn(ob)
# Don't directly compare objects since
# ``Warning() != Warning()``.
self.assertEqual(str(w[-1].message), str(UserWarning(ob)))
Bug report
Bug description:
Setting the
int
type value100
tomessage
argument of warnings.warn() gets the 1st error message as shown below:But, setting a bytes-like object to
message
argument ofwarnings.warn()
gets the 2nd error message as shown below:So, I set a
str
type value using decode() tomessage
argument ofwarnings.warn()
, then it works as shown below:So, the 1st error message should be something like below:
Or, It's better to say
str
which is more exact(precise) thanstring
:CPython versions tested on:
3.12
Operating systems tested on:
Windows