Skip to content

Commit 35813de

Browse files
authored
Move various tests from TestTypeChecker to functional tests (#5455)
* Remove duplicate test already in 'not_context_manager.py'
1 parent 8f12337 commit 35813de

File tree

6 files changed

+111
-183
lines changed

6 files changed

+111
-183
lines changed

tests/checkers/unittest_typecheck.py

Lines changed: 0 additions & 173 deletions
Original file line numberDiff line numberDiff line change
@@ -200,179 +200,6 @@ def test_nomember_on_c_extension_info_msg(self) -> None:
200200
with self.assertAddsMessages(message):
201201
self.checker.visit_attribute(node)
202202

203-
@set_config(
204-
contextmanager_decorators=(
205-
"contextlib.contextmanager",
206-
".custom_contextmanager",
207-
)
208-
)
209-
def test_custom_context_manager(self):
210-
"""Test that @custom_contextmanager is recognized as configured."""
211-
node = astroid.extract_node(
212-
"""
213-
from contextlib import contextmanager
214-
def custom_contextmanager(f):
215-
return contextmanager(f)
216-
@custom_contextmanager
217-
def dec():
218-
yield
219-
with dec():
220-
pass
221-
"""
222-
)
223-
with self.assertNoMessages():
224-
self.checker.visit_with(node)
225-
226-
def test_invalid_metaclass(self) -> None:
227-
module = astroid.parse(
228-
"""
229-
class InvalidAsMetaclass(object):
230-
pass
231-
232-
class FirstInvalid(object, metaclass=int):
233-
pass
234-
235-
class SecondInvalid(object, metaclass=InvalidAsMetaclass):
236-
pass
237-
238-
class ThirdInvalid(object, metaclass=2):
239-
pass
240-
241-
class FourthInvalid(object, metaclass=InvalidAsMetaclass()):
242-
pass
243-
"""
244-
)
245-
for class_obj, metaclass_name in (
246-
("FourthInvalid", "Instance of .InvalidAsMetaclass"),
247-
("ThirdInvalid", "2"),
248-
("SecondInvalid", "InvalidAsMetaclass"),
249-
("FirstInvalid", "int"),
250-
):
251-
classdef = module[class_obj]
252-
message = MessageTest(
253-
"invalid-metaclass", node=classdef, args=(metaclass_name,)
254-
)
255-
with self.assertAddsMessages(message):
256-
self.checker.visit_classdef(classdef)
257-
258-
def test_invalid_metaclass_function_metaclasses(self) -> None:
259-
module = astroid.parse(
260-
"""
261-
def invalid_metaclass_1(name, bases, attrs):
262-
return int
263-
def invalid_metaclass_2(name, bases, attrs):
264-
return 1
265-
class Invalid(metaclass=invalid_metaclass_1):
266-
pass
267-
class InvalidSecond(metaclass=invalid_metaclass_2):
268-
pass
269-
"""
270-
)
271-
for class_obj, metaclass_name in (("Invalid", "int"), ("InvalidSecond", "1")):
272-
classdef = module[class_obj]
273-
message = MessageTest(
274-
"invalid-metaclass", node=classdef, args=(metaclass_name,)
275-
)
276-
with self.assertAddsMessages(message):
277-
self.checker.visit_classdef(classdef)
278-
279-
def test_typing_namedtuple_not_callable_issue1295(self) -> None:
280-
module = astroid.parse(
281-
"""
282-
import typing
283-
Named = typing.NamedTuple('Named', [('foo', int), ('bar', int)])
284-
named = Named(1, 2)
285-
"""
286-
)
287-
call = module.body[-1].value
288-
callables = call.func.inferred()
289-
assert len(callables) == 1
290-
assert callables[0].callable()
291-
with self.assertNoMessages():
292-
self.checker.visit_call(call)
293-
294-
def test_typing_namedtuple_unsubscriptable_object_issue1295(self) -> None:
295-
module = astroid.parse(
296-
"""
297-
import typing
298-
MyType = typing.Tuple[str, str]
299-
"""
300-
)
301-
subscript = module.body[-1].value
302-
with self.assertNoMessages():
303-
self.checker.visit_subscript(subscript)
304-
305-
def test_staticmethod_multiprocessing_call(self) -> None:
306-
"""Make sure not-callable isn't raised for descriptors
307-
308-
astroid can't process descriptors correctly so
309-
pylint needs to ignore not-callable for them
310-
right now
311-
312-
Test for https://github.com/PyCQA/pylint/issues/1699
313-
"""
314-
call = astroid.extract_node(
315-
"""
316-
import multiprocessing
317-
multiprocessing.current_process() #@
318-
"""
319-
)
320-
with self.assertNoMessages():
321-
self.checker.visit_call(call)
322-
323-
def test_not_callable_uninferable_property(self) -> None:
324-
"""Make sure not-callable isn't raised for uninferable
325-
properties
326-
"""
327-
call = astroid.extract_node(
328-
"""
329-
class A:
330-
@property
331-
def call(self):
332-
return undefined
333-
334-
a = A()
335-
a.call() #@
336-
"""
337-
)
338-
with self.assertNoMessages():
339-
self.checker.visit_call(call)
340-
341-
def test_descriptor_call(self) -> None:
342-
call = astroid.extract_node(
343-
"""
344-
def func():
345-
pass
346-
347-
class ADescriptor:
348-
def __get__(self, instance, owner):
349-
return func
350-
351-
class AggregateCls:
352-
a = ADescriptor()
353-
354-
AggregateCls().a() #@
355-
"""
356-
)
357-
with self.assertNoMessages():
358-
self.checker.visit_call(call)
359-
360-
def test_unknown_parent(self) -> None:
361-
"""Make sure the callable check does not crash when a node's parent
362-
cannot be determined.
363-
"""
364-
call = astroid.extract_node(
365-
"""
366-
def get_num(n):
367-
return 2 * n
368-
get_num(10)()
369-
"""
370-
)
371-
with self.assertAddsMessages(
372-
MessageTest("not-callable", node=call, args="get_num(10)")
373-
):
374-
self.checker.visit_call(call)
375-
376203

377204
class TestTypeCheckerOnDecorators(CheckerTestCase):
378205
"Tests for pylint.checkers.typecheck on decorated functions."

tests/functional/i/invalid/m/invalid_metaclass.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,35 @@ class ThirdGood(object):
3232
@six.add_metaclass(ValidAsMetaclass)
3333
class FourthGood(object):
3434
pass
35+
36+
37+
class FirstInvalid(object, metaclass=int): # [invalid-metaclass]
38+
pass
39+
40+
41+
class SecondInvalid(object, metaclass=InvalidAsMetaclass): # [invalid-metaclass]
42+
pass
43+
44+
45+
class ThirdInvalid(object, metaclass=2): # [invalid-metaclass]
46+
pass
47+
48+
49+
class FourthInvalid(object, metaclass=InvalidAsMetaclass()): # [invalid-metaclass]
50+
pass
51+
52+
53+
def invalid_metaclass_1(name, bases, attrs):
54+
return int
55+
56+
57+
def invalid_metaclass_2(name, bases, attrs):
58+
return 1
59+
60+
61+
class Invalid(metaclass=invalid_metaclass_1): # [invalid-metaclass]
62+
pass
63+
64+
65+
class InvalidSecond(metaclass=invalid_metaclass_2): # [invalid-metaclass]
66+
pass
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
invalid-metaclass:37:0:38:8:FirstInvalid:Invalid metaclass 'int' used:UNDEFINED
2+
invalid-metaclass:41:0:42:8:SecondInvalid:Invalid metaclass 'InvalidAsMetaclass' used:UNDEFINED
3+
invalid-metaclass:45:0:46:8:ThirdInvalid:Invalid metaclass '2' used:UNDEFINED
4+
invalid-metaclass:49:0:50:8:FourthInvalid:Invalid metaclass 'Instance of invalid_metaclass.InvalidAsMetaclass' used:UNDEFINED
5+
invalid-metaclass:61:0:62:8:Invalid:Invalid metaclass 'int' used:UNDEFINED
6+
invalid-metaclass:65:0:66:8:InvalidSecond:Invalid metaclass '1' used:UNDEFINED

tests/functional/n/not_callable.py

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# pylint: disable=missing-docstring,no-self-use,too-few-public-methods,wrong-import-position,useless-object-inheritance,use-dict-literal
2+
# pylint: disable=wrong-import-order, undefined-variable
23

34
REVISION = None
45

@@ -117,7 +118,8 @@ def does_not_make_sense(self):
117118
PROP1.does_not_make_sense()
118119

119120

120-
import missing # pylint: disable=import-error
121+
import missing # pylint: disable=import-error
122+
121123

122124
class UnknownBaseCallable(missing.Blah):
123125
pass
@@ -133,3 +135,56 @@ def value(self):
133135
return 42
134136

135137
CLASS_WITH_PROP = ClassWithProperty().value() # [not-callable]
138+
139+
# Test typing.Namedtuple not callable
140+
# See: https://github.com/PyCQA/pylint/issues/1295
141+
import typing
142+
143+
Named = typing.NamedTuple("Named", [("foo", int), ("bar", int)])
144+
named = Named(1, 2)
145+
146+
# Test descriptor call
147+
def func():
148+
pass
149+
150+
151+
class ADescriptor:
152+
def __get__(self, instance, owner):
153+
return func
154+
155+
156+
class AggregateCls:
157+
a = ADescriptor()
158+
159+
160+
AggregateCls().a()
161+
162+
163+
# Make sure not-callable isn't raised for descriptors
164+
165+
# astroid can't process descriptors correctly so
166+
# pylint needs to ignore not-callable for them
167+
# right now
168+
169+
# Test for https://github.com/PyCQA/pylint/issues/1699
170+
171+
import multiprocessing
172+
173+
multiprocessing.current_process()
174+
175+
# Make sure not-callable isn't raised for uninferable properties
176+
class MyClass:
177+
@property
178+
def call(self):
179+
return undefined
180+
181+
182+
a = A()
183+
a.call()
184+
185+
# Make sure the callable check does not crash when a node's parent cannot be determined.
186+
def get_number(arg):
187+
return 2 * arg
188+
189+
190+
get_number(10)() # [not-callable]

tests/functional/n/not_callable.txt

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
not-callable:5:0:5:10::REVISION is not callable:UNDEFINED
2-
not-callable:23:12:23:22::INSTANCE is not callable:UNDEFINED
3-
not-callable:25:12:25:18::LIST is not callable:UNDEFINED
4-
not-callable:27:12:27:18::DICT is not callable:UNDEFINED
5-
not-callable:29:12:29:19::TUPLE is not callable:UNDEFINED
6-
not-callable:31:12:31:17::INT is not callable:UNDEFINED
7-
not-callable:66:0:66:13::PROP.test is not callable:UNDEFINED
8-
not-callable:67:0:67:13::PROP.custom is not callable:UNDEFINED
9-
not-callable:135:18:135:45::ClassWithProperty().value is not callable:UNDEFINED
1+
not-callable:6:0:6:10::REVISION is not callable:UNDEFINED
2+
not-callable:24:12:24:22::INSTANCE is not callable:UNDEFINED
3+
not-callable:26:12:26:18::LIST is not callable:UNDEFINED
4+
not-callable:28:12:28:18::DICT is not callable:UNDEFINED
5+
not-callable:30:12:30:19::TUPLE is not callable:UNDEFINED
6+
not-callable:32:12:32:17::INT is not callable:UNDEFINED
7+
not-callable:67:0:67:13::PROP.test is not callable:UNDEFINED
8+
not-callable:68:0:68:13::PROP.custom is not callable:UNDEFINED
9+
not-callable:137:18:137:45::ClassWithProperty().value is not callable:UNDEFINED
10+
not-callable:190:0:190:16::get_number(10) is not callable:UNDEFINED
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
"""Tests for unscubscriptable-object"""
2+
3+
# Test for typing.NamedTuple
4+
# See: https://github.com/PyCQA/pylint/issues/1295
5+
import typing
6+
7+
MyType = typing.Tuple[str, str]

0 commit comments

Comments
 (0)