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.

abstract_class_instantiated_py3.py 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. """Check that instantiating a class with
  2. `abc.ABCMeta` as metaclass fails if it defines
  3. abstract methods.
  4. """
  5. # pylint: disable=too-few-public-methods, missing-docstring
  6. # pylint: disable=abstract-method, import-error
  7. import abc
  8. import weakref
  9. from lala import Bala
  10. class GoodClass(object, metaclass=abc.ABCMeta):
  11. pass
  12. class SecondGoodClass(object, metaclass=abc.ABCMeta):
  13. def test(self):
  14. """ do nothing. """
  15. class ThirdGoodClass(object, metaclass=abc.ABCMeta):
  16. """ This should not raise the warning. """
  17. def test(self):
  18. raise NotImplementedError()
  19. class BadClass(object, metaclass=abc.ABCMeta):
  20. @abc.abstractmethod
  21. def test(self):
  22. """ do nothing. """
  23. class SecondBadClass(object, metaclass=abc.ABCMeta):
  24. @property
  25. @abc.abstractmethod
  26. def test(self):
  27. """ do nothing. """
  28. class ThirdBadClass(SecondBadClass):
  29. pass
  30. class Structure(object, metaclass=abc.ABCMeta):
  31. @abc.abstractmethod
  32. def __iter__(self):
  33. pass
  34. @abc.abstractmethod
  35. def __len__(self):
  36. pass
  37. @abc.abstractmethod
  38. def __contains__(self, _):
  39. pass
  40. @abc.abstractmethod
  41. def __hash__(self):
  42. pass
  43. class Container(Structure):
  44. def __contains__(self, _):
  45. pass
  46. class Sizable(Structure):
  47. def __len__(self):
  48. pass
  49. class Hashable(Structure):
  50. __hash__ = 42
  51. class Iterator(Structure):
  52. def keys(self): # pylint: disable=no-self-use
  53. return iter([1, 2, 3])
  54. __iter__ = keys
  55. class AbstractSizable(Structure):
  56. @abc.abstractmethod
  57. def length(self):
  58. pass
  59. __len__ = length
  60. class NoMroAbstractMethods(Container, Iterator, Sizable, Hashable):
  61. pass
  62. class BadMroAbstractMethods(Container, Iterator, AbstractSizable):
  63. pass
  64. class SomeMetaclass(metaclass=abc.ABCMeta):
  65. @abc.abstractmethod
  66. def prop(self):
  67. pass
  68. class FourthGoodClass(SomeMetaclass):
  69. """Don't consider this abstract if some attributes are
  70. there, but can't be inferred.
  71. """
  72. prop = Bala # missing
  73. def main():
  74. """ do nothing """
  75. GoodClass()
  76. SecondGoodClass()
  77. ThirdGoodClass()
  78. FourthGoodClass()
  79. weakref.WeakKeyDictionary()
  80. weakref.WeakValueDictionary()
  81. NoMroAbstractMethods()
  82. BadMroAbstractMethods() # [abstract-class-instantiated]
  83. BadClass() # [abstract-class-instantiated]
  84. SecondBadClass() # [abstract-class-instantiated]
  85. ThirdBadClass() # [abstract-class-instantiated]
  86. if 1: # pylint: disable=using-constant-test
  87. class FourthBadClass(object, metaclass=abc.ABCMeta):
  88. def test(self):
  89. pass
  90. else:
  91. class FourthBadClass(object, metaclass=abc.ABCMeta):
  92. @abc.abstractmethod
  93. def test(self):
  94. pass
  95. def main2():
  96. FourthBadClass() # [abstract-class-instantiated]