On Mar 3, 2020, at 23:11, Steven D'Aprano <[email protected]> wrote: > > But for stability reasons (what if the error messages are localised, or > change in the future?), we could give the exception a "reason" > attribute, with a documented stable ID, and make that the official way > to programmatically distinguish between the different kinds of parameter > errors: > > except ParameterError as err: > if err.reason == 1: > # too few arguments
I liked everything up to using magic numbers like this. Nobody will remember whether it’s 1 for too many arguments or too few; they’ll have to check the docs every time it’s been more than 20 minutes since they wrote the last handler. If you need to programmatically distinguish these, and you can’t use subclasses to do it, at least make it something like a named class attribute, so you can check if it’s TooFewArguments instead of 1. (That still isn’t as nice as an enum, but presumably 95% of the time when you display the reason you’ll also be displaying the error message, so that’s fine.) The other question: what exactly do you propose to change? I don’t think you can do this on the caller side (the ceval bytecode handler and the PyCall functions). But if you put it on the callee side, how do you handle everything? Presumably you want to handle C functions as well as Python functions; can you change the PyArg parsing functions? (I assume argclinic uses those?) That still leaves functions that parse *args and **kw and raise TypeError manually, whether in Python or in C, but I suppose you can say that in that case it really isn’t a parameter error (the signature really is *args, **kw so everything matches). What about inspect.Signature.bind? _______________________________________________ Python-ideas mailing list -- [email protected] To unsubscribe send an email to [email protected] https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/[email protected]/message/5MICTLXMFJSZLHV5NIJXFJJE3PQX4URK/ Code of Conduct: http://python.org/psf/codeofconduct/
