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