Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Improve super() error message and document zero-arg super() usage inside nested functions and generator expressions. #113212

Copy link
Copy link
Closed
@WolframAlph

Description

@WolframAlph
Issue body actions

Bug report

Bug description:

I tried searching for something related but I could not find exactly this case with super. Maybe I missed… so posting it here.
Following code works fine:

class A:
    def foo(self):
        return 1


class B(A):
    def foo(self):
        return [super().foo() for _ in range(10)]


print(B().foo())

But if you modify it to be like this:

class A:
    def foo(self):
        return 1


class B(A):
    def foo(self):
        return list(super().foo() for _ in range(10))


print(B().foo())

you will get following error:

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

Before #101441 , [super().foo() for _ in range(10)] would cause the same error but it was fixed implicitly by PEP709.
You can fix this yourself by specifying type and instance in super():

class A:
    def foo(self):
        return 1


class B(A):
    def foo(self):
        return list(super(B, self).foo() for _ in range(10))


print(B().foo())

If you decide that fixing this makes sense, can I work on fixing it? To me it makes sense (if you decide to leave it as it is) at least to give some kind of warning or error that in this case you should explicitly provide type and instance to super, because it is quite confusing why user gets this error as semantically this is valid and should work

CPython versions tested on:

CPython main branch

Operating systems tested on:

Linux, macOS

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-featureA feature request or enhancementA feature request or enhancement

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.