Skip to content

UP008 should only apply when the __class__ cell exists #19357

Open
@dscorbett

Description

@dscorbett

Summary

super-call-with-parameters (UP008) should only apply when super is a global variable or __class__ is a nonlocal variable. Expressions that are merely equal to super aren’t good enough. Otherwise, the zero-argument super call doesn’t work. One solution is for the autofix to rewrite the super-equivalent expression to super, although that won’t work in all contexts. Example:

$ cat >up008.py <<'# EOF'
import builtins
class A:
    def f(self):
        print("!")
class B(A):
    def f(self):
        builtins.super(B, self).f()
B().f()
# EOF

$ python up008.py
!

$ ruff --isolated check up008.py --select UP008 --unsafe-fixes --fix
Found 1 error (1 fixed, 0 remaining).

$ cat up008.py
import builtins
class A:
    def f(self):
        print("!")
class B(A):
    def f(self):
        builtins.super().f()
B().f()

$ python up008.py 2>&1 | tail -n 1
RuntimeError: super(): __class__ cell not found

Note that the expression that UP008 rewrites can still use builtins.super (or other expressions that equal super but are not literally super) but super or __class__ just needs to be mentioned somewhere in the method in order for the __class__ cell, which the zero-argument super uses, to be created. True positive example with super:

$ cat >up008_super.py <<'# EOF'
import builtins
super = None  # It still works even if `super` is shadowed!
class A:
    def f(self):
        print("!")
class B(A):
    def f(self):
        if False: super
        builtins.super(B, self).f()
B().f()
# EOF

$ ruff --isolated check up008_super.py --select UP008 --unsafe-fixes --fix
Found 1 error (1 fixed, 0 remaining).

$ python up008_super.py
!

True positive example with __class__:

$ cat >up008_class.py <<'# EOF'
import builtins
class A:
    def f(self):
        print("!")
class B(A):
    def f(self):
        if False: __class__
        builtins.super(B, self).f()
B().f()
# EOF

$ ruff --isolated check up008_class.py --select UP008 --unsafe-fixes --fix
Found 1 error (1 fixed, 0 remaining).

$ python up008_class.py
!

Version

ruff 0.12.3 (5bc81f2 2025-07-11)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingfixesRelated to suggested fixes for violationshelp wantedContributions especially welcome

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions