-
-
Notifications
You must be signed in to change notification settings - Fork 33.7k
Closed as not planned
Closed as not planned
Copy link
Labels
type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
Description
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) # ErrorTypeError: 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
Labels
type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error