[Python-ideas] Re: Exceptions with Message Templates

2019-08-12 Thread Kyle Lahnakoski
On 2019-08-08 11:52, Ryan Fox wrote: My proposal is a new exception class as the preferred base for user-defined exceptions: >>> class MyException(ExceptionTemplate): ...    message = 'Bad thing happened during {action} in {context}' >>> raise MyException(action=current_action,

[Python-ideas] Re: Exceptions with Message Templates

2019-08-09 Thread Andrew Barnert via Python-ideas
On Aug 9, 2019, at 12:00, Ryan Fox wrote: > > > If you’re wondering why this works, it’s because Error and InputError don’t > > override __new__. Which should make it obvious why a tutorial aimed at > > novices doesn’t get into the details, but that’s why Python has reference > > manuals

[Python-ideas] Re: Exceptions with Message Templates

2019-08-09 Thread Ryan Fox
> If you’re wondering why this works, it’s because Error and InputError don’t override __new__. Which should make it obvious why a tutorial aimed at novices doesn’t get into the details, but that’s why Python has reference manuals instead of just a tutorial. Between the links I've found, none of

[Python-ideas] Re: Exceptions with Message Templates

2019-08-09 Thread Sebastian Kreft
On Fri, Aug 9, 2019 at 12:55 PM Brett Cannon wrote: > > > On Thu, Aug 8, 2019 at 5:24 PM Sebastian Kreft wrote: > >> >> >> On Thu, Aug 8, 2019 at 7:09 PM Andrew Barnert via Python-ideas < >> python-ideas@python.org> wrote: >> >>> On Aug 8, 2019, at 15:01, Ryan Fox wrote: >>> >>> I don't see

[Python-ideas] Re: Exceptions with Message Templates

2019-08-09 Thread Brett Cannon
On Thu, Aug 8, 2019 at 11:38 AM Ryan Fox wrote: > Thanks for the comments. > > I'm not really sure what you mean with regards to backwards compatibility. > Would it suffice to have ExceptionTemplate.__init__ accept *args and pass > that into super().__init__? I see that BaseException does

[Python-ideas] Re: Exceptions with Message Templates

2019-08-09 Thread Brett Cannon
On Thu, Aug 8, 2019 at 5:24 PM Sebastian Kreft wrote: > > > On Thu, Aug 8, 2019 at 7:09 PM Andrew Barnert via Python-ideas < > python-ideas@python.org> wrote: > >> On Aug 8, 2019, at 15:01, Ryan Fox wrote: >> >> I don't see why you would want to access arguments by their position. >> >> >>

[Python-ideas] Re: Exceptions with Message Templates

2019-08-08 Thread Sebastian Kreft
On Thu, Aug 8, 2019 at 7:09 PM Andrew Barnert via Python-ideas < python-ideas@python.org> wrote: > On Aug 8, 2019, at 15:01, Ryan Fox wrote: > > I don't see why you would want to access arguments by their position. > > > Because that’s the way it’s worked since Python 1.x, and there’s tons of >

[Python-ideas] Re: Exceptions with Message Templates

2019-08-08 Thread Andrew Barnert via Python-ideas
On Aug 8, 2019, at 15:01, Ryan Fox wrote: > > I don't see why you would want to access arguments by their position. Because that’s the way it’s worked since Python 1.x, and there’s tons of existing code that expects it, including the default __str__ and __repr__ for exceptions and the code

[Python-ideas] Re: Exceptions with Message Templates

2019-08-08 Thread Ryan Fox
> Your magic template has apparently turned positional-or-keyword parameters into keyword-only. This means that every exception class that you’d normally construct with positional args today (which apparently includes even your example, since that’s what you did in version 1 and 2) now requires

[Python-ideas] Re: Exceptions with Message Templates

2019-08-08 Thread Andrew Barnert via Python-ideas
On Aug 8, 2019, at 08:52, Ryan Fox wrote: > > >>> class MyException(Exception): > ... def __init__(self, action, context): > ... super().__init__(f'Bad thing happened during {action} in > {context}') > >>> raise MyException(current_action, current_context) ... > Version 2 looks

[Python-ideas] Re: Exceptions with Message Templates

2019-08-08 Thread Ryan Fox
Thanks for the comments. I'm not really sure what you mean with regards to backwards compatibility. Would it suffice to have ExceptionTemplate.__init__ accept *args and pass that into super().__init__? I see that BaseException does something with args in __new__ and __init__, but I'm not very

[Python-ideas] Re: Exceptions with Message Templates

2019-08-08 Thread Brett Cannon
Three things. One, this isn't backwards-compatible as you are not passing any details down into Exception.__init__() to make sure that BaseException.args gets populated. Two, you can simplify your code by using str.format_map() instead of the string module. Three, I don't see enough overhead