12

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?

Gulzar
  • 17,272
  • 18
  • 86
  • 144
  • 1
    It's within `self`'s class, but even if it has the `_data` property there's no guarantee `other` is also an `A`. Have you read e.g. https://stackoverflow.com/q/44658367/3001761? – jonrsharpe Jun 04 '19 at 09:07
  • @jonrsharpe So what? the warning is about the private, not about "maybe not a correct type" – Gulzar Jun 04 '19 at 09:09
  • 3
    Are you aware that you can override the `==` operator in Python (and that the hook for doing so is `__eq__`, not `_equals`)? – user2357112 Jun 04 '19 at 09:09
  • 1
    It *doesn't* say *"maybe not a correct type"*, it says you're accessing a private member. Python is duck typed, from the interpreter's point of view it doesn't matter whether `other` is an `A` as long as it has the `_data` property. My point is that this may not be access within `other`'s class. – jonrsharpe Jun 04 '19 at 09:11
  • @user2357112 my problem is actually in one of my own functions, I just named it equals for the example. maybe it is confusing and should have just been named foo() – Gulzar Jun 04 '19 at 09:11
  • 1
    You should check they type... If you add an `if not isinstance(other, A): return false` you wont get the warning. Maybe simply adding the `, other: A` annotation is enough... – Giacomo Alzetta Jun 04 '19 at 09:12
  • @jonrsharpe which is why I expect it to assume _data exists for other, and I don't understand why that is relevant here. Will read the link you gave me, thanks! – Gulzar Jun 04 '19 at 09:12
  • @GiacomoAlzetta I don't understand what annotaion, could you elaborate please? – Gulzar Jun 04 '19 at 09:15
  • 1
    [Python3 supports type hints](https://docs.python.org/3/library/typing.html). PyCharm should be smart enough to use them to infer the types. – Giacomo Alzetta Jun 04 '19 at 09:16
  • @GiacomoAlzetta extremely cool! too bad this project is still on 2.7 Sure will use this when we migrate! – Gulzar Jun 04 '19 at 09:17
  • 1
    You can hint types in 2.7 with docstrings, PyCharm understands those too: https://www.jetbrains.com/help/pycharm/using-docstrings-to-specify-types.html – jonrsharpe Jun 04 '19 at 09:24

2 Answers2

15

If you really need it, like namedlist's ._asdict(), the answer is Can I get PyCharm to suppress a particular warning on a single line?:

class A(object):
    def __init__(self, data):
        self._data = data
    def _equals(self, other):
        # noinspection PyProtectedMember
        return self._data == other._data
Polv
  • 1,616
  • 1
  • 16
  • 31
3

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.

Gulzar
  • 17,272
  • 18
  • 86
  • 144