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.

slots_checks.py 1.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. """ Checks that classes uses valid __slots__ """
  2. # pylint: disable=too-few-public-methods, missing-docstring, no-absolute-import
  3. # pylint: disable=using-constant-test, wrong-import-position, no-else-return
  4. from collections import deque
  5. def func():
  6. if True:
  7. return ("a", "b", "c")
  8. else:
  9. return [str(var) for var in range(3)]
  10. class NotIterable(object):
  11. def __iter_(self):
  12. """ do nothing """
  13. class Good(object):
  14. __slots__ = ()
  15. class SecondGood(object):
  16. __slots__ = []
  17. class ThirdGood(object):
  18. __slots__ = ['a']
  19. class FourthGood(object):
  20. __slots__ = ('a%s' % i for i in range(10))
  21. class FifthGood(object):
  22. __slots__ = deque(["a", "b", "c"])
  23. class SixthGood(object):
  24. __slots__ = {"a": "b", "c": "d"}
  25. class Bad(object): # [invalid-slots]
  26. __slots__ = list
  27. class SecondBad(object): # [invalid-slots]
  28. __slots__ = 1
  29. class ThirdBad(object):
  30. __slots__ = ('a', 2) # [invalid-slots-object]
  31. class FourthBad(object): # [invalid-slots]
  32. __slots__ = NotIterable()
  33. class FifthBad(object):
  34. __slots__ = ("a", "b", "") # [invalid-slots-object]
  35. class SixthBad(object): # [single-string-used-for-slots]
  36. __slots__ = "a"
  37. class SeventhBad(object): # [single-string-used-for-slots]
  38. __slots__ = ('foo')
  39. class EighthBad(object): # [single-string-used-for-slots]
  40. __slots__ = deque.__name__
  41. class PotentiallyGood(object):
  42. __slots__ = func()
  43. class PotentiallySecondGood(object):
  44. __slots__ = ('a', deque.__name__)
  45. import six
  46. class Metaclass(type):
  47. def __iter__(cls):
  48. for value in range(10):
  49. yield str(value)
  50. @six.add_metaclass(Metaclass)
  51. class IterableClass(object):
  52. pass
  53. class PotentiallyThirdGood(object):
  54. __slots__ = IterableClass
  55. class PotentiallyFourthGood(object):
  56. __slots__ = Good.__slots__