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

super() does not work in nested functions, genexps, listcomps, and gives misleading exceptions #70682

Open
ztane mannequin opened this issue Mar 6, 2016 · 1 comment
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs)

Comments

@ztane
Copy link
Mannequin

ztane mannequin commented Mar 6, 2016

BPO 26495
Nosy @rhettinger, @benjaminp, @ztane, @jleedev, @tirkarthi

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 2016-03-06.18:47:20.307>
labels = ['interpreter-core']
title = 'super() does not work in nested functions, genexps, listcomps, and gives misleading exceptions'
updated_at = <Date 2020-01-09.18:40:36.658>
user = 'https://github.com/ztane'

bugs.python.org fields:

activity = <Date 2020-01-09.18:40:36.658>
actor = 'jleedev'
assignee = 'none'
closed = False
closed_date = None
closer = None
components = ['Interpreter Core']
creation = <Date 2016-03-06.18:47:20.307>
creator = 'ztane'
dependencies = []
files = []
hgrepos = []
issue_num = 26495
keywords = []
message_count = 1.0
messages = ['261266']
nosy_count = 5.0
nosy_names = ['rhettinger', 'benjamin.peterson', 'ztane', 'jleedev', 'xtreak']
pr_nums = []
priority = 'normal'
resolution = None
stage = None
status = 'open'
superseder = None
type = None
url = 'https://bugs.python.org/issue26495'
versions = []

@ztane
Copy link
Mannequin Author

ztane mannequin commented Mar 6, 2016

super() without arguments gives proper "super() without arguments" in functions, generator functions nested in methods, if *those* do not have arguments. But if you use super() in a nested function that takes an argument, or in a generator expression or a comprehension, you'd get

Got exception: TypeError super(type, obj): obj must be an instance or subtype of type

which is really annoying. Furthermore, if a nested function took another instance of type(self) as the first argument, then super() could refer unexpectedly to wrong instance:

    class Bar(Foo):
        def calculate(self, other_foos):
            def complicated_calculation(other):
                super().some_method(other)

            for item in other_foos:
                complicated_calculation(item)

now the super() call would not have implied self of calculate as the first argument, but the other argument of the nested function, all without warnings.

I believe it is a mistake that these nested functions can see __class__ at all, since it would just mostly lead them misbehaving unexpectedly.

@ztane ztane mannequin added the interpreter-core (Objects, Python, Grammar, and Parser dirs) label Mar 6, 2016
@ztane ztane mannequin changed the title super() does not work nested super() does not work in nested functions, genexps, listcomps, and gives misleading exceptions Mar 6, 2016
@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
interpreter-core (Objects, Python, Grammar, and Parser dirs)
Projects
None yet
Development

No branches or pull requests

0 participants