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

Do not define unneeded __str__ equal to __repr__ #80974

Closed
serhiy-storchaka opened this issue May 4, 2019 · 4 comments
Closed

Do not define unneeded __str__ equal to __repr__ #80974

serhiy-storchaka opened this issue May 4, 2019 · 4 comments
Labels
3.8 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement

Comments

@serhiy-storchaka
Copy link
Member

BPO 36793
Nosy @gpshead, @serhiy-storchaka, @MojoVampire
PRs
  • bpo-36793: Remove unneeded __str__ definitions. #13081
  • 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 = <Date 2019-08-31.19:37:22.464>
    created_at = <Date 2019-05-04.13:10:23.838>
    labels = ['interpreter-core', 'type-feature', '3.8']
    title = 'Do not define unneeded __str__ equal to __repr__'
    updated_at = <Date 2019-08-31.19:37:22.464>
    user = 'https://github.com/serhiy-storchaka'

    bugs.python.org fields:

    activity = <Date 2019-08-31.19:37:22.464>
    actor = 'serhiy.storchaka'
    assignee = 'none'
    closed = True
    closed_date = <Date 2019-08-31.19:37:22.464>
    closer = 'serhiy.storchaka'
    components = ['Interpreter Core']
    creation = <Date 2019-05-04.13:10:23.838>
    creator = 'serhiy.storchaka'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 36793
    keywords = ['patch']
    message_count = 4.0
    messages = ['341382', '341476', '341617', '348069']
    nosy_count = 3.0
    nosy_names = ['gregory.p.smith', 'serhiy.storchaka', 'josh.r']
    pr_nums = ['13081']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue36793'
    versions = ['Python 3.8']

    @serhiy-storchaka
    Copy link
    Member Author

    object.__str__() calls the __repr__() method. Therefore by default you do not need to define the __str__() method if it is the same as __repr__(): just define the __repr__() method. But there are few builtin classes which define both __repr__() and __str__() methods which are equal. In most cases this is a legacy of Python 2 where they where different.

    1. float and complex. In earlier Python 2 versions the repr of floating point number was longer (and more precise) that the str which was limited for readability. This was changed several times and in Python 3 the repr and the str are equal and contain as much digits as needed for the closest decimal approximation.

    2. int. In Python 2 the repr of long integer was different from the str: it contained the "L" suffix.

    3. bool. Since it is an int subclass with different repr, it needs to define __str__ to override int's __str__.

    4. subprocess.Handle and sre_constants._NamedIntConstant. As bool they need to override int's __str__.

    5. doctest.DocTestCase, doctest.DocFileCase. They need to override unittest.TestCase's __str__.

    6. http.client.IncompleteRead, xmlrpc.client.Error. They need to override BaseException's __str__ (don't know why they want to do this).

    7. asyncore.dispatcher, email.charset.Charset, logging.LogRecord, xmlrpc.client.MultiCall, xmlrpc.client.ServerProxy, decimal.Context (C implementation only), _pydecimal._WorkRep. There is no need to define __str__.

    In most of these cases the __str__ definition is redundant and can be removed. In cases 5 and 6 it is better to reset __str__ to object.__str__.

    The only failing tests in the Python testsuite is the json module test. The json module calls int.__str__ explicitly. It can be fixed by replacing it with int.__repr__.

    The user visible effect of these changes is that defining __repr__ in a subclass of these classes will affect the str() result (as for most of other classes). Although it is unlikely that you want to keep the str representation of the parent class when change the repr in the child class.

    @serhiy-storchaka serhiy-storchaka added 3.8 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) labels May 4, 2019
    @serhiy-storchaka serhiy-storchaka changed the title Do not set tp_str Do not define unneeded __str__ equal to __repr__ May 4, 2019
    @gpshead gpshead added the type-feature A feature request or enhancement label May 5, 2019
    @MojoVampire
    Copy link
    Mannequin

    MojoVampire mannequin commented May 5, 2019

    I like this. Always annoying to explicitly override both __repr__ and __str__ on subclasses of stuff like int when they should be the same. Patch looks good to me; I was originally wondering why some classes were replacing:

    __str__ = __repr__

    with:

    __str__ = object.__str__

    but checking their inheritance chains, it's clear *some* overload is needed for consistency, and using object.__str__ means reverting to the default case where subclasses only need to overload __repr__, rather than forcing all subclasses to overload both.

    @serhiy-storchaka
    Copy link
    Member Author

    New changeset 96aeaec by Serhiy Storchaka in branch 'master':
    bpo-36793: Remove unneeded __str__ definitions. (GH-13081)
    96aeaec

    @MojoVampire
    Copy link
    Mannequin

    MojoVampire mannequin commented Jul 17, 2019

    Serhiy: Did you plan to do any further work, or can this issue be closed?

    @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 interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants