[Python-ideas] `raise as` to raise with current exception as cause

2020-02-07 Thread Ram Rachum
Hi, I'd like to suggest an idea, that builds on PEPs 3134 and 409. This idea came up when discussing a problem on the django-developers mailing list: https://groups.google.com/forum/?utm_medium=email&utm_source=footer#!msg/django-developers/ibEOt3A9c2M/EP4gbQyTFwAJ I'll first explain my idea, an

[Python-ideas] Re: `raise as` to raise with current exception as cause

2020-02-07 Thread Serhiy Storchaka
07.02.20 16:28, Ram Rachum пише: The idea is to add `raise as` syntax, that raises an exception while setting the currently caught exception to be the cause. It'll look like this:     try:         1/0     except ZeroDivisionError:         raise as ValueError('Whatever') What it does is a

[Python-ideas] Re: `raise as` to raise with current exception as cause

2020-02-07 Thread Anders Hovmöller
If the only difference is the text between the stack traces, why aren't you suggesting to change that text? > On 7 Feb 2020, at 15:30, Ram Rachum wrote: > >  > Hi, > > I'd like to suggest an idea, that builds on PEPs 3134 and 409. > > This idea came up when discussing a problem on the django

[Python-ideas] Re: `raise as` to raise with current exception as cause

2020-02-07 Thread Ethan Furman
On 02/07/2020 07:33 AM, Serhiy Storchaka wrote: 07.02.20 16:28, Ram Rachum пише: The idea is to add `raise as` syntax, that raises an exception while setting the currently caught exception to be the cause. It'll look like this:      try:          1/0      except ZeroDivisionError:          rai

[Python-ideas] Re: `raise as` to raise with current exception as cause

2020-02-07 Thread Ethan Furman
On 02/07/2020 07:44 AM, Anders Hovmöller wrote: If the only difference is the text between the stack traces, why aren't you suggesting to change that text? Because the text is important in what it implies. See my other message for details. -- ~Ethan~ Aside: please trim the parts of your

[Python-ideas] Traits

2020-02-07 Thread Soni L.
I'd like to see traits some day, with a syntax similar to this one: trait Trait:   def x(self):     raise NotImplementedError   def y(self):     raise NotImplementedError trait Anoher:   def x(self):     raise NotImplementedError   def y(self):     raise NotImplementedError def foo(Trait(x)):  

[Python-ideas] Re: Traits

2020-02-07 Thread Nick Timkovich
On Fri, Feb 7, 2020 at 10:11 AM Soni L. wrote: > I'd like to see traits some day, with a syntax similar to this one: > ... > if the trait isn't used in the function definition you get the raw > object, where name conflicts between traits (but not between traits and > inherent methods) result in a

[Python-ideas] Re: Traits

2020-02-07 Thread Soni L.
On 2020-02-07 1:33 p.m., Nick Timkovich wrote: On Fri, Feb 7, 2020 at 10:11 AM Soni L. > wrote: I'd like to see traits some day, with a syntax similar to this one: ... if the trait isn't used in the function definition you get the raw object, wher

[Python-ideas] Re: `raise as` to raise with current exception as cause

2020-02-07 Thread Anders Hovmöller
> On 7 Feb 2020, at 16:59, Ethan Furman wrote: > > On 02/07/2020 07:44 AM, Anders Hovmöller wrote: > >> If the only difference is the text between the stack traces, why aren't you >> suggesting to change that text? > > Because the text is important in what it implies. See my other message

[Python-ideas] Re: `raise as` to raise with current exception as cause

2020-02-07 Thread Ethan Furman
On 02/07/2020 09:06 AM, Anders Hovmöller wrote: On 7 Feb 2020, at 16:59, Ethan Furman wrote: On 02/07/2020 07:44 AM, Anders Hovmöller wrote: If the only difference is the text between the stack traces, why aren't you suggesting to change that text? Because the text is important in what

[Python-ideas] Re: Traits

2020-02-07 Thread Stéfane Fermigier
Best reference that I know of: "Traits: A Mechanism for Fine-grained Reuse" by: STEPHANE DUCASSE OSCAR NIERSTRASZ and NATHANAEL SCHARLI ROEL WUYTS ANDREW P. BLACK *Inheritance is well-known and accepted as a mechanism for reuse in object-oriented languages. Unfortunately, due to the coarse granu

[Python-ideas] Re: `raise as` to raise with current exception as cause

2020-02-07 Thread Andrew Barnert via Python-ideas
> On Feb 7, 2020, at 06:32, Ram Rachum wrote: > It's possible that introducing the simpler `raise as` would increase adoption > and make users pay attention to the message between exception tracebacks. From the Django thread that you linked, when you asked whether they’d use it, the reply was:

[Python-ideas] Re: `raise as` to raise with current exception as cause

2020-02-07 Thread Shai Berger
Hi, In the Django thread, I suggested that an implicit "raise from" should be the behavior whenever an exception is raised directly in exception-handling code (that is, within an except: or finally: clause). Ram claimed there were problems with that, but gave no details; I would be happy to know w

[Python-ideas] Re: Traits

2020-02-07 Thread Chris Angelico
On Sat, Feb 8, 2020 at 4:01 AM Soni L. wrote: > Hello Nick! > > Traits are an alternative to Multiple Inheritance. They solve the problem of > name conflicts by making them an ambiguity error and requiring you to > disambiguate (at call site). > Okay, that sounds like a good summary. I'm not su

[Python-ideas] Re: `raise as` to raise with current exception as cause

2020-02-07 Thread David Mertz
Even apart from the long time delay, it seems clear that the Django developers rejected 'raise from' for design reasons. That might be right or wrong as a decision, but it's a separate project from Python itself. Nothing in the issue even hints that they would have accepted it *if only* the spelli

[Python-ideas] Re: pickle.reduce and deconstruct

2020-02-07 Thread Daniel Spitz
I just want to say, this would be extremely powerful. Awhile ago I implemented a demo that wraps the pickle extension machinery to enable "object graph transformers": https://gist.github.com/spitz-dan-l/3375a61fac6fe150574fac791567af4f (demo usage starts at line 307). Effectively it lets you match

[Python-ideas] Re: `raise as` to raise with current exception as cause

2020-02-07 Thread Andrew Barnert via Python-ideas
> On Feb 7, 2020, at 07:47, Anders Hovmöller wrote: > If the only difference is the text between the stack traces, why aren't you > suggesting to change that text? I think in the case where the exception handler code has an error in handling the exception (e.g., you try to log to a closed file)

[Python-ideas] Re: `raise as` to raise with current exception as cause

2020-02-07 Thread Brett Cannon
On Fri, Feb 7, 2020 at 7:56 AM Ethan Furman wrote: > On 02/07/2020 07:33 AM, Serhiy Storchaka wrote: > > 07.02.20 16:28, Ram Rachum пише: > >> The idea is to add `raise as` syntax, that raises an exception while > setting the currently caught exception to be the cause. It'll look like > this: > >

[Python-ideas] Re: `raise as` to raise with current exception as cause

2020-02-07 Thread Chris Angelico
On Sat, Feb 8, 2020 at 4:51 AM Shai Berger wrote: > To summarize, I am suggesting that > > except ExceptionType: > raise as OtherException(...) > > Have, more-or-less, the semantics of Python 2's: > > except ExceptionType: > traceback = sys.exc_info(

[Python-ideas] Pickle to/from filename or path

2020-02-07 Thread Todd
Currently with pickle you have to "open" the file separately from the pickle operation, generally using a "with" clause, then provide the file object to one of the pickle.dump or pickle.load. This adds quite a bit of boilerplate for simply saving a file I think it would be nice if pickle.dump and

[Python-ideas] Re: Pickle to/from filename or path

2020-02-07 Thread Antoine Pitrou
On Fri, 7 Feb 2020 13:03:52 -0500 Todd wrote: > Currently with pickle you have to "open" the file separately from the > pickle operation, generally using a "with" clause, then provide the file > object to one of the pickle.dump or pickle.load. This adds quite a bit of > boilerplate for simply sav

[Python-ideas] Re: pickle.reduce and deconstruct

2020-02-07 Thread Antoine Pitrou
I hadn't seen the original message (perhaps it fell through the GMane migration). I think this would indeed be a worthwhile addition to pickle. Is it possible to continue this discussion on python-dev? I don't think this is a PEP-level topic. Regards Antoine. On Fri, 7 Feb 2020 12:48:48 -0

[Python-ideas] Re: pickle.reduce and deconstruct

