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

docs: operator dunder (__add__, et al.) invocations described incorrectly #85872

Closed
wchargin mannequin opened this issue Sep 3, 2020 · 2 comments
Closed

docs: operator dunder (__add__, et al.) invocations described incorrectly #85872

wchargin mannequin opened this issue Sep 3, 2020 · 2 comments
Labels
3.7 (EOL) end of life 3.8 only security fixes 3.9 only security fixes 3.10 only security fixes docs Documentation in the Doc dir type-feature A feature request or enhancement

Comments

@wchargin
Copy link
Mannequin

wchargin mannequin commented Sep 3, 2020

BPO 41706
Nosy @brettcannon, @wchargin
PRs
  • bpo-41706: Fix dunder operator invocation docs #22084
  • 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 2021-08-05.00:08:48.071>
    created_at = <Date 2020-09-03.20:30:50.412>
    labels = ['3.7', '3.8', '3.9', '3.10', 'type-feature', 'docs']
    title = 'docs: operator dunder (`__add__`, et al.) invocations described incorrectly'
    updated_at = <Date 2021-08-05.00:08:48.071>
    user = 'https://github.com/wchargin'

    bugs.python.org fields:

    activity = <Date 2021-08-05.00:08:48.071>
    actor = 'brett.cannon'
    assignee = 'docs@python'
    closed = True
    closed_date = <Date 2021-08-05.00:08:48.071>
    closer = 'brett.cannon'
    components = ['Documentation']
    creation = <Date 2020-09-03.20:30:50.412>
    creator = 'wchargin'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 41706
    keywords = ['patch']
    message_count = 2.0
    messages = ['376316', '398954']
    nosy_count = 3.0
    nosy_names = ['brett.cannon', 'docs@python', 'wchargin']
    pr_nums = ['22084']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue41706'
    versions = ['Python 3.6', 'Python 3.7', 'Python 3.8', 'Python 3.9', 'Python 3.10']

    @wchargin
    Copy link
    Mannequin Author

    wchargin mannequin commented Sep 3, 2020

    The operator override dunder methods, like __add__, are described in
    the “Data model” docs here:
    https://docs.python.org/3.10/reference/datamodel.html#object.__add__

    Those docs say:

    For instance, to evaluate the expression x + y, where x is an
    instance of a class that has an __add__() method, x.__add__(y) is
    called.

    But this is not correct: x + y always uses type(x).__add__, which
    may be different from x.__add__ if __add__ has been set directly on
    the instance.

    Demonstration:

    class C:
        def __add__(self, other):
            return "from class"
    
    
    c = C()
    print(c + c)  # prints "from class"
    
    c.__add__ = lambda other: "from instance"
    print(c.__add__(c))  # prints "from instance"
    print(type(c).__add__(c, c))  # prints "from class"
    
    print(c + c)  # prints "from class"!
    

    The same issue appears in the reversed operator dunder (__radd__,
    et al.) docs below.

    I have a patch that I can submit as a PR; just need a bpo number.

    @wchargin wchargin mannequin added 3.7 (EOL) end of life 3.8 only security fixes 3.9 only security fixes 3.10 only security fixes labels Sep 3, 2020
    @wchargin wchargin mannequin assigned docspython Sep 3, 2020
    @wchargin wchargin mannequin added docs Documentation in the Doc dir type-feature A feature request or enhancement 3.7 (EOL) end of life 3.8 only security fixes 3.9 only security fixes 3.10 only security fixes labels Sep 3, 2020
    @wchargin wchargin mannequin assigned docspython Sep 3, 2020
    @wchargin wchargin mannequin added docs Documentation in the Doc dir type-feature A feature request or enhancement labels Sep 3, 2020
    @brettcannon
    Copy link
    Member

    New changeset 80f33f2 by William Chargin in branch 'main':
    bpo-41706: Fix special method invocation docs to mention using type() (GH-22084)
    80f33f2

    @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.7 (EOL) end of life 3.8 only security fixes 3.9 only security fixes 3.10 only security fixes docs Documentation in the Doc dir type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    1 participant