diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/functional/r/regression_02/regression_no_member_7631.py | 16 | ||||
-rw-r--r-- | tests/functional/t/ternary.py | 32 | ||||
-rw-r--r-- | tests/functional/t/ternary.txt | 16 | ||||
-rw-r--r-- | tests/functional/u/unnecessary/unnecessary_dunder_call.py | 6 | ||||
-rw-r--r-- | tests/test_check_parallel.py | 19 |
5 files changed, 74 insertions, 15 deletions
diff --git a/tests/functional/r/regression_02/regression_no_member_7631.py b/tests/functional/r/regression_02/regression_no_member_7631.py new file mode 100644 index 000000000..758aad057 --- /dev/null +++ b/tests/functional/r/regression_02/regression_no_member_7631.py @@ -0,0 +1,16 @@ +"""Regression test from https://github.com/PyCQA/pylint/issues/7631 +The following code should NOT raise no-member. +""" +# pylint: disable=missing-docstring,too-few-public-methods + +class Base: + attr: int = 2 + +class Parent(Base): + attr: int + +class Child(Parent): + attr = 2 + + def __init__(self): + self.attr = self.attr | 4 diff --git a/tests/functional/t/ternary.py b/tests/functional/t/ternary.py index 58171942f..48f97ffd9 100644 --- a/tests/functional/t/ternary.py +++ b/tests/functional/t/ternary.py @@ -1,18 +1,23 @@ """Test for old ternary constructs""" -from UNINFERABLE import condition, true_value, false_value, some_callable # pylint: disable=import-error +from UNINFERABLE import condition, some_callable, maybe_true, maybe_false # pylint: disable=import-error -SOME_VALUE1 = true_value if condition else false_value -SOME_VALUE2 = condition and true_value or false_value # [consider-using-ternary] +TRUE_VALUE = True +FALSE_VALUE = False + +SOME_VALUE1 = TRUE_VALUE if condition else FALSE_VALUE +SOME_VALUE2 = condition and TRUE_VALUE or FALSE_VALUE # [consider-using-ternary] +NOT_SIMPLIFIABLE_1 = maybe_true if condition else maybe_false +NOT_SIMPLIFIABLE_2 = condition and maybe_true or maybe_false SOME_VALUE3 = condition def func1(): """Ternary return value correct""" - return true_value if condition else false_value + return TRUE_VALUE if condition else FALSE_VALUE def func2(): """Ternary return value incorrect""" - return condition and true_value or false_value # [consider-using-ternary] + return condition and TRUE_VALUE or FALSE_VALUE # [consider-using-ternary] SOME_VALUE4 = some_callable(condition) and 'ERROR' or 'SUCCESS' # [consider-using-ternary] @@ -30,10 +35,23 @@ IS_LEAP_YEAR = YEAR % 4 == 0 and YEAR % 100 != 0 or YEAR % 400 == 0 def func4(): """"Using a Name as a condition but still emits""" truth_value = 42 - return condition and truth_value or false_value # [consider-using-ternary] + return condition and truth_value or FALSE_VALUE # [consider-using-ternary] def func5(): """"Using a Name that infers to False as a condition does not emit""" falsy_value = False - return condition and falsy_value or false_value # [simplify-boolean-expression] + return condition and falsy_value or FALSE_VALUE # [simplify-boolean-expression] + + +def func_control_flow(): + """Redefining variables should invalidate simplify-boolean-expression.""" + flag_a = False + flag_b = False + for num in range(2): + if num == 1: + flag_a = True + else: + flag_b = True + multiple = (flag_a and flag_b) or func5() + return multiple diff --git a/tests/functional/t/ternary.txt b/tests/functional/t/ternary.txt index bdec7bcc4..ca93acd2f 100644 --- a/tests/functional/t/ternary.txt +++ b/tests/functional/t/ternary.txt @@ -1,8 +1,8 @@ -consider-using-ternary:5:0:5:53::Consider using ternary (true_value if condition else false_value):UNDEFINED -consider-using-ternary:15:4:15:50:func2:Consider using ternary (true_value if condition else false_value):UNDEFINED -consider-using-ternary:18:0:18:63::Consider using ternary ('ERROR' if some_callable(condition) else 'SUCCESS'):UNDEFINED -consider-using-ternary:19:0:19:60::Consider using ternary ('greater' if SOME_VALUE1 > 3 else 'not greater'):UNDEFINED -consider-using-ternary:20:0:20:67::Consider using ternary ('both' if SOME_VALUE2 > 4 and SOME_VALUE3 else 'not'):UNDEFINED -simplify-boolean-expression:23:0:23:50::Boolean expression may be simplified to SOME_VALUE2:UNDEFINED -consider-using-ternary:33:4:33:51:func4:Consider using ternary (truth_value if condition else false_value):UNDEFINED -simplify-boolean-expression:39:4:39:51:func5:Boolean expression may be simplified to false_value:UNDEFINED +consider-using-ternary:8:0:8:53::Consider using ternary (TRUE_VALUE if condition else FALSE_VALUE):INFERENCE +consider-using-ternary:20:4:20:50:func2:Consider using ternary (TRUE_VALUE if condition else FALSE_VALUE):INFERENCE +consider-using-ternary:23:0:23:63::Consider using ternary ('ERROR' if some_callable(condition) else 'SUCCESS'):INFERENCE +consider-using-ternary:24:0:24:60::Consider using ternary ('greater' if SOME_VALUE1 > 3 else 'not greater'):INFERENCE +consider-using-ternary:25:0:25:67::Consider using ternary ('both' if SOME_VALUE2 > 4 and SOME_VALUE3 else 'not'):INFERENCE +simplify-boolean-expression:28:0:28:50::Boolean expression may be simplified to SOME_VALUE2:INFERENCE +consider-using-ternary:38:4:38:51:func4:Consider using ternary (truth_value if condition else FALSE_VALUE):INFERENCE +simplify-boolean-expression:44:4:44:51:func5:Boolean expression may be simplified to FALSE_VALUE:INFERENCE diff --git a/tests/functional/u/unnecessary/unnecessary_dunder_call.py b/tests/functional/u/unnecessary/unnecessary_dunder_call.py index cd1f21286..18e4ef855 100644 --- a/tests/functional/u/unnecessary/unnecessary_dunder_call.py +++ b/tests/functional/u/unnecessary/unnecessary_dunder_call.py @@ -122,3 +122,9 @@ INSTANTIATED_SELF = int("1").__add__(1) # [unnecessary-dunder-call] # since we can't apply alternate operators/functions here. a = [1, 2, 3] assert super(type(a), a).__str__() == "[1, 2, 3]" + +class MyString(str): + """Custom str implementation""" + def rjust(self, width, fillchar= ' '): + """Acceptable call to __index__""" + width = width.__index__() diff --git a/tests/test_check_parallel.py b/tests/test_check_parallel.py index 3b6d82e04..a7fb5c158 100644 --- a/tests/test_check_parallel.py +++ b/tests/test_check_parallel.py @@ -11,6 +11,7 @@ from __future__ import annotations import argparse import multiprocessing import os +from pickle import PickleError import dill import pytest @@ -231,6 +232,24 @@ class TestCheckParallelFramework: assert stats.statement == 18 assert stats.warning == 0 + def test_linter_with_unpickleable_plugins_is_pickleable(self) -> None: + """The linter needs to be pickle-able in order to be passed between workers""" + linter = PyLinter(reporter=Reporter()) + # We load an extension that we know is not pickle-safe + linter.load_plugin_modules(["pylint.extensions.overlapping_exceptions"]) + try: + dill.dumps(linter) + assert False, "Plugins loaded were pickle-safe! This test needs altering" + except (KeyError, TypeError, PickleError, NotImplementedError): + pass + + # And expect this call to make it pickle-able + linter.load_plugin_configuration() + try: + dill.dumps(linter) + except KeyError: + assert False, "Cannot pickle linter when using non-pickleable plugin" + def test_worker_check_sequential_checker(self) -> None: """Same as test_worker_check_single_file_no_checkers with SequentialTestChecker.""" linter = PyLinter(reporter=Reporter()) |