Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

access_to_protected_members.py 1.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. # pylint: disable=too-few-public-methods, W0231, print-statement
  2. # pylint: disable=no-classmethod-decorator
  3. """Test external access to protected class members."""
  4. from __future__ import print_function
  5. class MyClass(object):
  6. """Class with protected members."""
  7. _cls_protected = 5
  8. def __init__(self, other):
  9. MyClass._cls_protected = 6
  10. self._protected = 1
  11. self.public = other
  12. self.attr = 0
  13. def test(self):
  14. """Docstring."""
  15. self._protected += self._cls_protected
  16. print(self.public._haha) # [protected-access]
  17. def clsmeth(cls):
  18. """Docstring."""
  19. cls._cls_protected += 1
  20. print(cls._cls_protected)
  21. clsmeth = classmethod(clsmeth)
  22. def _private_method(self):
  23. """Doing nothing."""
  24. class Subclass(MyClass):
  25. """Subclass with protected members."""
  26. def __init__(self):
  27. MyClass._protected = 5
  28. super(Subclass, self)._private_method()
  29. INST = Subclass()
  30. INST.attr = 1
  31. print(INST.attr)
  32. INST._protected = 2 # [protected-access]
  33. print(INST._protected) # [protected-access]
  34. INST._cls_protected = 3 # [protected-access]
  35. print(INST._cls_protected) # [protected-access]
  36. class Issue1031(object):
  37. """Test for GitHub issue 1031"""
  38. _attr = 1
  39. def correct_access(self):
  40. """Demonstrates correct access"""
  41. return type(self)._attr
  42. def incorrect_access(self):
  43. """Demonstrates incorrect access"""
  44. if self._attr == 1:
  45. return type(INST)._protected # [protected-access]
  46. return None