summaryrefslogtreecommitdiff
path: root/pylint/test/functional/not_context_manager.py
blob: 97f6e97281b87cd7089c9d9fc3c198afa1c13631 (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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
"""Tests that onjects used in a with statement implement context manager protocol"""

# pylint: disable=too-few-public-methods, invalid-name, import-error, missing-docstring
# pylint: disable=no-init
# 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


class TestKnownBases(Missing):
    pass

with TestKnownBases():
    pass

# Ignore mixins.
class ManagerMixin(object):
    def test(self):
        with self:
            pass

class FullContextManager(ManagerMixin):
    def __enter__(self):
        return self
    def __exit__(self, *args):
        pass

# Test a false positive with returning a generator
# from a context manager.
def generator():
    yield 42

@contextmanager
def context_manager_returning_generator():
    return generator()

with context_manager_returning_generator():
    pass

FIRST = [context_manager_returning_generator()]
with FIRST[0]:
    pass

def other_indirect_func():
    return generator()

def not_context_manager():
    return other_indirect_func()

with not_context_manager(): # [not-context-manager]
    pass