Skip to content

Setting a int type value to message argument of warnings.warn() gets a wrong or abstract error message #125827

@hyperkai

Description

@hyperkai

Bug report

Bug description:

Setting the int type value 100 to message argument of warnings.warn() gets the 1st error message as shown below:

import warnings
#             ↓ ↓ ↓ ↓ ↓ ↓
warnings.warn(message=100) # Error

TypeError: expected string or bytes-like object, got 'int'

But, setting a bytes-like object to message argument of warnings.warn() gets the 2nd error message as shown below:

import warnings

warnings.warn(message=b"Hello World") # Error
warnings.warn(message=bytes(source="Hello World", encoding="utf-8")) # Error

print(type(b"Hello World"))  # <class 'bytes'>
print(type(bytes(source="Hello World", encoding="utf-8"))) # <class 'bytes'>

TypeError: cannot use a string pattern on a bytes-like object

So, I set a str type value using decode() to message argument of warnings.warn(), then it works as shown below:

import warnings

warnings.warn(message=b"Hello World".decode(encoding="utf-8")) # Works
warnings.warn(message=bytes(source="Hello World", encoding="utf-8").decode(encoding="utf-8")) # Works

print(type(b"Hello World".decode(encoding="utf-8"))) # <class 'str'>
print(type(bytes(source="Hello World", encoding="utf-8").decode(encoding="utf-8"))) # <class 'str'>

So, the 1st error message should be something like below:

TypeError: expected string, got 'int'

Or, It's better to say str which is more exact(precise) than string:

TypeError: expected str, got 'int'

CPython versions tested on:

3.12

Operating systems tested on:

Windows

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions