[Python-ideas] Add InvalidStateError to the standard exception hierarchy

2022-09-01 Thread Steve Jorgensen
I frequently find that I want to raise an exception when the target of a call is not in an appropriate state to perform the requested operation. Rather than choosing between `Exception` or defining a custom exception, it would be nice if there were a built-in `InvalidStateError` exception that

[Python-ideas] Re: Add InvalidStateError to the standard exception hierarchy

2022-09-01 Thread Jean Abou Samra
Le 01/09/2022 à 23:40, Steve Jorgensen a écrit : I frequently find that I want to raise an exception when the target of a call is not in an appropriate state to perform the requested operation. Rather than choosing between `Exception` or defining a custom exception, it would be nice if there

[Python-ideas] Re: Add InvalidStateError to the standard exception hierarchy

2022-09-01 Thread Steve Jorgensen
Matthias Görgens wrote: > > If the target of the call isn't in an appropriate state, isn't that a > > bug in the constructor that it allows you to construct objects that are > > in an invalid state? > > You should fix the object so that it is never in an invalid state rather > > than blaming the

[Python-ideas] Re: Add InvalidStateError to the standard exception hierarchy

2022-09-01 Thread Steven D'Aprano
On Thu, Sep 01, 2022 at 09:40:05PM -, Steve Jorgensen wrote: > I frequently find that I want to raise an exception when the target of > a call is not in an appropriate state to perform the requested > operation. Rather than choosing between `Exception` or defining a > custom exception, it

[Python-ideas] Re: Add InvalidStateError to the standard exception hierarchy

2022-09-01 Thread Chris Angelico
On Fri, 2 Sept 2022 at 10:51, Steve Jorgensen wrote: > > Matthias Görgens wrote: > > > If the target of the call isn't in an appropriate state, isn't that a > > > bug in the constructor that it allows you to construct objects that are > > > in an invalid state? > > > You should fix the object so

[Python-ideas] Re: Add InvalidStateError to the standard exception hierarchy

2022-09-01 Thread David Mertz, Ph.D.
On Thu, Sep 1, 2022, 9:12 PM Chris Angelico > > It depends on context whether it makes sense to define a custom > exception, and I agree that I frequently should define a custom exception. > In that case though, it would still be nice to have an appropriate generic > exception for that to inherit

[Python-ideas] Re: Add InvalidStateError to the standard exception hierarchy

2022-09-01 Thread Matthias Görgens
> > If the target of the call isn't in an appropriate state, isn't that a > bug in the constructor that it allows you to construct objects that are > in an invalid state? > > You should fix the object so that it is never in an invalid state rather > than blaming the caller. > You can't really do

[Python-ideas] Re: Add InvalidStateError to the standard exception hierarchy

2022-09-01 Thread Steve Jorgensen
Matthias Görgens wrote: > > If the target of the call isn't in an appropriate state, isn't that a > > bug in the constructor that it allows you to construct objects that are > > in an invalid state? > > You should fix the object so that it is never in an invalid state rather > > than blaming the

[Python-ideas] Re: Add InvalidStateError to the standard exception hierarchy

2022-09-01 Thread Bruce Leban
On Thu, Sep 1, 2022 at 2:57 PM Jean Abou Samra wrote: > > How would > a "state error" differ from this more precisely? What value would this new > exception type add? Both ValueError and this proposed StateError are very > generic. > Some examples: * a stream-like object that has been closed

[Python-ideas] Re: Add InvalidStateError to the standard exception hierarchy

2022-09-01 Thread Steve Jorgensen
Paul Moore wrote: > What's wrong with defining a custom exception? It's literally one line: > `class InvalidStateError(Exception): pass`. Two lines if you want to put > the `pass` on its own line. > The built in exceptions are ones that are raised by the core interpreter. > Even the stdlib doesn't

[Python-ideas] Re: Add InvalidStateError to the standard exception hierarchy

2022-09-01 Thread Bruce Leban
Java calls this IllegalStateException so I would suggest IllegalStateError. Looking at other exceptions that Java has, it would also be nice to have UnsupportedOperationError (I have used NotImplementedError for this but that suggests it might someday be implemented). On the other hand, as you

[Python-ideas] Re: Add InvalidStateError to the standard exception hierarchy

2022-09-01 Thread Paul Moore
What's wrong with defining a custom exception? It's literally one line: `class InvalidStateError(Exception): pass`. Two lines if you want to put the `pass` on its own line. The built in exceptions are ones that are raised by the core interpreter. Even the stdlib doesn't get builtin exceptions,

[Python-ideas] Re: Add InvalidStateError to the standard exception hierarchy

2022-09-01 Thread Steve Jorgensen
Jean Abou Samra wrote: > Le 01/09/2022 à 23:40, Steve Jorgensen a écrit : > > I frequently find that I want to raise an exception when the target of a > > call is not in an appropriate state to perform the requested operation. > > Rather than choosing between `Exception` or defining a custom