2020-02-07 Thread Andrew Barnert via Python-ideas
> On Feb 7, 2020, at 09:49, Daniel Spitz wrote: > > But, it uses pickle under the hood, because pickle is what knows how to > traverse object graphs with cycles, and pickle is what knows how to use the > extensible __reduce__ machinery. And this makes the entire demo silly and > really slow. (

[Python-ideas] Re: Traits

2020-02-07 Thread Mark Dickinson
Stéfane Fermigier wrote: > Note that there are several packages already in PyPI: > > https://pypi.org/project/traits/ > https://pypi.org/project/strait/ That first package is unrelated, I'm afraid: completely different meaning of the word "trait". In that case, a "trait" is an observable, typed

[Python-ideas] Re: Pickle to/from filename or path

2020-02-07 Thread Andrew Barnert via Python-ideas
> On Feb 7, 2020, at 10:21, Todd wrote: > > I think it would be nice if pickle.dump and pickle.load would also accept a > filename or path. Pickle intentionally has the same API as marshal, and so do a number of other modules—not just in the stdlib, but popular third party modules. So, I don’t

[Python-ideas] Re: Pickle to/from filename or path

2020-02-07 Thread Serhiy Storchaka
07.02.20 20:41, Antoine Pitrou пише: What you call "quite a bit of boilerplate" is just an additional line of code. If you are frequently being bothered by this (I'm curious what the context is?), it's easy to add the desired helper function to your own library. In general, I don't think adding

[Python-ideas] Re: Traits

2020-02-07 Thread Calvin Spealman
On Fri, Feb 7, 2020 at 12:02 PM Soni L. wrote: > > > > On 2020-02-07 1:33 p.m., Nick Timkovich wrote: > > On Fri, Feb 7, 2020 at 10:11 AM Soni L. wrote: >> >> I'd like to see traits some day, with a syntax similar to this one: >> ... >> if the trait isn't used in the function definition you get t

[Python-ideas] Re: `raise as` to raise with current exception as cause

2020-02-07 Thread Ram Rachum
On Fri, Feb 7, 2020 at 7:47 PM Shai Berger wrote: > Hi, > > In the Django thread, I suggested that an implicit "raise from" should > be the behavior whenever an exception is raised directly in > exception-handling code (that is, within an except: or finally: > clause). Ram claimed there were prob

[Python-ideas] Re: `raise as` to raise with current exception as cause

2020-02-07 Thread Ram Rachum
I would like to raise a sort-of ultimatum to everyone in this thread. As far as I know, the `raise foo from bar` syntax, and the distinction between the two exception-chaining messages, didn't really catch on. I know about them, like them and use them, but most Python developers and open-source pa

[Python-ideas] Re: `raise as` to raise with current exception as cause

2020-02-07 Thread Soni L.
On 2020-02-07 5:38 p.m., Ram Rachum wrote: I would like to raise a sort-of ultimatum to everyone in this thread. As far as I know, the `raise foo from bar` syntax, and the distinction between the two exception-chaining messages, didn't really catch on. I know about them, like them and use th

[Python-ideas] Re: `raise as` to raise with current exception as cause

2020-02-07 Thread Ram Rachum
Hi Soni, I think that your suggestion is identical to Shai's. It would be good, but unfortunately it has the 3 problems I raised in my email from an hour ago. On Fri, Feb 7, 2020 at 11:00 PM Soni L. wrote: > > > On 2020-02-07 5:38 p.m., Ram Rachum wrote: > > I would like to raise a sort-of ulti

[Python-ideas] Re: `raise as` to raise with current exception as cause

2020-02-07 Thread Soni L.
On 2020-02-07 6:05 p.m., Ram Rachum wrote: Hi Soni, I think that your suggestion is identical to Shai's. It would be good, but unfortunately it has the 3 problems I raised in my email from an hour ago. of which 2 and 3 are personal opinions. (for 2: you can just use a function. I'd conside

[Python-ideas] Re: Traits

2020-02-07 Thread Greg Ewing
On 8/02/20 5:59 am, Soni L. wrote: Traits are an alternative to Multiple Inheritance. They solve the problem of name conflicts by making them an ambiguity error and requiring you to disambiguate (at call site). This sounds like something that would work better in a statically typed language, w

[Python-ideas] Re: Traits

2020-02-07 Thread Ethan Furman
On 02/07/2020 01:40 PM, Greg Ewing wrote: On 8/02/20 5:59 am, Soni L. wrote: Traits are an alternative to Multiple Inheritance. They solve the problem of name conflicts by making them an ambiguity error and requiring you to disambiguate (at call site). This sounds like something that would

[Python-ideas] Re: `raise as` to raise with current exception as cause

2020-02-07 Thread Andrew Barnert via Python-ideas
On Feb 7, 2020, at 12:43, Ram Rachum wrote: > > I would like to raise a sort-of ultimatum to everyone in this thread. Is that a raise from Django or a raise as? :) > As far as I know, the `raise foo from bar` syntax, and the distinction > between the two exception-chaining messages, didn't rea

[Python-ideas] Re: `raise as` to raise with current exception as cause

2020-02-07 Thread Greg Ewing
My reaction to this whole discussion is to question whether it's worth having two different forms of exception chaining in the first place. It seems to be a subtle distinction that most programmers either aren't aware of or can't be bothered making when they write a 'raise' statement. I'm not con

[Python-ideas] Re: `raise as` to raise with current exception as cause

2020-02-07 Thread Steven D'Aprano
On Sat, Feb 08, 2020 at 11:29:00AM +1300, Greg Ewing wrote: > So my proposal would be to merge the two kinds of chaining into > one, and use wording in the traceback that is neutral as to the > relationship between the two exceptions. -1 There's no harm in the current wording. If very few people

[Python-ideas] Re: `raise as` to raise with current exception as cause

2020-02-07 Thread Steven D'Aprano
On Fri, Feb 07, 2020 at 04:28:53PM +0200, Ram Rachum wrote: > The idea is to add `raise as` syntax, that raises an exception while > setting the currently caught exception to be the cause. It'll look like > this: > > try: > 1/0 > except ZeroDivisionError: > raise as ValueE

[Python-ideas] Re: `raise as` to raise with current exception as cause

2020-02-07 Thread Steven D'Aprano
On Fri, Feb 07, 2020 at 10:38:42PM +0200, Ram Rachum wrote: > As far as I know, the `raise foo from bar` syntax, and the distinction > between the two exception-chaining messages, didn't really catch on. I know > about them, like them and use them, but most Python developers and > open-source pack

[Python-ideas] Re: `raise as` to raise with current exception as cause

2020-02-07 Thread Steven D'Aprano
On Fri, Feb 07, 2020 at 09:57:18AM -0800, Andrew Barnert via Python-ideas wrote: > However, Shai Berger had an interesting idea in that Django thread: > treat a `raise` directly under an `except` block special. For example: > > except ZeroDivisionError: > raise ValueError('whatever')

[Python-ideas] Re: Traits

2020-02-07 Thread Steven D'Aprano
On Fri, Feb 07, 2020 at 10:33:00AM -0600, Nick Timkovich wrote: > I assume traits are a feature of another language, but not being familiar > with it can you illustrate its need a bit better? Can you give an example > in current Python, and how it could be made more clear with the notional > trait

[Python-ideas] Re: Pickle to/from filename or path

2020-02-07 Thread Christopher Barker
Frankly, I’ve often wanted this for other things that share a similar API, JSON comes to mind. Loading from/saving to a file is a pretty common thing— nice to make it easy. And JSON at least has *s versions for strings, so no confusion there. But it’s been this way a LONG time, and with a gener

[Python-ideas] Re: Perhaps allow leading zeroes in integer literals

2020-02-07 Thread Richard Damon
On 2/6/20 7:03 AM, Jonathan Fine wrote: SUMMARY = It was once a good idea, for Python 3 to forbid leading zeroes in integer literals. Since then circumstances have changed. Perhaps it's now, or soon will be, a good time to permit this. A SURPRISE == I was surprised by: >>> 0

[Python-ideas] Re: `raise as` to raise with current exception as cause

2020-02-07 Thread Andrew Barnert via Python-ideas
> On Feb 7, 2020, at 16:11, Steven D'Aprano wrote: > > Shai Berger wants to set it implicily, based on where the raise is. If > it is "directly" under the except line, implicitly set the cause. > > It seems like a truly bad idea to change the semantics of the exception > depending on which of

[Python-ideas] Re: `raise as` to raise with current exception as cause

2020-02-07 Thread Shai Berger
On Fri, 7 Feb 2020 20:08:35 -0800 Andrew Barnert wrote: > > On Feb 7, 2020, at 16:11, Steven D'Aprano > > wrote: > > > > Shai Berger wants to set it implicily, based on where the raise is. > > If it is "directly" under the except line, implicitly set the cause. > > > > I interpreted “directly