New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Tab completion executes @property getter function #88915
Comments
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, <tab> 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.<tab>~~~Foo has a value of 4~~~
10 <tab>~~~Foo has a value of 4~~~
11
12 bar.print_value bar.value
13 >>> bar.p<tab>~~~Foo has a value of 4~~~
14 <tab>rint_value~~~Foo has a value of 4~~~
15 ~~~Foo has a value of 4~~~
16
17 bar.print_value
18 >>> bar.v<tab>alue 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. |
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. |
Woah, til the python shell has tab completion! This does seem like undesirable behavior. I'd like to work on a fix for this if that's all right, assuming that this behavior should not occur. I haven't exactly found where the tab autocomplete is implemented, but I'm assuming I'll find in one of the functions I see in the backtrace when I pause python at the interpreter prompt; maybe one of these? (my best quick guesses by function name).
If anyone can point me in the right direction, that'd be great, and I think I can work on this one tomorrow if that's all right! |
It looks to me like the issue is caused by the eval() in line 155 of the rlcompleter.py file ( Line 155 in bb3e0c2
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 ( Line 1162 in 196998e
|
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 ( Line 180 in bb3e0c2
A possible fix to the tab completion issue might be to add to/expand the warning about evaluating arbitrary C code ( Line 145 in bb3e0c2
|
That is correct! I was able to fix the undesirable behavior by adding an early exit condition if we appear to have a property object. I checked for the existence of a property object like this: class Foo:
@property
def bar(self):
return 'baz'
def is_property(object, attr):
return isinstance(getattr(type(object), attr, None), property)
The code that follows line 180 in the loop just checks if I opened a GitHub PR. Let me know what you think! |
Wow, that was quick and the code looks clean too! Thanks for fixing that up! |
Thanks, Jack! ✨ 🍰 ✨ |
@property
methods #27401@property
methods (GH-27401) #27444@property
methods (GH-27401) #27445Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: