Skip to content
New issue

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

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

Already on GitHub? # to your account

SPR001 false positives for two-argument super() #316

Closed
andersk opened this issue Oct 4, 2022 · 2 comments · Fixed by #323
Closed

SPR001 false positives for two-argument super() #316

andersk opened this issue Oct 4, 2022 · 2 comments · Fixed by #323
Assignees
Labels
bug Something isn't working

Comments

@andersk
Copy link
Contributor

andersk commented Oct 4, 2022

super(MyClass, arg) can only be replaced with super() if arg is the first argument to a function that’s defined within class MyClass. In other cases, the SPR001 diagnostic from ruff (and from flake8-super, I guess) is a false positive.

Some examples:

class BaseClass:
    def f(self):
        pass


def defined_outside(self):
    super(MyClass, self).f()  # CANNOT use super()


class MyClass(BaseClass):
    def normal(self):
        super(MyClass, self).f()  # can use super()
        super().f()

    def different_argument(self, other):
        super(MyClass, other).f()  # CANNOT use super()

    def comprehension_scope(self):
        [super(MyClass, self).f() for x in [1]]  # CANNOT use super()

    def inner_functions(self):
        def outer_argument():
            super(MyClass, self).f()  # CANNOT use super()

        def inner_argument(self):
            super(MyClass, self).f()  # can use super()
            super().f()

        outer_argument()
        inner_argument(self)

    def inner_class(self):
        class InnerClass:
            super(MyClass, self).f()  # CANNOT use super()

            def method(inner_self):
                super(MyClass, self).f()  # CANNOT use super()

        InnerClass().method()

    defined_outside = defined_outside


MyClass().normal()
MyClass().different_argument(MyClass())
MyClass().comprehension_scope()
MyClass().inner_functions()
MyClass().inner_class()
MyClass().defined_outside()

Some false positives in the wild:

https://github.com/zulip/zulip/blob/5.6/zerver/lib/test_helpers.py#L167
https://github.com/zulip/zulip/blob/5.6/zerver/tests/test_auth_backends.py#L326

@charliermarsh
Copy link
Member

Nice, good call, will fix.

@charliermarsh charliermarsh added the bug Something isn't working label Oct 4, 2022
@charliermarsh charliermarsh self-assigned this Oct 4, 2022
@charliermarsh
Copy link
Member

Working on this now.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants