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.

non_iterator_returned.py 2.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. """Check non-iterators returned by __iter__ """
  2. # pylint: disable=too-few-public-methods, missing-docstring, no-self-use
  3. import six
  4. class FirstGoodIterator(object):
  5. """ yields in iterator. """
  6. def __iter__(self):
  7. for index in range(10):
  8. yield index
  9. class SecondGoodIterator(object):
  10. """ __iter__ and next """
  11. def __iter__(self):
  12. return self
  13. def __next__(self):
  14. """ Infinite iterator, but still an iterator """
  15. return 1
  16. def next(self):
  17. """Same as __next__, but for Python 2."""
  18. return 1
  19. class ThirdGoodIterator(object):
  20. """ Returns other iterator, not the current instance """
  21. def __iter__(self):
  22. return SecondGoodIterator()
  23. class FourthGoodIterator(object):
  24. """ __iter__ returns iter(...) """
  25. def __iter__(self):
  26. return iter(range(10))
  27. class IteratorMetaclass(type):
  28. def __next__(cls):
  29. return 1
  30. def next(cls):
  31. return 2
  32. @six.add_metaclass(IteratorMetaclass)
  33. class IteratorClass(object):
  34. """Iterable through the metaclass."""
  35. class FifthGoodIterator(object):
  36. """__iter__ returns a class which uses an iterator-metaclass."""
  37. def __iter__(self):
  38. return IteratorClass
  39. class FileBasedIterator(object):
  40. def __init__(self, path):
  41. self.path = path
  42. self.file = None
  43. def __iter__(self):
  44. if self.file is not None:
  45. self.file.close()
  46. self.file = open(self.path)
  47. # self file has two infered values: None and <instance of 'file'>
  48. # we don't want to emit error in this case
  49. return self.file
  50. class FirstBadIterator(object):
  51. """ __iter__ returns a list """
  52. def __iter__(self): # [non-iterator-returned]
  53. return []
  54. class SecondBadIterator(object):
  55. """ __iter__ without next """
  56. def __iter__(self): # [non-iterator-returned]
  57. return self
  58. class ThirdBadIterator(object):
  59. """ __iter__ returns an instance of another non-iterator """
  60. def __iter__(self): # [non-iterator-returned]
  61. return SecondBadIterator()
  62. class FourthBadIterator(object):
  63. """__iter__ returns a class."""
  64. def __iter__(self): # [non-iterator-returned]
  65. return ThirdBadIterator