Incorrect error message when use pad with invalid input type
Hi there,
When I use Crypto.Util.Padding.pad with str input, it raises an error. This is an expected TypeError, but the error message is incorrect.
The example:
password = 'test string'
padded_data = Padding.pad(password, 128)
And the error message:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File ".../lib/python3.6/site-packages/Crypto/Util/Padding.py", line 64, in pad
return data_to_pad + padding
TypeError: must be str, not bytes
I think this message will confusing users because the correct input type should be bytes not str, but this message implies total different meaning.
Maybe a type check before line64 in Crypto/Util/Padding.py to handle incorrect input will be better?
I see your point but doing explicit type checking for every parameter decreases readability and make the code longer and more boring, so that is best done sparely and only when indeed the type of the parameter is not obvious.
Starting from this version, we added type stubs so that mypy or your IDE can indicate that there is a type mismatch even before execution. I think that's a better approach.
@Legrandin This issue actually bugged me for a while too... Until I found it here in closed issues. As @chenjr0719 says, the error msg is highly misleading... and I was only able to ascertain the return type by looking at the type stubs... still its not very helpful... that error message is annoying...
You're probably barking at the wrong tree here.
Seriously, this error message is generated from the Python interpreter, and should be addressed there for a better phrase. At that point, it implies a conversion to the left hand argument type, which conflicts with the 12th (and the 2nd to some degree) Zen of Python principle.
@chenjr0719 thanks, you really saved my ass by posting this. I was super confused by this error message.