Skip to content

SLF001 special-cases the name self #24275

@dscorbett

Description

@dscorbett

Summary

private-member-access (SLF001) has false positives and false negatives because it keys off the parameter name self. The name is just a convention; what really matters is that it is the first parameter of an instance method. If that parameter is not named self, there is a false positive. If a different parameter is named self, there is a false negative. If a parameter of a non-instance method (see classmethod-decorators and staticmethod-decorators) or a non-method is named self, there is a false negative. If a non-parameter variable is named self, there is a false negative. Example:

$ cat >slf001.py <<'# EOF'
class C:
    def false_positive(this):
        this._x = 0

    def false_negative_1(this, self):
        self._x = 1

    @classmethod
    def false_negative_2(self):
        return self._x

    @staticmethod
    def false_negative_3(self):
        return self._x

def false_negative_4(self):
    return self._x

def false_negative_5():
    self = C()
    return self._x

self = C()
def false_negative_6():
    return self._x
# EOF

$ ruff --isolated check --select SLF001 slf001.py                        
SLF001 Private member accessed: `_x`
 --> slf001.py:3:9
  |
1 | class C:
2 |     def false_positive(this):
3 |         this._x = 0
  |         ^^^^^^^
4 |
5 |     def false_negative_1(this, self):
  |

Found 1 error.

Version

ruff 0.15.8 (c2a8815 2026-03-26)

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions