Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

False positive for protected-access / W0212 in a very simple class #9891

Closed
bshoshany opened this issue Aug 23, 2024 · 3 comments
Closed

False positive for protected-access / W0212 in a very simple class #9891

bshoshany opened this issue Aug 23, 2024 · 3 comments
Labels
Duplicate 🐫 Duplicate of an already existing issue

Comments

@bshoshany
Copy link
Contributor

bshoshany commented Aug 23, 2024

Bug description

Pylint seems to be reporting a false positive, unless I'm misunderstanding something about how classes work. Here is a minimal example:

# pylint: disable=missing-module-docstring, missing-class-docstring, missing-function-docstring, too-few-public-methods

from typing import Self

class MyClass:
    def __init__(self) -> None:
        self._private = 42

    def test(self, other: Self) -> None:
        print(self._private)
        print(other._private)

Even though other is clearly a member of the same class MyClass based on the type annotation, Pylint complains about "Access to a protected member _private of a client class" with error code "protected-access / W0212". Looking at the error code description, it says:

"Used when a protected member (i.e. class member with a name beginning with an underscore) is access outside the class or a descendant of the class where it's defined."

Clearly, _private is being accesses from inside the class where it's defined, so this appears to be a false positive. Even type-annotating the class explicitly doesn't help:

# pylint: disable=missing-module-docstring, missing-class-docstring, missing-function-docstring, too-few-public-methods

from __future__ import annotations

class MyClass:
    def __init__(self) -> None:
        self._private = 42

    def test(self, other: MyClass) -> None:
        print(self._private)
        print(other._private)

Now the other object is explicitly defined as being an instance of MyClass, but Pylint still complains about access to a protected member.

Configuration

No response

Command used

pylint test.py

Pylint output

************* Module test
test.py:11:14: W0212: Access to a protected member _private of a client class (protected-access)

------------------------------------------------------------------
Your code has been rated at 8.57/10 (previous run: 8.57/10, +0.00)

Expected behavior

Any method in the class MyClass should be able to access the class's private members on any instance of that very same class.

Pylint version

pylint 3.2.6
astroid 3.3.1
Python 3.12.5 (tags/v3.12.5:ff3bc82, Aug  6 2024, 20:45:27) [MSC v.1940 64 bit (AMD64)]

OS / Environment

Windows 11

Additional dependencies

No response

@bshoshany bshoshany added the Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling label Aug 23, 2024
@jacobtylerwalls
Copy link
Member

Thanks for the report. The essence of the issue in this case is that pylint doesn't trust type annotations. There's an obvious value prop there for poorly or untyped projects.

@jacobtylerwalls jacobtylerwalls closed this as not planned Won't fix, can't repro, duplicate, stale Aug 23, 2024
@jacobtylerwalls jacobtylerwalls added Invalid Not a bug, already exists or already fixed and removed Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling labels Aug 23, 2024
@bshoshany
Copy link
Contributor Author

Thanks so much for your fast response! If I understand correctly, you are saying that Pylint doesn't trust the type annotation saying that other is of type Self (or MyClass) because it might be wrong if this is a poorly-typed project. If I may ask, what about well-typed projects, where the user spends hours ensuring all type annotations are correct (guilty...), and therefore this is indeed a clear false positive, as in the example above? Perhaps there can at least be some kind of flag that tells Pylint to trust the user's type annotations?

@jacobtylerwalls
Copy link
Member

These are good questions, and they can be explored more fully on #4813 in a context broader than just this message 👍 .

@jacobtylerwalls jacobtylerwalls added Duplicate 🐫 Duplicate of an already existing issue and removed Invalid Not a bug, already exists or already fixed labels Aug 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate 🐫 Duplicate of an already existing issue
Projects
None yet
Development

No branches or pull requests

2 participants