From 6fec464be57a9db182dc25466e82bc49f68cd121 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20van=20Noord?= <13665637+DanielNoord@users.noreply.github.com> Date: Fri, 8 Jul 2022 14:04:52 +0200 Subject: [PATCH] Fix a crash involving properties within ``try ... except`` blocks --- ChangeLog | 4 ++++ astroid/inference.py | 3 +++ tests/unittest_scoped_nodes.py | 24 ++++++++++++++++++++++++ 3 files changed, 31 insertions(+) diff --git a/ChangeLog b/ChangeLog index 4d3ac2a4d4..14a2bafb51 100644 --- a/ChangeLog +++ b/ChangeLog @@ -118,6 +118,10 @@ Release date: TBA Closes PyCQA/pylint#7092 +* Fix a crash involving properties within ``try ... except`` blocks. + + Closes PyCQA/pylint#6592 + * Prevent creating ``Instance`` objects that proxy other ``Instance``s when there is ambiguity (or user error) in calling ``__new__(cls)``. diff --git a/astroid/inference.py b/astroid/inference.py index de1535d3ba..7286b68cfd 100644 --- a/astroid/inference.py +++ b/astroid/inference.py @@ -1152,6 +1152,9 @@ def infer_functiondef( property_already_in_parent_locals = self.name in parent_frame.locals and any( isinstance(val, objects.Property) for val in parent_frame.locals[self.name] ) + # We also don't want to pass parent if the definition is within a Try node + if isinstance(self.parent, (nodes.TryExcept, nodes.TryFinally)): + property_already_in_parent_locals = True prop_func = objects.Property( function=self, diff --git a/tests/unittest_scoped_nodes.py b/tests/unittest_scoped_nodes.py index 45307c8bdc..13b879d774 100644 --- a/tests/unittest_scoped_nodes.py +++ b/tests/unittest_scoped_nodes.py @@ -2435,6 +2435,30 @@ class Derived(Parent): assert isinstance(inferred, objects.Property) +def test_property_in_body_of_try() -> None: + """Regression test for https://github.com/PyCQA/pylint/issues/6596.""" + node: nodes.Return = builder._extract_single_node( + """ + def myfunc(): + try: + + @property + def myfunc(): + return None + + except TypeError: + pass + + @myfunc.setter + def myfunc(): + pass + + return myfunc() #@ + """ + ) + next(node.value.infer()) + + def test_issue940_enums_as_a_real_world_usecase() -> None: node = builder.extract_node( """