diff options
Diffstat (limited to 'tests/unittest_brain.py')
-rw-r--r-- | tests/unittest_brain.py | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/tests/unittest_brain.py b/tests/unittest_brain.py index d3bf12bc..a33647fe 100644 --- a/tests/unittest_brain.py +++ b/tests/unittest_brain.py @@ -3112,6 +3112,8 @@ def test_str_and_bytes(code, expected_class, expected_value): def test_no_recursionerror_on_self_referential_length_check() -> None: """ Regression test for https://github.com/PyCQA/astroid/issues/777 + + This test should only raise an InferenceError and no RecursionError. """ with pytest.raises(InferenceError): node = astroid.extract_node( @@ -3126,5 +3128,56 @@ def test_no_recursionerror_on_self_referential_length_check() -> None: node.inferred() +def test_inference_on_outer_referential_length_check() -> None: + """ + Regression test for https://github.com/PyCQA/pylint/issues/5244 + See also https://github.com/PyCQA/astroid/pull/1234 + + This test should succeed without any error. + """ + node = astroid.extract_node( + """ + class A: + def __len__(self) -> int: + return 42 + + class Crash: + def __len__(self) -> int: + a = A() + return len(a) + + len(Crash()) #@ + """ + ) + inferred = node.inferred() + assert len(inferred) == 1 + assert isinstance(inferred[0], nodes.Const) + assert inferred[0].value == 42 + + +def test_no_attributeerror_on_self_referential_length_check() -> None: + """ + Regression test for https://github.com/PyCQA/pylint/issues/5244 + See also https://github.com/PyCQA/astroid/pull/1234 + + This test should only raise an InferenceError and no AttributeError. + """ + with pytest.raises(InferenceError): + node = astroid.extract_node( + """ + class MyClass: + def some_func(self): + return lambda: 42 + + def __len__(self): + return len(self.some_func()) + + len(MyClass()) #@ + """ + ) + assert isinstance(node, nodes.NodeNG) + node.inferred() + + if __name__ == "__main__": unittest.main() |