From 54a6dadde33580e9d9a2cbef1100ad341fc3775e Mon Sep 17 00:00:00 2001 From: Ehsan Iran-Nejad Date: Mon, 19 Feb 2024 06:07:47 -0600 Subject: [PATCH] properties with setters are now reported as 'property' for completion (#1983) * properties with setters are now reported as 'property' for completion * code cleanups * fixed test * fixed tests * Revert "fixed test" This reverts commit a80c955a489bce4a649452f7db49e59eab3ba38c. * code quality cleanup * so picky * Revert "Revert "fixed test"" This reverts commit 58dfc5292e9df5415a212f6bf28356e76d515157. * updated test per maintainer comments #1983 * removed extra char --- jedi/parser_utils.py | 14 +++++++++----- test/completion/context.py | 6 ++---- test/test_api/test_classes.py | 4 ++-- 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/jedi/parser_utils.py b/jedi/parser_utils.py index 3c7fa151..ddec28e8 100644 --- a/jedi/parser_utils.py +++ b/jedi/parser_utils.py @@ -320,7 +320,7 @@ def expr_is_dotted(node): return node.type == 'name' -def _function_is_x_method(*method_names): +def _function_is_x_method(decorator_checker): def wrapper(function_node): """ This is a heuristic. It will not hold ALL the times, but it will be @@ -330,12 +330,16 @@ def _function_is_x_method(*method_names): """ for decorator in function_node.get_decorators(): dotted_name = decorator.children[1] - if dotted_name.get_code() in method_names: + if decorator_checker(dotted_name.get_code()): return True return False return wrapper -function_is_staticmethod = _function_is_x_method('staticmethod') -function_is_classmethod = _function_is_x_method('classmethod') -function_is_property = _function_is_x_method('property', 'cached_property') +function_is_staticmethod = _function_is_x_method(lambda m: m == "staticmethod") +function_is_classmethod = _function_is_x_method(lambda m: m == "classmethod") +function_is_property = _function_is_x_method( + lambda m: m == "property" + or m == "cached_property" + or (m.endswith(".setter")) +) diff --git a/test/completion/context.py b/test/completion/context.py index d77c79c3..e3e596f2 100644 --- a/test/completion/context.py +++ b/test/completion/context.py @@ -21,11 +21,9 @@ class Y(X): #? [] def __doc__ - # This might or might not be what we wanted, currently properties are also - # used like this. IMO this is not wanted ~dave. - #? ['__class__'] - def __class__ #? [] + def __class__ + #? ['__class__'] __class__ diff --git a/test/test_api/test_classes.py b/test/test_api/test_classes.py index 080a6d57..0c3dac5a 100644 --- a/test/test_api/test_classes.py +++ b/test/test_api/test_classes.py @@ -348,8 +348,8 @@ def test_parent_on_comprehension(Script): def test_type(Script): for c in Script('a = [str()]; a[0].').complete(): - if c.name == '__class__' and False: # TODO fix. - assert c.type == 'class' + if c.name == '__class__': + assert c.type == 'property' else: assert c.type in ('function', 'statement')