[issue44752] Tab completion executes @property getter function
New submission from Ryan Pecor : After making a class using the @property decorator to implement a getter, using tab completion that matches the getter function name executes the function. See below for example (line numbers added, indicates when the user presses the tab key): 1 >>> class Foo(object): 2 ... def __init__(self,value): 3 ... self.value = value 4 ... @property 5 ... def print_value(self): 6 ... print("Foo has a value of {}".format(self.value)) 7 ... 8 >>> bar = Foo(4) 9 >>> bar.~~~Foo has a value of 4~~~ 10 ~~~Foo has a value of 4~~~ 11 12 bar.print_value bar.value 13 >>> bar.p~~~Foo has a value of 4~~~ 14 rint_value~~~Foo has a value of 4~~~ 15 ~~~Foo has a value of 4~~~ 16 17 bar.print_value 18 >>> bar.value I pressed tab after typing "bar." in line 9. It then printed the remainder of line 9 and moved the cursor to line 10. Pressing tab again prints line 10 and 11 before finally showing the expected output on line 12. lines 13-17 follow the same steps, but after typing "bar.p" to show that it happens whenever you tab and it matches the getter. Line 18 shows a correct tab completion resulting from hitting tab after typing "bar.v" which does not match the getter function. -- components: Interpreter Core messages: 398323 nosy: RPecor priority: normal severity: normal status: open title: Tab completion executes @property getter function type: behavior versions: Python 3.9 ___ Python tracker <https://bugs.python.org/issue44752> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue44752] Tab completion executes @property getter function
Ryan Pecor added the comment: I forgot to mention that I also added "~~~" to either side of the printed string every time it printed to help differentiate the printed string from commands that I typed into the interpreter. -- ___ Python tracker <https://bugs.python.org/issue44752> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue44752] Tab completion executes @property getter function
Ryan Pecor added the comment: It looks to me like the issue is caused by the eval() in line 155 of the rlcompleter.py file (https://github.com/python/cpython/blob/bb3e0c240bc60fe08d332ff5955d54197f79751c/Lib/rlcompleter.py#L155) which runs the function in order to see if it runs or raises an exception. I thought maybe replacing it with hasattr() might work, but it looks like the issue is repeated there as well! >>> hasattr(bar, "print_value") Foo has a value of 4 True This goes over to the C side of things now (https://github.com/python/cpython/blob/196998e220d6ca030e5a1c8ad63fcaed8e049a98/Python/bltinmodule.c#L1162) and I'll put in another issue regarding that! -- ___ Python tracker <https://bugs.python.org/issue44752> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue44752] Tab completion executes @property getter function
Ryan Pecor added the comment: Actually, hasattr() specifically states that it uses getattr() so that behavior is expected from getattr() so I will not be creating a separate issue for that. Now that I see hasattr() uses getattr(), it looks like the tab completion issue might not stem from line 155, but from line 180 (https://github.com/python/cpython/blob/bb3e0c240bc60fe08d332ff5955d54197f79751c/Lib/rlcompleter.py#L180) where it calls getattr(). A possible fix to the tab completion issue might be to add to/expand the warning about evaluating arbitrary C code (https://github.com/python/cpython/blob/bb3e0c240bc60fe08d332ff5955d54197f79751c/Lib/rlcompleter.py#L145) when using tab to autocomplete. -- ___ Python tracker <https://bugs.python.org/issue44752> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue44752] Tab completion executes @property getter function
Ryan Pecor added the comment: Wow, that was quick and the code looks clean too! Thanks for fixing that up! -- ___ Python tracker <https://bugs.python.org/issue44752> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com