Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document best practices for exceptions #59239

Open
bitdancer opened this issue Jun 8, 2012 · 11 comments
Open

Document best practices for exceptions #59239

bitdancer opened this issue Jun 8, 2012 · 11 comments
Labels
3.8 only security fixes 3.9 only security fixes 3.10 only security fixes docs Documentation in the Doc dir type-bug An unexpected behavior, bug, or error

Comments

@bitdancer
Copy link
Member

BPO 15034
Nosy @rhettinger, @terryjreedy, @abalkin, @ezio-melotti, @merwok, @bitdancer, @cjerdonek, @hynek, @vedgar, @iritkatriel

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = None
created_at = <Date 2012-06-08.01:22:03.888>
labels = ['type-bug', '3.8', '3.9', '3.10', 'docs']
title = 'Document best practices for exceptions'
updated_at = <Date 2020-11-11.07:31:11.433>
user = 'https://github.com/bitdancer'

bugs.python.org fields:

activity = <Date 2020-11-11.07:31:11.433>
actor = 'veky'
assignee = 'docs@python'
closed = False
closed_date = None
closer = None
components = ['Documentation']
creation = <Date 2012-06-08.01:22:03.888>
creator = 'r.david.murray'
dependencies = []
files = []
hgrepos = []
issue_num = 15034
keywords = []
message_count = 11.0
messages = ['162513', '162540', '162541', '164033', '164076', '164434', '171039', '171065', '183034', '380713', '380739']
nosy_count = 12.0
nosy_names = ['rhettinger', 'terry.reedy', 'belopolsky', 'ezio.melotti', 'eric.araujo', 'r.david.murray', 'chris.jerdonek', 'docs@python', 'tshepang', 'hynek', 'veky', 'iritkatriel']
pr_nums = []
priority = 'normal'
resolution = None
stage = 'needs patch'
status = 'open'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue15034'
versions = ['Python 3.8', 'Python 3.9', 'Python 3.10']

@bitdancer
Copy link
Member Author

And I wish I knew what those were.

@bitdancer bitdancer added the docs Documentation in the Doc dir label Jun 8, 2012
@bitdancer bitdancer added the type-bug An unexpected behavior, bug, or error label Jun 8, 2012
@merwok
Copy link
Member

merwok commented Jun 8, 2012

I don’t understand the request.

@bitdancer
Copy link
Member Author

The obvious example is that the tutorial makes no mention of calling 'super' in __init__. I'm also aware that there are issues of pickleability that arise if you do things one way, but do not arise if you do things another way. But I don't know the details, and I'd like to see the tutorial show an example of the *best* way to write a user defined exception so that they behave like the built in Python exceptions.

@abalkin abalkin changed the title tutorial should use best practices in user defined execeptions section tutorial should use best practices in user defined exceptions section Jun 8, 2012
@rhettinger
Copy link
Contributor

This seems more like a Stack Overflow question or fodder for a blog post.

When such best practices become well known and agreed upon, they can be added toa HOWTO document. The tutorial is problematic because beginners start there and sprinkling the nuances of super() throughout the tutorial will only get in the way of the main learning points for a given section.

@bitdancer
Copy link
Member Author

OK, let's move this, then. I asked the question because I'd like to know what the best practice is for exceptions in the stdlib. This is an area in which we have made quite a bit of progress recently (ie: the work done on exceptions for Python3, and PEP-3151), but I think there is still a lack of documentation (and possibly consensus?) on best practices for the stdlib.

@bitdancer bitdancer added docs Documentation in the Doc dir and removed docs Documentation in the Doc dir labels Jun 26, 2012
@bitdancer bitdancer changed the title tutorial should use best practices in user defined exceptions section Devguide should document best practices for stdlib exceptions Jun 26, 2012
@bitdancer bitdancer reopened this Jun 26, 2012
@terryjreedy
Copy link
Member

I agree with Raymond that this, especially super() is not tutorial material. I agree with David that we need something somewhere else. Just today a commit was pushed about IOException subclasses causing problems because they have an __init__ but don't call super(). (Something like that.) I had no idea about this issue.

The stdlib has things like "class ModException(Exception): pass". Is that okay because it does *not* have __init__ override?

@ezio-melotti
Copy link
Member

I'm not sure this belongs to the devguide.
Ideally the best way to do it should be documented in the docs.
This doesn't necessary mean it should be in the tutorial, but if the way documented in the tutorial is "wrong", I'd rather tell users "this is the right way, do like this and don't worry about the details right now (but follow this link if you want to know more)" than "use this wrongish way that is simple but will break as soon as you start doing something more complex".

@terryjreedy
Copy link
Member

I think the Library Manual's chapter 5 on exceptions, currently called Built-in Exceptions, would be a good place for a section on subclassing exceptions.

@ezio-melotti
Copy link
Member

I'm removing the "devguide" component and update the title accordingly.

@ezio-melotti ezio-melotti added docs Documentation in the Doc dir and removed docs Documentation in the Doc dir labels Feb 26, 2013
@ezio-melotti ezio-melotti changed the title Devguide should document best practices for stdlib exceptions Document best practices for exceptions Feb 26, 2013
@iritkatriel
Copy link
Member

There are examples in the tutorial where super().__init__ is not called, like here:

https://docs.python.org/3/tutorial/errors.html#user-defined-exceptions

@iritkatriel iritkatriel added 3.8 only security fixes 3.9 only security fixes 3.10 only security fixes labels Nov 10, 2020
@vedgar
Copy link
Mannequin

vedgar mannequin commented Nov 11, 2020

Terry: of course, if a method is not overridden, there's no need to call super()'s method, since the only thing it would do is call the same thing that would be called without overriding.

But of course, if Exception's __init__ does anything, then super() should be called from the overrridden method.

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.8 only security fixes 3.9 only security fixes 3.10 only security fixes docs Documentation in the Doc dir type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

6 participants