Pedro added the comment:

Paul,
There are a number of scenarios in which you need to check a value and not just 
a type. For instance, I have a tool with several command-line arguments that 
need to be *nonnegative* integers, and a call to int() would not catch that. 
Many other utilities that ask for an argument of "how many times do you want to 
do X?" only make sense if you provide a nonnegative number.

Entering dates is another one. There is no callable in the datetime module or 
built-in that takes only one argument that can be used to validate a date. And 
if you write your own, you may need to make sure the date is within a specified 
range: only in the future, only in the past, no more/fewer than X days away 
from today, etc. My tool receives a date to execute a database query in the 
form of "select * from table where created_date >= argument_date." I can 
perform the validation further into the script, but performing as much 
validation up front when the arguments are parsed allows you to fail more 
quickly in the case of invalid input.

I can understand that ArgumentTypeError is available to be used when there is a 
problem with the arguments' type. But disallowing the use of custom messages in 
TypeError and ValueError seems like an arbitrary restriction with no pros and 
at least some cons. I assume that this was not an arbitrary decision and there 
really was a reason for it, but I can't see what that reason is.

----------

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue30220>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to