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.

not_context_manager.py 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. """Tests that onjects used in a with statement implement context manager protocol"""
  2. # pylint: disable=too-few-public-methods, invalid-name, import-error, missing-docstring
  3. # pylint: disable=no-init,wrong-import-position
  4. # Tests no messages for objects that implement the protocol
  5. class Manager(object):
  6. def __enter__(self):
  7. pass
  8. def __exit__(self, type_, value, traceback):
  9. pass
  10. with Manager():
  11. pass
  12. class AnotherManager(Manager):
  13. pass
  14. with AnotherManager():
  15. pass
  16. # Tests message for class that doesn't implement the protocol
  17. class NotAManager(object):
  18. pass
  19. with NotAManager(): #[not-context-manager]
  20. pass
  21. # Tests contextlib.contextmanager usage is recognized as correct.
  22. from contextlib import contextmanager
  23. @contextmanager
  24. def dec():
  25. yield
  26. with dec(): # valid use
  27. pass
  28. # Tests a message is produced when a contextlib.contextmanager
  29. # decorated function is used without being called.
  30. with dec: # [not-context-manager]
  31. pass
  32. # Tests no messages about context manager protocol
  33. # if the type can't be inferred.
  34. from missing import Missing
  35. with Missing():
  36. pass
  37. # Tests context managers as names.
  38. def penelopa():
  39. return 42
  40. hopa = dec()
  41. tropa = penelopa()
  42. with tropa: # [not-context-manager]
  43. pass
  44. with hopa:
  45. pass
  46. # Tests that no messages are emitted for function calls
  47. # which return managers
  48. def wrapper():
  49. return dec()
  50. with wrapper():
  51. pass
  52. # Tests for properties returning managers.
  53. class Property(object):
  54. @property
  55. def ctx(self):
  56. return dec()
  57. @property
  58. def not_ctx(self):
  59. return 42
  60. lala = Property()
  61. with lala.ctx:
  62. # Don't emit when the context manager is the
  63. # result of accessing a property.
  64. pass
  65. with lala.not_ctx: # [not-context-manager]
  66. pass
  67. class TestKnownBases(Missing):
  68. pass
  69. with TestKnownBases():
  70. pass
  71. # Ignore mixins.
  72. class ManagerMixin(object):
  73. def test(self):
  74. with self:
  75. pass
  76. class FullContextManager(ManagerMixin):
  77. def __enter__(self):
  78. return self
  79. def __exit__(self, *args):
  80. pass
  81. # Test a false positive with returning a generator
  82. # from a context manager.
  83. def generator():
  84. yield 42
  85. @contextmanager
  86. def context_manager_returning_generator():
  87. return generator()
  88. with context_manager_returning_generator():
  89. pass
  90. FIRST = [context_manager_returning_generator()]
  91. with FIRST[0]:
  92. pass
  93. def other_indirect_func():
  94. return generator()
  95. def not_context_manager():
  96. return other_indirect_func()
  97. with not_context_manager(): # [not-context-manager]
  98. pass