Skip to content

Commit ae3f36d

Browse files
authored
Add tests to improve coverage (#8096)
2 parents 09df968 + e0a06e3 commit ae3f36d

File tree

5 files changed

+39
-15
lines changed

5 files changed

+39
-15
lines changed

pylint/checkers/base_checker.py

+6
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,12 @@ def messages(self) -> list[MessageDefinition]:
235235
]
236236

237237
def get_message_definition(self, msgid: str) -> MessageDefinition:
238+
# TODO: 3.0: Remove deprecated method
239+
warnings.warn(
240+
"'get_message_definition' is deprecated and will be removed in 3.0.",
241+
DeprecationWarning,
242+
stacklevel=2,
243+
)
238244
for message_definition in self.messages:
239245
if message_definition.msgid == msgid:
240246
return message_definition

pylint/checkers/classes/special_methods_checker.py

-3
Original file line numberDiff line numberDiff line change
@@ -296,9 +296,6 @@ def _is_dict(node: InferenceResult) -> bool:
296296

297297
@staticmethod
298298
def _is_iterator(node: InferenceResult) -> bool:
299-
if node is astroid.Uninferable:
300-
# Just ignore Uninferable objects.
301-
return True
302299
if isinstance(node, bases.Generator):
303300
# Generators can be iterated.
304301
return True

pylint/checkers/typecheck.py

+1-10
Original file line numberDiff line numberDiff line change
@@ -499,16 +499,7 @@ def _emit_no_member(
499499
return False
500500
except astroid.NotFoundError:
501501
return True
502-
if (
503-
owner.parent
504-
and isinstance(owner.parent, nodes.ClassDef)
505-
and owner.parent.name == "EnumMeta"
506-
and owner_name == "__members__"
507-
and node.attrname in {"items", "values", "keys"}
508-
):
509-
# Avoid false positive on Enum.__members__.{items(), values, keys}
510-
# See https://github.com/PyCQA/pylint/issues/4123
511-
return False
502+
512503
# Don't emit no-member if guarded behind `IF` or `IFExp`
513504
# * Walk up recursively until if statement is found.
514505
# * Check if condition can be inferred as `Const`,

tests/checkers/unittest_base_checker.py

+24
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44

55
"""Unittest for the BaseChecker class."""
66

7+
import pytest
78

89
from pylint.checkers import BaseChecker
910
from pylint.checkers.imports import ImportsChecker
1011
from pylint.checkers.typecheck import TypeChecker
12+
from pylint.exceptions import InvalidMessageError
1113
from pylint.extensions.while_used import WhileChecker
1214
from pylint.lint.pylinter import PyLinter
1315

@@ -26,6 +28,11 @@ def __init__(self) -> None:
2628
}
2729

2830

31+
class MissingFieldsChecker(BaseChecker):
32+
name = "basic"
33+
msgs = {"W0001": ("msg-name",)} # type: ignore[dict-item]
34+
35+
2936
class LessBasicChecker(OtherBasicChecker):
3037
options = (
3138
(
@@ -121,3 +128,20 @@ def test_base_checker_ordering() -> None:
121128
assert fake_checker_1 > fake_checker_3
122129
assert fake_checker_2 > fake_checker_3
123130
assert fake_checker_1 == fake_checker_2
131+
132+
133+
def test_base_checker_invalid_message() -> None:
134+
linter = PyLinter()
135+
136+
with pytest.raises(InvalidMessageError):
137+
linter.register_checker(MissingFieldsChecker(linter))
138+
139+
140+
def test_get_message_definition() -> None:
141+
checker = LessBasicChecker()
142+
with pytest.warns(DeprecationWarning):
143+
with pytest.raises(InvalidMessageError):
144+
checker.get_message_definition("W123")
145+
146+
with pytest.warns(DeprecationWarning):
147+
assert checker.get_message_definition("W0001")

tests/functional/n/non/non_iterator_returned.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""Check non-iterators returned by __iter__ """
22

3-
# pylint: disable=too-few-public-methods, missing-docstring, consider-using-with
4-
3+
# pylint: disable=too-few-public-methods, missing-docstring, consider-using-with, import-error
4+
from uninferable import UNINFERABLE
55

66
class FirstGoodIterator:
77
""" yields in iterator. """
@@ -99,3 +99,9 @@ class FourthBadIterator:
9999

100100
def __iter__(self): # [non-iterator-returned]
101101
return ThirdBadIterator
102+
103+
class SixthGoodIterator:
104+
"""__iter__ returns Uninferable."""
105+
106+
def __iter__(self):
107+
return UNINFERABLE

0 commit comments

Comments
 (0)