How to suppress warning "Access to protected member" in pycharm method?

Viewed 5670

I have some class

class A(object):
    def __init__(self, data):
        self._data = data
    def _equals(self, other):
        return self._data == other._data

Pycharm doesn't like that I access other._data because it is private.

"Access to protected member"

This doesn't make sense to me, because the access is made from within the class.

How do I either suppress this warning, or write correct code?

2 Answers

Python 3.5+ answer (Type hints introduced):

from __future__ import annotations


class A(object):
    def __init__(self, data):
        self._data = data

    def _equals(self, other: A):
        return self._data == other._data

Using type hints as suggested by @Giacomo Alzetta, and allowing to type hint the class itself using from __future__ import annotations.

No need to hack PyCharm anymore or write ugly comments.


As also noted by @jonrsharpe, python 2 also supports type hints via docstrings.
I will not post this here as I support Python 2 losing support.

Related