Skip to content

SLF001: access to private field of another instance of the same class #9022

@nktrnv

Description

@nktrnv

Is this a bug or do I understand something wrong?

class Foo:
    def __init__(self) -> None:
        self._value = 0
    
    def get_new_instance_with_another_value(self, value: int) -> "Foo":
        foo = Foo()
        foo._value = value  # SLF001 Private member accessed: `_value`
        return foo

For example, in Java, such code will be valid:

public class Foo {
    private int value = 0;
    
    public Foo getNewInstanceWithAnotherValue(int value) {
        Foo foo = new Foo();
        foo.value = value; // No issues
        return foo;
    }
}

Metadata

Metadata

Assignees

No one assigned

    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